简体   繁体   English

如何在 PHP 中检查 foreach() 循环的状态?

[英]How to check the status of a foreach() loop in Php?

I need to check the status of a foreach loop after importing an sql file into a new database.将 sql 文件导入新数据库后,我需要检查 foreach 循环的状态。 I'm not sure how to do it.我不知道该怎么做。

//import tables into new db
$newconn = new mysqli($servername, $username, $password, $newDB);
$filename = 'tables.sql'; 
$op_data = '';
$lines = file($filename);
foreach ($lines as $line)
{
    if (substr($line, 0, 2) == '--' || $line == '')//This IF Remove Comment Inside SQL FILE
    {
        continue;
    }
    $op_data .= $line;
    if (substr(trim($line), -1, 1) == ';')//Breack Line Upto ';' NEW QUERY
    {
        $newconn->query($op_data);
        $op_data = '';
    }
}

if(foreach == SUCCESS){
echo 'Success! Gabriel escaped Negan!';
}else{
echo 'Crap! Father Gabriel died!';
}

If you're just wanting to run code after your foreach loop has completed then you can just put that code after the loop.如果您只想在 foreach 循环完成后运行代码,那么您可以将该代码放在循环之后。 The code you put after the loop will no be run until the foreach loop has finished.在 foreach 循环完成之前,您放置在循环之后的代码不会运行。

foreach($lines as $line) {
    //loop code
}

//code to be run after loop ends

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM