三元运算符和 PHP 包括
Ternary operators and PHP includes
我正在尝试使用三元运算符在包含文件而不是 if 语句之前检查文件的有效性;但由于某些原因,我无法让它运行,页面 returns 空白。我错过了什么?
(is_file('connect.php')) ? include 'connect.php' : echo "erreur de chargement";
(is_file('header.html')) ? include 'header.html' : echo "erreur de chargement";
(is_file('nav.html')) ? include 'nav.html' : echo "erreur de chargement";
(is_file('table.html')) ? include 'table.html' : echo "erreur de chargement";
(is_file('footer.html')) ? include 'footer.html' : echo "erreur de chargement";
PHP 中有内置功能:include
和 require
都具有 _once
的变体,无需这种三元调节即可为您完成工作。
include include 语句包含并评估指定的文件。
require require 与 include 相同,除了失败时它还会产生致命的 E_COMPILE_ERROR 级错误。换句话说,它将停止脚本,而 include 仅发出警告 (E_WARNING),允许脚本继续。
非常简单:如果 include
或 include_once 没有找到文件,那么 PHP 只生成 E_WARNING 并且脚本继续。
但是,如果 require
或 require_once 没有找到该文件,则 PHP 会生成致命错误并终止脚本。
使用它代替任何类型的条件,甚至是三元条件。
三元通常用于创建字符串,所以这不会起作用,因为您正在尝试使用三元不支持的表达式。
如果您出于某种原因绝对想避免 require
,最好制作一个您需要包含的文件数组,然后循环遍历它们。
$require = [
'connect.php',
'header.html'
//... etc
];
foreach($require as $file) {
if(is_file($file)) {
//if file exists, include and continue to next requirement
include $file;
continue;
}
//if this code is reached, the file does not exist.
echo "erreur de chargement $file";
//or die("erreur de chargement $file"); if you want the page to stop executing on failure
}
这也很好,因为您可以轻松添加新需求,甚至可以创建自动放入此数组的需求数据库。
但是,直接使用 require
.
肯定更容易,也更推荐
不允许你那样使用三元运算符。您不能将三元运算符的真实结果和回显作为错误结果包括在内。用 if
代替:
if (is_file('connect.php')) include 'connect.php' else echo "erreur de chargement";
if (is_file('header.html')) include 'header.html' else echo "erreur de chargement";
if (is_file('nav.html')) include 'nav.html' else echo "erreur de chargement";
if (is_file('table.html')) include 'table.html' else echo "erreur de chargement";
if (is_file('footer.html')) include 'footer.html' else echo "erreur de chargement";
为什么你看到空白页?
这是因为您关闭了错误报告。在代码的第一行使用它来启用错误报告:
error_reporting(E_ALL);
我正在尝试使用三元运算符在包含文件而不是 if 语句之前检查文件的有效性;但由于某些原因,我无法让它运行,页面 returns 空白。我错过了什么?
(is_file('connect.php')) ? include 'connect.php' : echo "erreur de chargement";
(is_file('header.html')) ? include 'header.html' : echo "erreur de chargement";
(is_file('nav.html')) ? include 'nav.html' : echo "erreur de chargement";
(is_file('table.html')) ? include 'table.html' : echo "erreur de chargement";
(is_file('footer.html')) ? include 'footer.html' : echo "erreur de chargement";
PHP 中有内置功能:include
和 require
都具有 _once
的变体,无需这种三元调节即可为您完成工作。
include include 语句包含并评估指定的文件。
require require 与 include 相同,除了失败时它还会产生致命的 E_COMPILE_ERROR 级错误。换句话说,它将停止脚本,而 include 仅发出警告 (E_WARNING),允许脚本继续。
非常简单:如果 include
或 include_once 没有找到文件,那么 PHP 只生成 E_WARNING 并且脚本继续。
但是,如果 require
或 require_once 没有找到该文件,则 PHP 会生成致命错误并终止脚本。
使用它代替任何类型的条件,甚至是三元条件。
三元通常用于创建字符串,所以这不会起作用,因为您正在尝试使用三元不支持的表达式。
如果您出于某种原因绝对想避免 require
,最好制作一个您需要包含的文件数组,然后循环遍历它们。
$require = [
'connect.php',
'header.html'
//... etc
];
foreach($require as $file) {
if(is_file($file)) {
//if file exists, include and continue to next requirement
include $file;
continue;
}
//if this code is reached, the file does not exist.
echo "erreur de chargement $file";
//or die("erreur de chargement $file"); if you want the page to stop executing on failure
}
这也很好,因为您可以轻松添加新需求,甚至可以创建自动放入此数组的需求数据库。
但是,直接使用 require
.
不允许你那样使用三元运算符。您不能将三元运算符的真实结果和回显作为错误结果包括在内。用 if
代替:
if (is_file('connect.php')) include 'connect.php' else echo "erreur de chargement";
if (is_file('header.html')) include 'header.html' else echo "erreur de chargement";
if (is_file('nav.html')) include 'nav.html' else echo "erreur de chargement";
if (is_file('table.html')) include 'table.html' else echo "erreur de chargement";
if (is_file('footer.html')) include 'footer.html' else echo "erreur de chargement";
为什么你看到空白页?
这是因为您关闭了错误报告。在代码的第一行使用它来启用错误报告:
error_reporting(E_ALL);