Last active
December 10, 2015 19:48
-
-
Save jmfontaine/4483544 to your computer and use it in GitHub Desktop.
PHP && operator can break foreach loops Edit: I was wrong. There is no break here.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function isOk($message) { | |
echo $message . PHP_EOL; | |
return 'Error' !== $message; | |
} | |
$messages = array('Info', 'Error', 'Info'); | |
$isOk = true; | |
foreach ($messages as $message) { | |
$isOk = $isOk && isOk($message); | |
} | |
var_dump($isOk); | |
echo '------------' . PHP_EOL; | |
$isOk = true; | |
foreach ($messages as $message) { | |
if (!isOk($message)) { | |
$isOk = false; | |
} | |
} | |
var_dump($isOk); |
Ce qui m'étonnait c'était qu'il fasse un break implicite mais c'était une erreur d'interprétation de ma part donc plus rien ne m'étonne à présent (si ce n'est ma bêtise).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oui, il fini la boucle, mais n'évalue jamais
isOk()
. Qu'est ce qui t'étonnes, je ne saisis pas ?