3v4l.org

run code in 300+ PHP versions simultaneously
<?php function check_array($arr = false, $index = false){ if(is_array($arr)){ if(is_numeric($index)||is_string($index)){ if(array_key_exists($index, $arr)){ return $arr[$index]; } } } return false; } $types = array( // boolean true, false, // sig int -2, -1, 0, 1, 2, // float 1.111, // string "Punc,tua!ti'o\"n", // array array(), array(0,1,2), array(1 => 0, 1, 2), // object new stdClass, // NULL NULL ); // print heading echo "array\tindex\tresult\n"; $run_once = false; foreach($types as $type){ $line = ''; $myArr = $type; foreach($types as $type){ $myIndex = $type; $line .= gettype($myArr); if(is_scalar($myArr)){ if($myArr === true){ $line .= '("TRUE")'; } else if($myArr === false){ $line .= '("FALSE")'; } else { $line .= "($myArr)"; } } $line .= "\t"; $line .= gettype($myIndex); if(is_scalar($myIndex)){ if($myIndex === true){ $line .= '("TRUE")'; } else if($myIndex === false){ $line .= '("FALSE")'; } else { $line .= "($myIndex)"; } } $line .= "\t"; $result = check_array($myArr, $myIndex); $line .= $result === false ? "FALSE" : $result; $line .= "\n"; echo $line; } }

preferences:
35.15 ms | 402 KiB | 5 Q