views:

145

answers:

2

When I run this code, About half-way through the concatenation loop, $xml becomes null and remains null throughout the rest of the concatenation loop. Has anyone else ran into this? Or see why this is happening?

$xml = '';
foreach($this->currentColumns['unknown'] as $column => $value)
{
   $xml .= "<columnName>";
   $xml .= $column;
   $xml .= "</columnName>\r\n";
}
return $xml;

Thanks in advance for your help.

John

A: 

You're going to have to print out your $column values as you go. If you're getting a very unexpected column name, you might have to test on that condition before creating the xml string for it.

memnoch_proxy
If I replace what is in the loop with just $xml .= $column;Then it works. Sorry, should have mentioned that. But as soon as I add back in the first or the last line of the loop, then it has the problem described above.
postalservice14
Well in this case, try putting all what's in the loop on a single line like this: $xml .= "<columnName>". strval($column) ."</columnName>\r\n";
JP
strval() is a good choice, thanks for pointing that out. I've seen that used before.
memnoch_proxy
+3  A: 

In case $this->currentColumns is some kind of result of an XML parsing (with SimpleXML for instance), it's very possible that the elements of this array are not really strings, but XMLElement objects, or something close enough.

Try casting your variable, so you're sure you're catenating strings and not objects :

$xml = '';
foreach($this->currentColumns['unknown'] as $column => $value)
{
   $xml .= "<columnName>";
   $xml .= (string)$column;  // <--- here is the trick
   $xml .= "</columnName>\r\n";
}
return $xml;
Nicolas
Thanks Nicolas, currentColumns is an array I build at a higher level in the code. What this function is doing is part of an xml string that I am creating and will be posting to an API. But for the heck of it, I added the string casting and it did not fix it. This is really weird, I'm beginning to think PHP bug here. I am using PHP 5.3
postalservice14