title: PHP autovivification
body: <p><strong>Update: My original intention for this question was to determine if PHP actually has this feature. This has been lost in the answers' focus on the scalar issue. Please see this new question instead: <a href="https://stackoverflow.com/questions/5964420/does-php-have-autovivification">"Does PHP have autovivification?"</a> This question is left here for reference.</strong></p>
  
  <p><a href="http://en.wikipedia.org/wiki/Autovivification" rel="nofollow noreferrer">According to Wikipedia</a>, PHP doesn't have autovivification, yet this code works:</p>
  
  <pre><code>$test['a']['b'] = 1;
  $test['a']['c'] = 1;
  $test['b']['b'] = 1;
  $test['b']['c'] = 1;
  
  var_dump($test);
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>array
    'a' =&gt; 
      array
        'b' =&gt; int 1
        'c' =&gt; int 1
    'b' =&gt; 
      array
        'b' =&gt; int 1
        'c' =&gt; int 1
  </code></pre>
  
  <p>I found that this code works too:</p>
  
  <pre><code>$test['a'][4] = 1;
  $test['b'][4]['f'] = 3;
  </code></pre>
  
  <p>But adding this line causes an warning ("Warning: Cannot use a scalar value as an array")</p>
  
  <pre><code>$test['a'][4]['f'] = 3;
  </code></pre>
  
  <p>What's going on here? Why does it fail when I add the associative element after the index? Is this 'true' Perl-like autovivification, or some variation of it, or something else?</p>
  
  <p>Edit: oh, I see the error with the scalar now, oops! These work as expected:</p>
  
  <pre><code>$test['a'][4]['a'] = 1;
  $test['a'][4]['b'] = 2;
  $test['a'][5]['c'] = 3;
  $test['a'][8]['d'] = 4;
  </code></pre>
  
  <p>So, php does have autovivification? Searching Google for "php autovivification" doesn't bring up a canonical answer or example of it.</p>