ryoakg
5/27/2016 - 11:44 AM

expand-variable-into-string.php

<?php
$a = [1,2,3];
"$a";                           // Notice: Array to string conversion
"${a}";                         // Notice: Array to string conversion
"${a[0]} - ${a[1]} - ${a[2]}"; // '1 - 2 - 3'
"{$a[0]} - {$a[1]} - {$a[2]}"; // '1 - 2 - 3'

$h = ['a'=>1, 'b'=>2, 'c'=>3];
"${h['a']} - ${h['b']} - ${h['c']}"; // '1 - 2 - 3'
"{$h['a']} - {$h['b']} - {$h['c']}"; // '1 - 2 - 3'

$str = 'abc';
"${strtolower('STR')}";         // 'abc'
"{${strtolower('STR')}}";       // 'abc'
"strtolower('STR')";            // 'strtolower(\'STR\')'

$o = (object) ['a' => 'v1',
               'b' => (object)['a' => 'v2',
                               'b' => 'v3']];
"{$o}";                         // Catchable fatal error: Object of class stdClass could not be converted to string
"{$o->{'a'}}";                  // 'v1'
"{$o->{'b'}->{'a'}}";           // 'v2'
"{$o->{'b'}->{'b'}}";           // 'v3'