Why Else Loops The Number Of Array Times When If Does Not?

People,

Have a look at this foreach code:

<?php
/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

//script 4: https://stackoverflow.com/questions/13795789/check-if-string-contains-word-in-array
$content = "There are ***** all over the place!";
$banned_words = array('evil', 'devil', 'cruelty');
foreach($banned_words as $band) {
    $place = strpos($content, $band);
    if (!empty($place)) {
        echo "Script 4 - Found Banned Word: $band";
        exit;
    } else {
        echo "No banned words found!";
    }
}
?>

As you can see the number of loops that the foreach would make is dependant on the number of items in the array. Since the array has 3 items then from my test the foreach is getting looped 3 times.
Now, how do I know it is getting looped 3 times ? i know because of what is being echoed:

No banned words found!No banned words found!No banned words found!

You can see the phrase in the echo has been echoed 3 times. That means, the ELSE got triggered 3 times. Which also means the foreach got triggered 3 times. Right ?
Now, let’s change the code a little so that the IF->THEN gets triggered instead:


<?php
/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

//script 4: https://stackoverflow.com/questions/13795789/check-if-string-contains-word-in-array
$content = "There are devils all over the place!";
$banned_words = array('evil', 'devil', 'cruelty');
foreach($banned_words as $band) {
    $place = strpos($content, $band);
    if (!empty($place)) {
        echo "Script 4 - Found Banned Word: $band";
        exit;
    } else {
        echo "No banned words found!";
    }
}
?>

The echo is now:
Script 4 - Found Banned Word: evil

The script should have found devil too but it did not. And that is due to the loop getting halted after the 1st loop.
And so, my question is, why does the foreach only loop until the 1st match has been met ? Why doesn’t it check for the other matches too ? Why does it not for the words in the other array(s) ?
I mean, I did not create any condition for it to stop as soon as one match has been found. Now did I ? I did not use the DoWhile.
(When I used to build bots, I used to use the loop like this: Loop(total number), LoopWhile, LoopWhile Not. And, I know the DoWhile is the same as the LoopWhile we have).
Now, how to change the code so the script checks for matches for all the words present in the array ? In our case, the script should loop thrice checking for all 3 different words. And the result should show TRUE echoes like this:

Script 4 - Found Banned Word: evil
Script 4 - Found Banned Word: devil

Or, show like this:

Script 4 - Found Banned Word: evil
Script 4 - Found Banned Word: evil
No banned words found! (when checking for “cruelty” in our example).

What do you believe exit; does?

http://php.net/manual/en/function.exit.php

3 Likes

Opps!
I rid the EXIT and I now get what I want:

Script 4 - Found Banned Word: evilScript 4 - Found Banned Word: devilNo banned words found!

:stuck_out_tongue_closed_eyes:

So, what is the difference between the EXIT and the BREAK then ?

<?php
//script 4: https://stackoverflow.com/questions/13795789/check-if-string-contains-word-in-array

$content = "There are evil devils all over the place!";
$banned_words = array('evil', 'devil');

foreach($banned_words as $banned) {
    $place = strpos($content, $banned);
    if (!empty($place)) {
        echo "Script 4 - Found Banned Word: $band";
        exit;
    } else {
        echo "No banned words found!";
    }
}
?>
<br>
<?php
/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

//script 5: https://stackoverflow.com/questions/32522192/check-if-an-array-element-is-in-a-string

$banned_words = array("trash", "rubbish", "garbage");
$content = "This is my rubbish and trash text";
$banned = false;

for($i=0; $i < count($banned_words); $i++) {
    if(strrpos($content, $banned_words[$i]) != FALSE ) {
       $banned = true;
	   echo "Script 5 - Found Banned Word:";
	   print_r($banned_words[$i]);
       break;
    }
}
?>

Both stop looping as soon as a match is found. And they move no longer to the next item in the array.

Googling:
https://www.google.com/search?q=difference+between+exit+and+break+in+php+%3F&oq=difference+between+exit+and+break+in+php+%3F&gs_l=psy-ab.3..0i22i30k1.864311.879154.0.879463.44.34.1.0.0.0.512.4893.2-8j7j0j1.16.0....0...1.1.64.psy-ab..28.16.4385...0j0i67k1j0i131k1j0i10k1.317x_WNehmY

It is as if the questioner took the words right out of my mouth:

EDIT:
So, it seems the BREAK stops the loop and moves onto the next part of the code while the EXIT stops the script altogether. Was the answerer correct ? (Check the link above).

So, it seems the BREAK stops the loop and moves onto the next part of the code while the EXIT stops the script altogether. Was the answerer correct ?

Exactly.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.