include ( $_GET['p'] == 'home') ? 'pages/home.php' : NULL;
gives the error:
Notice: Undefined index: p in /var/www/index.php on line 38
Warning:
require(): Filename cannot be empty in /var/www/index.php on line 38
Fatal error: require(): Failed opening required ”
(include_path=’.:/usr/share/php:/usr/share/pear’) in
/var/www/index.php on line 38
I understand the undefined index, but why am I getting the other errors? This line:
include ( !isset($_GET['p'])) ? 'pages/home.php': NULL;
works fine. Note that the first code will work fine in an if statement (apart from the undefined index, which I understand)
includeexpects a string that represents the path to the file that is to be included. SoNULLwill get converted to string that results in an empty string. And consequently including a file referenced by an empty string results in a warning.Simply use an
ifinstead: