tgu*_*926 2 php ternary-operator
include ( $_GET['p'] == 'home') ? 'pages/home.php' : NULL;
Run Code Online (Sandbox Code Playgroud)
给出错误:
注意:未定义的索引:第38行的/var/www/index.php中的p
警告:require():第38行的/var/www/index.php中的文件名不能为空
致命错误:require():需要打开失败第38行/var/www/index.php中的''(include_path ='.:/ usr/share/php:/ usr/share/pear')
我理解未定义的索引,但为什么我得到其他错误?这一行:
include ( !isset($_GET['p'])) ? 'pages/home.php': NULL;
Run Code Online (Sandbox Code Playgroud)
工作良好.请注意,第一个代码在if语句中可以正常工作(除了未定义的索引,我理解)
include需要一个字符串,表示要包含的文件的路径.因此NULL将转换为导致空字符串的字符串.因此,包含空字符串引用的文件会导致警告.
只需使用一个if代替:
if (!isset($_GET['p'])) {
include 'pages/home.php';
}
Run Code Online (Sandbox Code Playgroud)