<?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";
}
}
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, false 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, true given in /in/aO76k on line 42
Testing with class (with casting) ================================================>>
propertyOne => propertyOneValue
propertyThree => propertyThreeValue
foreachTest privateProperty => 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
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
foreachTest privateProperty => 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