So I have this code which assigns different variables depending on if a file exists or not:
$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;
}
but I need to do more with this, I need to make sure the file has content in it. I do the check in the conditional like this:
if (file_exists($progress_file_path) && count(file($scanned)) > 0)
because the $scanned
variable hasn't been defined yet. So the ideal solution would be if I can break out of the if statement, and jump to the else
part of the conditional.
Something like this:
$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;
}
but that doesn't work, you can't break or continue out of an if statement. I was thinking maybe using goto
to jump into the else
part of the conditional but I doubt that will work either. Is there a way to jump to the else part of a conditional like this?
You can try to check both of your conditions in one if statement to have at least one else like this
$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;
}
Refining Anant answer
$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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.