-3

I'm getting the following error in PHP:

Notice: Use of undefined constant CONSTANT

on the exact line where I define it:

define(CONSTANT, true);

What am I doing wrong? I defined it, so why does it say "Undefined constant"?

Narf
  • 14,381
  • 3
  • 36
  • 65

5 Answers5

10

You need to quote the string which becomes a constant

define('CONSTANT', true);
RiggsFolly
  • 89,708
  • 20
  • 100
  • 143
donald123
  • 5,400
  • 3
  • 24
  • 23
6

The best way to understand what are you doing wrong is to read PHP manual.

Here is definition of define function.

bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )

So the first argument must be a string.

phpio.net
  • 123
  • 5
2

If you write it like that you are using the value of an already defined constant as a constant name.

What you want to do is to pass the name as a string:

define('CONSTANT', true);
Ibrahim
  • 1,954
  • 14
  • 21
1

Although not really, strictly relevant to your case, it is most desirable to first check that a CONSTANT has not been previously defined before (re)defining it.... It is also important to keep in mind that defining CONSTANTS using define requires that the CONSTANT to be defined is a STRING ie. enclosed within Quotes like so:

<?php

    // CHECK THAT THE CONSTANTS HASN'T ALREADY BEEN DEFINED BEFORE DEFINING IT...
    // AND BE SURE THE NAME OF THE CONSTANT IS WRAPPED WITHIN QUOTES...
    defined('A_CONSTANT') or define('A_CONSTANT', 'AN ALPHA-NUMERIC VALUE', true);

    // BUT USING THE CONSTANT, YOU NEED NOT ENCLOSE IT WITHIN QUOTES.
    echo A_CONSTANT;  //<== YIELDS::   "AN ALPHA-NUMERIC VALUE" 
Poiz
  • 7,519
  • 2
  • 13
  • 17
0

See below currect way to define constant

define('Variable','Value',case-sensitive);

Here Variable ==> Define your Constant Variable Name
Here Value ==> Define Constant value
Here case-sensitive Defult value 'false', Also you can set value 'true' and 'false'
Pravin Vavadiya
  • 3,099
  • 1
  • 15
  • 33