3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $tpnb = 5; test(); die(); function test() { global $tpnb; error_log('$tpnb value: ' . $tpnb . ' type:' . gettype($tpnb)); $xmlStruct = <<<EOF <?xml version="1.0" encoding="UTF-8"?> <templates> <object type="obj_1" label="Label for object 1"></object> <object type="obj_2" label="Label for object 2"></object> <object type="obj_3" label="Label for object 3"></object> <object type="obj_4" label="Label for object 4"></object> <object type="obj_5" label="Label for object 5"></object> <object type="obj_6" label="Label for object 6"></object> <object type="obj_7" label="Label for object 7"></object> <object type="obj_8" label="Label for object 8"></object> <object type="obj_9" label="Label for object 9"></object> <object type="obj_10" label="Label for object 10"></object> </templates> EOF; $tplxml = simplexml_load_string($xmlStruct); echo '<table border="1">'; echo '<tr>'; echo'<td style="vertical-align:top"><pre><h1>1: $tpnb</h1>'; // try accessing "object" directly print_r($tplxml->object[$tpnb]); echo '</pre></td>'; echo'<td style="vertical-align:top"><pre><h1>2: (int) $tpnb</h1>'; // try accessing "object" with an index cast print_r($tplxml->object[(int) $tpnb]); echo '</pre></td>'; echo'<td style="vertical-align:top"><pre><h1>3: ($tpnb + 1 - 1)</h1>'; // try accessing "object" by increasing and decreasing the index print_r($tplxml->object[($tpnb + 1 - 1)]); echo '</pre></td>'; echo'<td style="vertical-align:top"><pre><h1>4: temp var</h1>'; // use a temporary variable to re-set the index in the same variable $tpnbx = $tpnb; unset($tpnb); $tpnb = $tpnbx; print_r($tplxml->object[$tpnb]); echo '</pre></td>'; echo '</tr></table>'; }
Output for 5.6.0 - 5.6.23, 7.0.20 - 7.3.0rc4
<table border="1"><tr><td style="vertical-align:top"><pre><h1>1: $tpnb</h1>SimpleXMLElement Object ( [@attributes] => Array ( [type] => obj_6 [label] => Label for object 6 ) ) </pre></td><td style="vertical-align:top"><pre><h1>2: (int) $tpnb</h1>SimpleXMLElement Object ( [@attributes] => Array ( [type] => obj_6 [label] => Label for object 6 ) ) </pre></td><td style="vertical-align:top"><pre><h1>3: ($tpnb + 1 - 1)</h1>SimpleXMLElement Object ( [@attributes] => Array ( [type] => obj_6 [label] => Label for object 6 ) ) </pre></td><td style="vertical-align:top"><pre><h1>4: temp var</h1>SimpleXMLElement Object ( [@attributes] => Array ( [type] => obj_6 [label] => Label for object 6 ) ) </pre></td></tr></table>$tpnb value: 5 type:integer
Output for 7.0.0 - 7.0.9
<table border="1"><tr><td style="vertical-align:top"><pre><h1>1: $tpnb</h1></pre></td><td style="vertical-align:top"><pre><h1>2: (int) $tpnb</h1>SimpleXMLElement Object ( [@attributes] => Array ( [type] => obj_6 [label] => Label for object 6 ) ) </pre></td><td style="vertical-align:top"><pre><h1>3: ($tpnb + 1 - 1)</h1>SimpleXMLElement Object ( [@attributes] => Array ( [type] => obj_6 [label] => Label for object 6 ) ) </pre></td><td style="vertical-align:top"><pre><h1>4: temp var</h1>SimpleXMLElement Object ( [@attributes] => Array ( [type] => obj_6 [label] => Label for object 6 ) ) </pre></td></tr></table>$tpnb value: 5 type:integer