-2

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,600
  • 3
  • 37
  • 66

5 Answers5

11

You need to quote the string which becomes a constant

define('CONSTANT', true);
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
donald123
  • 5,638
  • 3
  • 26
  • 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
  • 2,034
  • 15
  • 22
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,611
  • 2
  • 15
  • 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,195
  • 1
  • 17
  • 34