Skip to content

Instantly share code, notes, and snippets.

@y-uti
Last active April 22, 2016 10:12
Show Gist options
  • Save y-uti/e9a686c08d7abe8b97c3e4dc63099bf5 to your computer and use it in GitHub Desktop.
Save y-uti/e9a686c08d7abe8b97c3e4dc63099bf5 to your computer and use it in GitHub Desktop.
<?php
function test($init, $key = 'a')
{
$var = $init;
$var[$key] = 1;
echo var_export($init, true), ': ', is_array($var) ? 1 : 0, "\n";
}
test(null); // OK
test(false); // OK
test(true); // PHP Warning: Cannot use a scalar value as an array in ...
test(0); // PHP Warning: Cannot use a scalar value as an array in ...
test(1); // PHP Warning: Cannot use a scalar value as an array in ...
test(''); // OK
test('abc'); // PHP Warning: Illegal string offset 'a' in ...
test(new stdClass()); // PHP Fatal error: Uncaught Error: Cannot use object of type stdClass as array in ...
@y-uti
Copy link
Author

y-uti commented Apr 22, 2016

配列ではない変数に添字を指定して代入したときの挙動

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment