How to do process with for() function and give return true in the last

时间:2015-06-26 09:56:53

标签: php

I have a project, and I have one problem. Can you help me? I have this code:

for($i=0; $i < $rows; $i++){
   if(do_something == true){
      return true;
   }else{
      return false;
   }
}     

If, I have 10 data, and I want do_something is process all my data. But the problem is, the script is stop in first data. So, I delete the return true; and leave it blank. Now the script process all my data. My question is, how can I put return true; in the end of the process, so I know no error in the process.

3 个答案:

答案 0 :(得分:0)

use this:

for($i=0; $i < $rows; $i++){
   if(do_something == false){
       return false;
   }
}
return true;

it will "do something" as long as it is successful, and until all are done, and abort at the first "false" result of "do something".

答案 1 :(得分:0)

Just change your logic a bit and do something like this:

So if something goes wrong you stop and return FALSE, if everything went correct you end the loop and return TRUE.

for($i = 0; $i < $rows; $i++) {
   if(do_something != TRUE)
       return FALSE;
}
return TRUE;

答案 2 :(得分:0)

you can try this

$j=0;
for($i=0; $i < $rows; $i++) {
    if(do_something == true) {
        /*return true;*/
        $j++;    
    } else {
        return false;
    }
}
if($j == $rows) {
    return true;
} else {
    return false;
}