PHP variable "supernaturally set" - how is this possible???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    PHP variable "supernaturally set" - how is this possible???

    print_r("From index.php 20: $prefix: stateXML = $stateXML<P>");
    foreach (array('state', 'country') as $prefix) {
    print_r("From index.php 21: stateXML = $stateXML<P>");
    ${$prefix . 'XML'} = $this->getXML($pref ix . '.xml');
    }


    I have never seen anything like this in my 3+ years of PHP coding!
    When I ran this code, I get the following output:



    From index.php 20: : stateXML =

    From index.php 21: stateXML = state


    How on earth did $stateXML get set, there is literally NOTHING that
    sets it!!! Here is the method getXML():



    function getXML($xmlFile Name) { // XML STRING METHOD
    global $DOCUMENT_ROOT, $devpath, $basePath;

    $path = "$DOCUMENT_ROOT $devpath/image_catalog";

    if (!$xmlFileName || !file_exists("$ path/xml/$xmlFileName"))
    die("$path/xml/$xmlFileName does not exist");
    $fileID = @fopen("$path/xml/$xmlFileName", 'r') or
    die("$path/xml/$xmlFileName could not be found");
    if (!$fileID) return '';
    $xmlStuff = fread($fileID, filesize("$path/xml/$xmlFileName")) ;
    fclose($fileID) ;
    return $xmlStuff;
    }


    Again, how is $stateXML existing when there is clearly NO reason for
    it to ever exist??

    Help!
    Phil
  • Pedro Graca

    #2
    Re: PHP variable &quot;supernatu rally set&quot; - how is this possible???

    Phil Powell wrote:[color=blue]
    > print_r("From index.php 20: $prefix: stateXML = $stateXML<P>");
    > foreach (array('state', 'country') as $prefix) {
    > print_r("From index.php 21: stateXML = $stateXML<P>");
    > ${$prefix . 'XML'} = $this->getXML($pref ix . '.xml');
    > }
    >
    >
    > I have never seen anything like this in my 3+ years of PHP coding!
    >
    > When I ran this code, I get the following output:
    >
    >
    >
    > From index.php 20: : stateXML =
    >
    > From index.php 21: stateXML = state
    >
    >
    > How on earth did $stateXML get set, there is literally NOTHING that
    > sets it!!! Here is the method getXML():[/color]
    (snip)


    Strange coding, in fact! :)
    [ Why print_r() where a simple echo (or print()) would do? ]


    This line

    ${$prefix . 'XML'} = $this->getXML($pref ix . '.xml');

    when $prefix='state' is the same as

    ${$prefix . 'XML'} = $this->getXML($pref ix . '.xml');
    ${'state' . 'XML'} = $this->getXML('stat e' . '.xml');
    ${'stateXML'} = $this->getXML('state. xml');

    $stateXML = $this->getXML('state. xml');


    but $stateXML doesn't get set *before* it is printed !!!

    The first time through the loop it will print a blank $stateXML, and
    _after_ that set it to the contents of ".../xml/state.xml"; the second
    time through the loop it will print that data and set $countryXML to the
    contents of the file ".../xml/country.xml".


    read about 'variable variables' @

    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Phil Powell

      #3
      Re: PHP variable &quot;supernatu rally set&quot; - how is this possible???

      Pedro Graca <hexkid@hotpop. com> wrote in message news:<bu76t0$ep j27$1@ID-203069.news.uni-berlin.de>...[color=blue]
      > Phil Powell wrote:[color=green]
      > > print_r("From index.php 20: $prefix: stateXML = $stateXML<P>");
      > > foreach (array('state', 'country') as $prefix) {
      > > print_r("From index.php 21: stateXML = $stateXML<P>");
      > > ${$prefix . 'XML'} = $this->getXML($pref ix . '.xml');
      > > }
      > >
      > >
      > > I have never seen anything like this in my 3+ years of PHP coding!
      > >
      > > When I ran this code, I get the following output:
      > >
      > >
      > >
      > > From index.php 20: : stateXML =
      > >
      > > From index.php 21: stateXML = state
      > >
      > >
      > > How on earth did $stateXML get set, there is literally NOTHING that
      > > sets it!!! Here is the method getXML():[/color]
      > (snip)
      >
      >
      > Strange coding, in fact! :)
      > [ Why print_r() where a simple echo (or print()) would do? ]
      >
      >
      > This line
      >
      > ${$prefix . 'XML'} = $this->getXML($pref ix . '.xml');
      >
      > when $prefix='state' is the same as
      >
      > ${$prefix . 'XML'} = $this->getXML($pref ix . '.xml');
      > ${'state' . 'XML'} = $this->getXML('stat e' . '.xml');
      > ${'stateXML'} = $this->getXML('state. xml');
      >
      > $stateXML = $this->getXML('state. xml');
      >
      >
      > but $stateXML doesn't get set *before* it is printed !!!
      >
      > The first time through the loop it will print a blank $stateXML, and
      > _after_ that set it to the contents of ".../xml/state.xml"; the second
      > time through the loop it will print that data and set $countryXML to the
      > contents of the file ".../xml/country.xml".
      >
      >
      > read about 'variable variables' @
      > http://www.php.net/manual/en/languag...s.variable.php[/color]


      Thanx, however, it still occurs. I traced it to literally occur the
      moment I go into the foreach loop *BEFORE* it ever goes to
      $this->getXML(). Somehow, for reasons I cannot explain, I found out
      that not only is $stateXML existing with the value of 'state', but so
      is $state, $xml, $XML, $State, $a, $b, $c, $x, $y, $z, $... literally
      every existing combination of valid letters that form a variable all
      suddenly not only exist but have a value of 'state'!

      Phil

      Comment

      Working...