Php 如果有空的回显消息?
嗨,我有一段代码,可以删除txt文件中的每个换行符。现在我的问题是,如果txt文件是空的,它会给出一条错误消息,Php 如果有空的回显消息?,php,Php,嗨,我有一段代码,可以删除txt文件中的每个换行符。现在我的问题是,如果txt文件是空的,它会给出一条错误消息,$line不存在。现在,我想添加一段代码,如果txt没有内容,它将显示类似“列表中没有电子邮件”的消息 但这并没有给出任何结果……我可以想出一些方法来检查文件是否为空: $textfile = file('text.txt'); $lines = count($textfile); if (empty($lines)) echo 'file is empty'; 或: 或
$line
不存在。现在,我想添加一段代码,如果txt没有内容,它将显示类似“列表中没有电子邮件”的消息
但这并没有给出任何结果……我可以想出一些方法来检查文件是否为空:
$textfile = file('text.txt');
$lines = count($textfile);
if (empty($lines))
echo 'file is empty';
或:
或者,如果要检查它是否为空,不包括空行或额外的空白:
$textfile = trim(file_get_contents('text.txt'));
if (empty($textfile))
echo 'file is empty';
$textfile = file('text.txt');
$lines = count($textfile);
if (empty($lines))
echo 'file is empty';
if (filesize('text.txt') === 0)
echo 'file is empty';
$textfile = trim(file_get_contents('text.txt'));
if (empty($textfile))
echo 'file is empty';