Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.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-中断if语句_Php - Fatal编程技术网

PHP-中断if语句

PHP-中断if语句,php,Php,我有一段代码,根据文件是否存在分配不同的变量: $progress_file_path = './data/progress.txt'; if (file_exists($progress_file_path) && count($scanned) > 0) { $scanned = file($progress_file_path); $last = end($scanned); $end = $limit + count($scanned);

我有一段代码,根据文件是否存在分配不同的变量:

$progress_file_path = './data/progress.txt';

if (file_exists($progress_file_path) && count($scanned) > 0) {
    $scanned = file($progress_file_path);
    $last = end($scanned);
    $end = $limit + count($scanned);
}
else { 
    $last = 'unset'; 
    $end = $limit;
}
但是我需要做更多的工作,我需要确保文件中有内容。我按以下条件进行签入:

if (file_exists($progress_file_path) && count(file($scanned)) > 0)
$progress_file_path = './data/progress.txt';

if (file_exists($progress_file_path)) {
    $scanned = file($progress_file_path);

    if (count($scanned) > 0) { break; }

    $last = end($scanned);
    $end = $limit + count($scanned);

}I 
else { 
    $last = 'unset'; 
    $end = $limit;
}
因为尚未定义
$scanned
变量。因此,理想的解决方案是,我可以跳出if语句,跳转到条件语句的
else
部分

大概是这样的:

if (file_exists($progress_file_path) && count(file($scanned)) > 0)
$progress_file_path = './data/progress.txt';

if (file_exists($progress_file_path)) {
    $scanned = file($progress_file_path);

    if (count($scanned) > 0) { break; }

    $last = end($scanned);
    $end = $limit + count($scanned);

}I 
else { 
    $last = 'unset'; 
    $end = $limit;
}

但这是行不通的,你不能打断或继续if语句。我在想,也许可以使用
goto
跳转到条件的
else
部分,但我怀疑这也行得通。有没有办法跳转到像这样的条件的else部分?

您可以尝试在一个if语句中检查两个条件,以至少有一个像这样的条件

$progress_file_path = './data/progress.txt';

if (file_exists($progress_file_path) && count(file($progress_file_path)) > 0) {
    $scanned = file($progress_file_path);

    $last = end($scanned);
    $end = $limit + count($scanned);
} else {
    $last = 'unset'; 
    $end = $limit;
}
精炼答案

$progress_file_path = './data/progress.txt';
$last = 'unset'; 
$end = $limit;
if (file_exists($progress_file_path)) {
    $scanned = file($progress_file_path);
    if (count($scanned) < 0) { 
        $last = end($scanned);
        $end = $limit + count($scanned);
    }
}

$progress_file_path='./data/progress.txt';
$last='unset';
$end=$limit;
如果(文件存在($progress\u file\u path)){
$scanned=文件($progress\u file\u path);
如果(计数($scanned)<0){
$last=结束($scanned);
$end=$limit+计数($scanned);
}
}

$progress\u file\u path='./data/progress.txt';如果(file_存在($progress_file_path)){$scanned=file($progress_file_path);如果(count($scanned)>0){$last='unset';$end=$limit;}否则{$last=end($scanned);$end=$limit+count($scanned);}
SelfDecode支持检查答案并将其中一个标记为接受答案。你也可以投票给其他人,如果他们将来有用的话。谢谢。但是如果文件$progress\u file\u path(progress.txt)不存在,那么当我尝试用file()打开它时,它会给我一个警告或错误。这是对的,但如果文件不存在,它不应该检查它。如果文件不存在,If的第一个条件为false,它将转到else