3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function test_val_as_array($val) { $type = gettype($val); try { 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 "$type offset 'test' = {$val['test']}\n"; echo "val after access = ".var_export($val, true)."\n"; } catch (Exception $e) { echo "caught error {$e->getMessage()} when accessing $type as array\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);
based on PEICX
Output for 7.0.20 - 7.2.0
val before access = NULL accessing NULL as array set NULL offset 'test' to string 'wtf' NULL offset 'test' = wtf val after access = array ( 'test' => 'wtf', ) val before access = 'test' Warning: Illegal string offset 'test' in /in/A7gEa on line 7 accessing string as array t Warning: Illegal string offset 'test' in /in/A7gEa on line 8 set string offset 'test' to string 'wtf' Warning: Illegal string offset 'test' in /in/A7gEa on line 10 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/A7gEa on line 8 set integer offset 'test' to string 'wtf' 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/A7gEa on line 8 set boolean offset 'test' to string 'wtf' 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/A7gEa on line 8 set double offset 'test' to string 'wtf' 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/A7gEa:7 Stack trace: #0 /in/A7gEa(22): test_val_as_array(Object(stdClass)) #1 {main} thrown in /in/A7gEa on line 7
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' NULL offset 'test' = wtf val after access = array ( 'test' => 'wtf', ) val before access = 'test' Warning: Illegal string offset 'test' in /in/A7gEa on line 7 accessing string as array t Warning: Illegal string offset 'test' in /in/A7gEa on line 8 set string offset 'test' to string 'wtf' Warning: Illegal string offset 'test' in /in/A7gEa on line 10 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/A7gEa on line 8 set integer offset 'test' to string 'wtf' 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/A7gEa on line 8 set boolean offset 'test' to string 'wtf' 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/A7gEa on line 8 set double offset 'test' to string 'wtf' 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/A7gEa:7 Stack trace: #0 /in/A7gEa(22): test_val_as_array(Object(stdClass)) #1 {main} thrown in /in/A7gEa on line 7
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' NULL offset 'test' = wtf val after access = array ( 'test' => 'wtf', ) val before access = 'test' Warning: Illegal string offset 'test' in /in/A7gEa on line 7 accessing string as array t Warning: Illegal string offset 'test' in /in/A7gEa on line 8 set string offset 'test' to string 'wtf' Warning: Illegal string offset 'test' in /in/A7gEa on line 10 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/A7gEa on line 8 set integer offset 'test' to string 'wtf' 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/A7gEa on line 8 set boolean offset 'test' to string 'wtf' 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/A7gEa on line 8 set double offset 'test' to string 'wtf' 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/A7gEa on line 7
Process exited with code 255.
Output for 5.4.22 - 5.6.21
val before access = NULL accessing NULL as array set NULL offset 'test' to string 'wtf' NULL offset 'test' = wtf val after access = array ( 'test' => 'wtf', ) val before access = 'test' Warning: Illegal string offset 'test' in /in/A7gEa on line 7 accessing string as array t Warning: Illegal string offset 'test' in /in/A7gEa on line 8 set string offset 'test' to string 'wtf' Warning: Illegal string offset 'test' in /in/A7gEa on line 10 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/A7gEa on line 8 set integer offset 'test' to string 'wtf' 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/A7gEa on line 8 set boolean offset 'test' to string 'wtf' 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/A7gEa on line 8 set double offset 'test' to string 'wtf' 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/A7gEa on line 7
Process exited with code 255.
Output for 5.3.18 - 5.3.29
val before access = NULL accessing NULL as array set NULL offset 'test' to string 'wtf' NULL offset 'test' = wtf val after access = array ( 'test' => 'wtf', ) val before access = 'test' accessing string as array t set string offset 'test' to string 'wtf' 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/A7gEa on line 8 set integer offset 'test' to string 'wtf' 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/A7gEa on line 8 set boolean offset 'test' to string 'wtf' 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/A7gEa on line 8 set double offset 'test' to string 'wtf' 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/A7gEa on line 7
Process exited with code 255.