basic syntax question

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
CodeLobster

basic syntax question

Post by CodeLobster »

i need to stop the whole loop for a certain condition. if condition is met the script must jump

to the next foreach token, test again and continue if it passes the test.

end 1 and break stop the whole script.

end does it right.

Code: Select all

 $base = "file 1,file 2,file 3,file 4";
 foreach($file, $base, ",")
 {
 IF ($file == "file 3") { end; }
 ELSEIF ($file != "file 3") { echo "$file"; }
 }
1. is this the right syntax?

2. is it valid for while loops either?

CodeLobster

Re: basic syntax question

Post by CodeLobster »

suggestion: a command to jump to the next foreach token no matter how

the script is structured. im stuck on a situation of several nested operations and

such a command would save my ask. end does not solves all my problems.

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Script skip current loop with continue command

Post by Stefan »

How about continue ?

Code: Select all

$base = "file 1,file 2,file 3,file 4,file 5";
   foreach($file, $base, ",")
   {
     
      IF ($file == "file 3") { continue; }
     
      //ELSE:
       
      echo "$file"; 
    
   }


/*
Output:
file 1
file 2
file 4
file 5
*/

CodeLobster

Re: basic syntax question

Post by CodeLobster »

i had a distorted understanding on script command continue that was preventing me

from even trying it.

it solved my problem stefan, many thanks!

Post Reply