3v4l.org

run code in 300+ PHP versions simultaneously
<?php function test($array) { try { return is_indexed_array($array); } catch (Exception $e) { echo $e->getMessage(); return 'error'; } } function is_indexed_array($var) { return (array_merge($var) === $var && is_numeric( implode( array_keys( $var ) ) ) ); } ////non-arrays //assert(test(null)); //assert(test('')); //assert(test(0)); //assert(test(false)); //assert(test(new stdClass)); assert(test([]) === true); // empty array assert(test([null, 1, 2]) === true); // random test assert(test(['' => '']) === false); // random test assert(test([0 => '']) === true); // random test assert(test([1 => 'a']) === false); // random test // numeric keys assert(test(['', 'a', 'b']) === true); // proper indexed array assert(test([1 => 'a', 2 => 'b', 3 => 'c']) === false); // sequential but not 0-based assert(test([-1 => '', 0 => '', 1 => 'a']) === false); // sequential but not 0-based assert(test([0 => '', 2 => 'b', 3 => 'c']) === false); // 1 index missing assert(test([0 => '', 2 => 'b', 1 => 'a']) === false); // numeric but unordered // string keys assert(test(['' => '', 'a' => 'a', 'b' => 'b']) === false); // proper dictionary // mixed keys assert(test(['' => '', 1 => 'a', 2 => 'b']) === false); // first key string but rest not; also, associative even though '' == 0 assert(test([0 => '', 1 => 'a', 'b' => 'b']) === false); // first key numeric but rest not // numeric string keys assert(test([' 0' => '']) === false); // associative even though ' 0' == 0 assert(test(['00' => '']) === false); // associative even though '00' == 0 assert(test(['0*' => '']) === false); // associative even though '0*' == 0 assert(test(['0.0' => '']) === false); // associative even though '0.0' == 0.0 assert(test(['0x0' => '']) === false); // associative even though '0x0' == 0 assert(test(['0e0' => '']) === false); // associative even though '0e0' == 0
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Fatal error: Uncaught AssertionError: assert(test([]) === true) in /in/ZF60D:28 Stack trace: #0 /in/ZF60D(28): assert(false, 'assert(test([])...') #1 {main} thrown in /in/ZF60D on line 28
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Fatal error: Uncaught AssertionError: assert(test([]) === true) in /in/ZF60D:28 Stack trace: #0 /in/ZF60D(28): assert(false, 'assert(test([])...') #1 {main} thrown in /in/ZF60D on line 28
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, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Warning: assert(): assert(test([]) === true) failed in /in/ZF60D on line 28 Warning: assert(): assert(test([' 0' => '']) === false) failed in /in/ZF60D on line 50 Warning: assert(): assert(test(['00' => '']) === false) failed in /in/ZF60D on line 51 Warning: assert(): assert(test(['0.0' => '']) === false) failed in /in/ZF60D on line 53 Warning: assert(): assert(test(['0e0' => '']) === false) failed in /in/ZF60D on line 55
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Warning: assert(): Assertion failed in /in/ZF60D on line 28 Warning: assert(): Assertion failed in /in/ZF60D on line 50 Warning: assert(): Assertion failed in /in/ZF60D on line 51 Warning: assert(): Assertion failed in /in/ZF60D on line 53 Warning: assert(): Assertion failed in /in/ZF60D on line 54 Warning: assert(): Assertion failed in /in/ZF60D on line 55

preferences:
188.21 ms | 402 KiB | 228 Q