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"; } }

preferences:
23.08 ms | 405 KiB | 5 Q