467,075 Members | 1,070 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,075 developers. It's quick & easy.

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($prefix . '.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($xmlFileName) { // 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
Jul 17 '05 #1
  • viewed: 1634
Share:
2 Replies
Phil Powell wrote:
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($prefix . '.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():

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

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

when $prefix='state' is the same as

${$prefix . 'XML'} = $this->getXML($prefix . '.xml');
${'state' . 'XML'} = $this->getXML('state' . '.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
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
Pedro Graca <he****@hotpop.com> wrote in message news:<bu************@ID-203069.news.uni-berlin.de>...
Phil Powell wrote:
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($prefix . '.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():

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

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

when $prefix='state' is the same as

${$prefix . 'XML'} = $this->getXML($prefix . '.xml');
${'state' . 'XML'} = $this->getXML('state' . '.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

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
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by William Payne | last post: by
reply views Thread by Pierson C | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.