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.

Name: unexpected code optimisation in foreach loop fullscreencopydownloadembedprint


Your result can be seen below.

Result of php executing





Full code of unexpected code optimisation in foreach loop.php

  1. <?php
  2.  
  3.  
  4. $keys = [0, 1, 2];
  5.  
  6. function doSomething($key) {
  7.     $results = [
  8.         true,
  9.         false,
  10.         true,
  11.     ];
  12.  
  13.     echo 'doSomething with $key = ' . $key . "\n";
  14.  
  15.     return $results[$key];
  16. }
  17.  
  18. $result = true;
  19.  
  20. foreach($keys as $key) {
  21.     $result = $result && doSomething($key);
  22. }
  23.  
  24. echo 'Main result: ' . ($result 'true' : 'false') . "\n";
File Description
  • unexpected code optimisation in foreach loop
  • PHP Code
  • 17 Jan-2023
  • 355 Bytes
You can Share it: