3v4l.org

run code in 300+ PHP versions simultaneously
<?php $dataFormat = array( "dateTimeOfData" => "DateTime", "open" => "double", "high" => "double", "low" => "double", "close" => "double", "volume" => "integer", "isCritical" => "boolean"//Temporary ); function validateFormat($data){ //Check that all the required fields exist $missing = array_diff_key($dataFormat, $data); $extra = array_diff_key($data, $dataFormat); if(!empty($missing) || !empty($extra)){ throw new InvalidArgumentException("Incorrect data format provided"); } //Check that all the data is of the correct type foreach($data as $k => $v){ $type = gettype($v) === "object" ? get_class($v) : gettype($v); $correctType = $dataFormat[$k]; if($type !== $correctType){ throw new InvalidArgumentException("Data is of an incorrect type."); } } } $arr = array("dateTimeOfData" => new DateTime(), "open" => 10.1, "high" => 10.1, "low" => 10.1, "close" => 10.1, "volume" => 10, "isCritical" => true); validateFormat($arr);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Warning: Undefined variable $dataFormat in /in/qn14L on line 15 Fatal error: Uncaught TypeError: array_diff_key(): Argument #1 ($array) must be of type array, null given in /in/qn14L:15 Stack trace: #0 /in/qn14L(15): array_diff_key(NULL, Array) #1 /in/qn14L(36): validateFormat(Array) #2 {main} thrown in /in/qn14L on line 15
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Notice: Undefined variable: dataFormat in /in/qn14L on line 15 Warning: array_diff_key(): Expected parameter 1 to be an array, null given in /in/qn14L on line 15 Notice: Undefined variable: dataFormat in /in/qn14L on line 16 Warning: array_diff_key(): Expected parameter 2 to be an array, null given in /in/qn14L on line 16 Notice: Undefined variable: dataFormat in /in/qn14L on line 23 Notice: Trying to access array offset on value of type null in /in/qn14L on line 23 Fatal error: Uncaught InvalidArgumentException: Data is of an incorrect type. in /in/qn14L:25 Stack trace: #0 /in/qn14L(36): validateFormat(Array) #1 {main} thrown in /in/qn14L on line 25
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Warning: array_diff_key(): Expected parameter 1 to be an array, null given in /in/qn14L on line 15 Warning: array_diff_key(): Expected parameter 2 to be an array, null given in /in/qn14L on line 16 Fatal error: Uncaught InvalidArgumentException: Data is of an incorrect type. in /in/qn14L:25 Stack trace: #0 /in/qn14L(36): validateFormat(Array) #1 {main} thrown in /in/qn14L on line 25
Process exited with code 255.
Output for 7.3.0 - 7.3.31
Notice: Undefined variable: dataFormat in /in/qn14L on line 15 Warning: array_diff_key(): Expected parameter 1 to be an array, null given in /in/qn14L on line 15 Notice: Undefined variable: dataFormat in /in/qn14L on line 16 Warning: array_diff_key(): Expected parameter 2 to be an array, null given in /in/qn14L on line 16 Notice: Undefined variable: dataFormat in /in/qn14L on line 23 Fatal error: Uncaught InvalidArgumentException: Data is of an incorrect type. in /in/qn14L:25 Stack trace: #0 /in/qn14L(36): validateFormat(Array) #1 {main} thrown in /in/qn14L on line 25
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33
Notice: Undefined variable: dataFormat in /in/qn14L on line 15 Warning: array_diff_key(): Argument #1 is not an array in /in/qn14L on line 15 Notice: Undefined variable: dataFormat in /in/qn14L on line 16 Warning: array_diff_key(): Argument #2 is not an array in /in/qn14L on line 16 Notice: Undefined variable: dataFormat in /in/qn14L on line 23 Fatal error: Uncaught InvalidArgumentException: Data is of an incorrect type. in /in/qn14L:25 Stack trace: #0 /in/qn14L(36): validateFormat(Array) #1 {main} thrown in /in/qn14L on line 25
Process exited with code 255.
Output for 5.4.2 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28
Notice: Undefined variable: dataFormat in /in/qn14L on line 15 Warning: array_diff_key(): Argument #1 is not an array in /in/qn14L on line 15 Notice: Undefined variable: dataFormat in /in/qn14L on line 16 Warning: array_diff_key(): Argument #2 is not an array in /in/qn14L on line 16 Notice: Undefined variable: dataFormat in /in/qn14L on line 23 Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Data is of an incorrect type.' in /in/qn14L:25 Stack trace: #0 /in/qn14L(36): validateFormat(Array) #1 {main} thrown in /in/qn14L on line 25
Process exited with code 255.

preferences:
193.88 ms | 403 KiB | 275 Q