unexpected code optimisation in foreach loop - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of unexpected code optimisation in foreach loop.php
- <?php
- $keys = [0, 1, 2];
- function doSomething($key) {
- $results = [
- true,
- false,
- true,
- ];
- echo 'doSomething with $key = ' . $key . "\n";
- return $results[$key];
- }
- $result = true;
- foreach($keys as $key) {
- $result = $result && doSomething($key);
- }
- echo 'Main result: ' . ($result ? 'true' : 'false') . "\n";