A Jenson A Jenson - 2 months ago 19
PHP Question

Why when I use isset() with a constant fatal error is thrown?

I'm using isset() in my script, and if I use with a constant instead of a variable a fatal error seems to be thrown.

My Question:
Why when I use isset() with a constant fatal error is thrown?

<?php

switch($current_page) {
case 'kb':
define('TITLE', 'Knowledge Base');
break;
case 'edit':
define('TITLE', 'Edit');
break;
case 'new':
define('TITLE', 'New');
break;
}
...
...
...
if (isset(TITLE)) {
echo "<h1>".TITLE."</h1>";
}


I ran some tests, and see that both the constant and variable have same data type:

define('TITLE', 'Test');
$title = 'Test';

var_dump(TITLE); // string(4) "Test"
var_dump($title); // string(4) "Test"


I checked http://php.net/manual/en/language.constants.php and do not see anything that would account for this.

Answer Source

PHP Documentation

Warning
isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined() function.