3v4l.org

run code in 300+ PHP versions simultaneously
<?php // tests testCase("Testing with null", null); testCase("Testing with false", false); testCase("Testing with true", true); testCase("Testing with class", new foreachTest()); testCase("Testing with array", ["key_1" => "val_1", "key_2" => "val_2"]); testCase("Testing with undefined"); // defining the class used in the test case class foreachTest { public $propertyOne = "propertyOneValue"; public $propertyThree = "propertyThreeValue"; private $privateProperty = "privateProperty"; } // test helper function function testCase($description){ // checking for optional argument in a way that allows to "pass in" an "undefined" value $argumentCount = func_num_args(); $inputVariable = $argumentCount > 1 ? func_get_arg(1) : 'noInputDefined' ; if ($inputVariable !== 'noInputDefined') { $undefined = $inputVariable; } try{ // echo test separator with description input echo "\n\n$description (with casting) " . str_repeat("=", 48) . ">>\n\n"; // testing foreach ((array)$undefined as $k => $v) { // potential output echo "$k => $v\n"; } echo "\n\n" . str_repeat("-", 24) . ">> (without casting)\n\n"; // testing without cast foreach ($undefined as $k => $v) { // potential output echo "$k => $v\n"; } }catch(Exception $e){ // test case error $errMessage = $e->getMessage(); echo "Error: $errMessage"; } }
Output for 8.0.0 - 8.0.13, 8.1rc1 - rc3
Testing with null (with casting) ================================================>> ------------------------>> (without casting) Warning: foreach() argument must be of type array|object, null given in /in/aO76k on line 42 Testing with false (with casting) ================================================>> 0 => ------------------------>> (without casting) Warning: foreach() argument must be of type array|object, bool given in /in/aO76k on line 42 Testing with true (with casting) ================================================>> 0 => 1 ------------------------>> (without casting) Warning: foreach() argument must be of type array|object, bool given in /in/aO76k on line 42 Testing with class (with casting) ================================================>> propertyOne => propertyOneValue propertyThree => propertyThreeValue foreachTestprivateProperty => privateProperty ------------------------>> (without casting) propertyOne => propertyOneValue propertyThree => propertyThreeValue Testing with array (with casting) ================================================>> key_1 => val_1 key_2 => val_2 ------------------------>> (without casting) key_1 => val_1 key_2 => val_2 Testing with undefined (with casting) ================================================>> Warning: Undefined variable $undefined in /in/aO76k on line 34 ------------------------>> (without casting) Warning: Undefined variable $undefined in /in/aO76k on line 42 Warning: foreach() argument must be of type array|object, null given in /in/aO76k on line 42
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.26
Testing with null (with casting) ================================================>> ------------------------>> (without casting) Warning: Invalid argument supplied for foreach() in /in/aO76k on line 42 Testing with false (with casting) ================================================>> 0 => ------------------------>> (without casting) Warning: Invalid argument supplied for foreach() in /in/aO76k on line 42 Testing with true (with casting) ================================================>> 0 => 1 ------------------------>> (without casting) Warning: Invalid argument supplied for foreach() in /in/aO76k on line 42 Testing with class (with casting) ================================================>> propertyOne => propertyOneValue propertyThree => propertyThreeValue foreachTestprivateProperty => privateProperty ------------------------>> (without casting) propertyOne => propertyOneValue propertyThree => propertyThreeValue Testing with array (with casting) ================================================>> key_1 => val_1 key_2 => val_2 ------------------------>> (without casting) key_1 => val_1 key_2 => val_2 Testing with undefined (with casting) ================================================>> Notice: Undefined variable: undefined in /in/aO76k on line 34 ------------------------>> (without casting) Notice: Undefined variable: undefined in /in/aO76k on line 42 Warning: Invalid argument supplied for foreach() in /in/aO76k on line 42
Output for 7.3.32 - 7.3.33
Testing with null (with casting) ================================================>> ------------------------>> (without casting) Warning: Invalid argument supplied for foreach() in /in/aO76k on line 42 Testing with false (with casting) ================================================>> 0 => ------------------------>> (without casting) Warning: Invalid argument supplied for foreach() in /in/aO76k on line 42 Testing with true (with casting) ================================================>> 0 => 1 ------------------------>> (without casting) Warning: Invalid argument supplied for foreach() in /in/aO76k on line 42 Testing with class (with casting) ================================================>> propertyOne => propertyOneValue propertyThree => propertyThreeValue foreachTestprivateProperty => privateProperty ------------------------>> (without casting) propertyOne => propertyOneValue propertyThree => propertyThreeValue Testing with array (with casting) ================================================>> key_1 => val_1 key_2 => val_2 ------------------------>> (without casting) key_1 => val_1 key_2 => val_2 Testing with undefined (with casting) ================================================>> ------------------------>> (without casting) Warning: Invalid argument supplied for foreach() in /in/aO76k on line 42
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/aO76k on line 8
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/aO76k on line 8
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/aO76k on line 8
Process exited with code 255.

preferences:
50.94 ms | 470 KiB | 5 Q