Fix – PHP Notice: Use of undefined constant.

This is a common notice / warning that occurs whenever PHP has detected the usage of an undefined constant. ┬áIn case you didn’t already know, a constant is a simple value that cannot change during the execution of a script. i.e. If you define constant A as “123”, you won’t be able to change it to “456” at a later stage.

An example of a constant being defined in PHP:


//create a constant called PI
define('PI', 3.14);

//print it out to the screen.
echo PI;

PI is a good example of a constant, simply because the value for PI never changes. It will always be 3.14 and nothing in our code should be able to change that.

In some cases, this kind of notice can appear, even though the developer isn’t using any constants.

Forgetting to use a $ symbol at the start of a variable name.


//Create a simple variable called $name.
$name = "Wayne";

//Print it out.
echo name;

In the above example, I have forgotten to place a dollar sign ($) in front of my $name variable. This will result in the error:

Notice: Use of undefined constant name – assumed ‘name’

Because there is no dollar sign in front of the variable “name”, PHP assumes that I was trying to reference a constant variable called “name”.

Forgetting to place quotes around strings.


//Print POST variable called "email".
echo $_POST[email];

In the example above, I failed to place quotes around the $_POST variable “email”. This will cause the following notice:

Notice: Use of undefined constant name – assumed ’email’

To fix this, I’d obviously have to do the following:


//Print POST variable called "email".
echo $_POST["email"];

As you can see, the fix here was to place quotes around the string “email”.

Here is an example of an associated array that will cause this error:

//A custom array
$myArray = array(
    title => 'This is a title',
    page => 'page-url'

In this case, I have properly quoted the array’s elements. However, I have failed to quote the array’s keys. As a result, our script will spit out warnings about title and page being undefined constants.

Hopefully, you found this guide to be helpful!