3v4l.org

run code in 300+ PHP versions simultaneously
<?php $spamWords = ['blah', 'nah', 'hah', 'spam', 'spamm']; $spamMessage = 'Here we go blah blah blah'; // this is the way you do this $spam = 0; foreach ($spamWords as $spamWord) { if (strpos($spamMessage, $spamWord) === false) { echo 'This keyword aint here, previous value - ' . $spam . PHP_EOL; $spam = 0; } else { $spam++; echo 'I am spam' . PHP_EOL; } } if ($spam === 0) { echo 'not spam' . PHP_EOL; } else { echo 'spam' . PHP_EOL; } // well obviously, you will echo 'spam' only if lastword from your spam-words will be present // so you need to keep your variable echo 'Right approach' . PHP_EOL; $spam = 0; foreach ($spamWords as $spamWord) { if (strpos($spamMessage, $spamWord) !== false) { $spam++; } } // now works as expected if ($spam === 0) { echo 'not spam'; } else { echo 'spam'; }
Output for git.master, git.master_jit, rfc.property-hooks
I am spam This keyword aint here, previous value - 1 This keyword aint here, previous value - 0 This keyword aint here, previous value - 0 This keyword aint here, previous value - 0 not spam Right approach spam

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
34.66 ms | 406 KiB | 5 Q