php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72588 Using global var doesn't work while accessing SimpleXML element
Submitted: 2016-07-12 18:18 UTC Modified: 2016-07-12 18:59 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: tdrpic at atdi dot us dot com Assigned:
Status: Closed Package: Variables related
PHP Version: 7.0.8 OS: Windows
Private report: No CVE-ID:
 [2016-07-12 18:18 UTC] tdrpic at atdi dot us dot com
Description:
------------
When making a variable global inside a function and using it as an index to access a SimpleXML element, the variable's value is non-properly handled and the XML's element is not returned (although you can error_log the variable's value and type).

If you cast the variable to an integer, the XML's element is returned (as expected) - see column #2.

Compare the results of the four different tests in the sample script.

Also:
- if you try the test removing the function call (to avoid declaring the variable as global), the expected element is returned.

- using $GLOBALS also works returning the expected element.

- the output of the code is different between PHP 5.4.16 and PHP 7.0.8

Test script:
---------------
<?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>';
}


Expected result:
----------------
All the columns should return:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => obj_6
            [label] => Label for object 6
        )

)

Actual result:
--------------
Column one is empty.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-12 18:59 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2016-07-12 18:59 UTC] cmb@php.net
Confirmed, see <https://3v4l.org/OcRGM>, or the more readable
<https://3v4l.org/IiZvt>. Apparently, the regression happened with
PHP 7.0.0.
 [2016-07-13 02:51 UTC] laruence@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=8129b839922c14e1a4b3a82c702faa178e1cdec3
Log: Fixed bug #72588 (Using global var doesn't work while accessing SimpleXML element)
 [2016-07-13 02:51 UTC] laruence@php.net
-Status: Verified +Status: Closed
 [2016-07-20 11:30 UTC] davey@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=8129b839922c14e1a4b3a82c702faa178e1cdec3
Log: Fixed bug #72588 (Using global var doesn't work while accessing SimpleXML element)
 [2016-10-17 10:11 UTC] bwoebi@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=8129b839922c14e1a4b3a82c702faa178e1cdec3
Log: Fixed bug #72588 (Using global var doesn't work while accessing SimpleXML element)
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Mon Feb 20 22:01:35 2017 UTC