3v4l.org

run code in 300+ PHP versions simultaneously
<?php function test_val_as_array($val) { $type = gettype($val); echo "val before access = ".var_export($val, true)."\n"; echo "accessing $type as array {$val['test']}\n"; $val['test'] = 'wtf'; echo "set $type offset test to string wtf\n"; echo "accessing $type offset test {$val['test']}\n"; echo "val after access = ".var_export($val, true)."\n"; }; test_val_as_array(null); test_val_as_array('test'); test_val_as_array(123); test_val_as_array(true); test_val_as_array(1.2); test_val_as_array(new stdClass);
Output for 8.3.0 - 8.3.6
val before access = NULL Warning: Trying to access array offset on null in /in/PEICX on line 6 accessing NULL as array set NULL offset test to string wtf accessing NULL offset test wtf val after access = array ( 'test' => 'wtf', ) val before access = 'test' Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /in/PEICX:6 Stack trace: #0 /in/PEICX(14): test_val_as_array('test') #1 {main} thrown in /in/PEICX on line 6
Process exited with code 255.
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18
val before access = NULL Warning: Trying to access array offset on value of type null in /in/PEICX on line 6 accessing NULL as array set NULL offset test to string wtf accessing NULL offset test wtf val after access = array ( 'test' => 'wtf', ) val before access = 'test' Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /in/PEICX:6 Stack trace: #0 /in/PEICX(14): test_val_as_array('test') #1 {main} thrown in /in/PEICX on line 6
Process exited with code 255.
Output for 7.4.0 - 7.4.33
val before access = NULL Notice: Trying to access array offset on value of type null in /in/PEICX on line 6 accessing NULL as array set NULL offset test to string wtf accessing NULL offset test wtf val after access = array ( 'test' => 'wtf', ) val before access = 'test' Warning: Illegal string offset 'test' in /in/PEICX on line 6 accessing string as array t Warning: Illegal string offset 'test' in /in/PEICX on line 7 set string offset test to string wtf Warning: Illegal string offset 'test' in /in/PEICX on line 9 accessing string offset test w val after access = 'west' val before access = 123 Notice: Trying to access array offset on value of type int in /in/PEICX on line 6 accessing integer as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set integer offset test to string wtf Notice: Trying to access array offset on value of type int in /in/PEICX on line 9 accessing integer offset test val after access = 123 val before access = true Notice: Trying to access array offset on value of type bool in /in/PEICX on line 6 accessing boolean as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set boolean offset test to string wtf Notice: Trying to access array offset on value of type bool in /in/PEICX on line 9 accessing boolean offset test val after access = true val before access = 1.2 Notice: Trying to access array offset on value of type float in /in/PEICX on line 6 accessing double as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set double offset test to string wtf Notice: Trying to access array offset on value of type float in /in/PEICX on line 9 accessing double offset test val after access = 1.2 val before access = (object) array( ) Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /in/PEICX:6 Stack trace: #0 /in/PEICX(18): test_val_as_array(Object(stdClass)) #1 {main} thrown in /in/PEICX on line 6
Process exited with code 255.
Output for 7.3.0 - 7.3.33
val before access = NULL accessing NULL as array set NULL offset test to string wtf accessing NULL offset test wtf val after access = array ( 'test' => 'wtf', ) val before access = 'test' Warning: Illegal string offset 'test' in /in/PEICX on line 6 accessing string as array t Warning: Illegal string offset 'test' in /in/PEICX on line 7 set string offset test to string wtf Warning: Illegal string offset 'test' in /in/PEICX on line 9 accessing string offset test w val after access = 'west' val before access = 123 accessing integer as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set integer offset test to string wtf accessing integer offset test val after access = 123 val before access = true accessing boolean as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set boolean offset test to string wtf accessing boolean offset test val after access = true val before access = 1.2 accessing double as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set double offset test to string wtf accessing double offset test val after access = 1.2 val before access = (object) array( ) Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /in/PEICX:6 Stack trace: #0 /in/PEICX(18): test_val_as_array(Object(stdClass)) #1 {main} thrown in /in/PEICX on line 6
Process exited with code 255.
Output for 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33
val before access = NULL accessing NULL as array set NULL offset test to string wtf accessing NULL offset test wtf val after access = array ( 'test' => 'wtf', ) val before access = 'test' Warning: Illegal string offset 'test' in /in/PEICX on line 6 accessing string as array t Warning: Illegal string offset 'test' in /in/PEICX on line 7 set string offset test to string wtf Warning: Illegal string offset 'test' in /in/PEICX on line 9 accessing string offset test w val after access = 'west' val before access = 123 accessing integer as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set integer offset test to string wtf accessing integer offset test val after access = 123 val before access = true accessing boolean as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set boolean offset test to string wtf accessing boolean offset test val after access = true val before access = 1.2 accessing double as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set double offset test to string wtf accessing double offset test val after access = 1.2 val before access = stdClass::__set_state(array( )) Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /in/PEICX:6 Stack trace: #0 /in/PEICX(18): test_val_as_array(Object(stdClass)) #1 {main} thrown in /in/PEICX on line 6
Process exited with code 255.
Output for 7.0.0 - 7.0.6
val before access = NULL accessing NULL as array set NULL offset test to string wtf accessing NULL offset test wtf val after access = array ( 'test' => 'wtf', ) val before access = 'test' Warning: Illegal string offset 'test' in /in/PEICX on line 6 accessing string as array t Warning: Illegal string offset 'test' in /in/PEICX on line 7 set string offset test to string wtf Warning: Illegal string offset 'test' in /in/PEICX on line 9 accessing string offset test w val after access = 'west' val before access = 123 accessing integer as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set integer offset test to string wtf accessing integer offset test val after access = 123 val before access = true accessing boolean as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set boolean offset test to string wtf accessing boolean offset test val after access = true val before access = 1.19999999999999995559108 accessing double as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set double offset test to string wtf accessing double offset test val after access = 1.19999999999999995559108 val before access = stdClass::__set_state(array( )) Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /in/PEICX:6 Stack trace: #0 /in/PEICX(18): test_val_as_array(Object(stdClass)) #1 {main} thrown in /in/PEICX on line 6
Process exited with code 255.
Output for 5.4.1 - 5.4.21, 5.6.28
val before access = NULL accessing NULL as array set NULL offset test to string wtf accessing NULL offset test wtf val after access = array ( 'test' => 'wtf', ) val before access = 'test' Warning: Illegal string offset 'test' in /in/PEICX on line 6 accessing string as array t Warning: Illegal string offset 'test' in /in/PEICX on line 7 set string offset test to string wtf Warning: Illegal string offset 'test' in /in/PEICX on line 9 accessing string offset test w val after access = 'west' val before access = 123 accessing integer as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set integer offset test to string wtf accessing integer offset test val after access = 123 val before access = true accessing boolean as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set boolean offset test to string wtf accessing boolean offset test val after access = true val before access = 1.2 accessing double as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set double offset test to string wtf accessing double offset test val after access = 1.2 val before access = stdClass::__set_state(array( )) Fatal error: Cannot use object of type stdClass as array in /in/PEICX on line 6
Process exited with code 255.
Output for 5.4.22 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.21
val before access = NULL accessing NULL as array set NULL offset test to string wtf accessing NULL offset test wtf val after access = array ( 'test' => 'wtf', ) val before access = 'test' Warning: Illegal string offset 'test' in /in/PEICX on line 6 accessing string as array t Warning: Illegal string offset 'test' in /in/PEICX on line 7 set string offset test to string wtf Warning: Illegal string offset 'test' in /in/PEICX on line 9 accessing string offset test w val after access = 'west' val before access = 123 accessing integer as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set integer offset test to string wtf accessing integer offset test val after access = 123 val before access = true accessing boolean as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set boolean offset test to string wtf accessing boolean offset test val after access = true val before access = 1.19999999999999995559108 accessing double as array Warning: Cannot use a scalar value as an array in /in/PEICX on line 7 set double offset test to string wtf accessing double offset test val after access = 1.19999999999999995559108 val before access = stdClass::__set_state(array( )) Fatal error: Cannot use object of type stdClass as array in /in/PEICX on line 6
Process exited with code 255.

preferences:
208.12 ms | 403 KiB | 242 Q