2
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)

hakre
  • 193,403
  • 52
  • 435
  • 836
tgun926
  • 1,573
  • 4
  • 21
  • 37
  • Notices are very low level. Most people ignore them or don't bother if they get a couple (which I'm for or against). The fatal error is the problem. – Jared Farrish Dec 16 '12 at 09:47
  • The error says require, your code has include. Please fix your question, it's obviously providing wrong information. Also the error messages are pretty clear. I wonder which part of those is not understandable to you. Would be good to know to give better answers. We also have a reference on site that might be useful to you: [Reference - What does this error mean in PHP?](http://stackoverflow.com/q/12769982/367456) - it also covers all the messages you have given. – hakre Dec 16 '12 at 09:52
  • Thanks for the link. I was using include, not require - even though the error outputted was to do with require. – tgun926 Dec 16 '12 at 10:50

2 Answers2

7

include expects a string that represents the path to the file that is to be included. So NULL will 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 if instead:

if (!isset($_GET['p'])) {
    include 'pages/home.php';
}
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • Oh, so ternary operators in PHP will check both cases before proceeding? Thank you – tgun926 Dec 16 '12 at 10:53
  • @helpabeginner No. It’s mainly because of the conditional expression that returns `null` in case the condition `$_GET['p'] == 'home'` is falsely. And that will then result in the case you’ve observed. – Gumbo Dec 16 '12 at 11:01
0

Please don't do that! Try something like this instead:

(isset($_GET['p']) && $_GET['p'] === 'home') ? include 'pages/home.php' : '';
PhearOfRayne
  • 4,990
  • 3
  • 31
  • 44
  • This code has a useless side effect of evaluating the expression `''` for no good reason. – Ja͢ck Dec 16 '12 at 09:53
  • @Jack I'd normally agree with you, however the OP is clearly wanting his code on one line. Otherwise they wouldn't be using a ternary statement to accomplish a simple if statement. – PhearOfRayne Dec 16 '12 at 09:56
  • this code does not work, `include` is not a function. – hakre Dec 16 '12 at 09:56
  • That's why we are here, to make them see they shouldn't put everything on one line :) – Ja͢ck Dec 16 '12 at 09:59
  • @hakre I would love to know where you think there is any reference to include being called a function? In php include is a special language construct, therefore parentheses are not needed around its argument. – PhearOfRayne Dec 16 '12 at 10:02
  • @StevenFarley: Don't stop, go on. The consequence of that is? [hint: see my comment above] – hakre Dec 16 '12 at 10:04
  • Your code was the first thing I was trying (to get rid of the warning notice). However, it still produces the fatal error and warning – tgun926 Dec 16 '12 at 10:52