Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果有空的回显消息?_Php - Fatal编程技术网

Php 如果有空的回显消息?

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

嗨,我有一段代码,可以删除txt文件中的每个换行符。现在我的问题是,如果txt文件是空的,它会给出一条错误消息,
$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';