Page 1 of 1

Unusual echo bug? What am I screwing up?

Posted: Wed Jul 26, 2006 9:10 am
by jcbrand1
I'm not sure if my problem is with SimpleXML (doubtful) or something goofy with the echo function but the following code snippet produces unusual results:

Code: Select all

<?php
        $xml = simplexml_load_file("test.xml");

        // Next line prints:
        //       Accepted Formats: ->format[0]
        echo "Accepted Formats: $xml->formats->format[0]<br>\n";
                                   
        $f0 = $xml->formats->format[0];

        // Both of these work fine.
        //       Accepted Formats: Image
        echo "Accepted Formats: $f0<br>\n";
        printf("Accepted Formats: %s<br>\n", $xml->formats->format[0]);
?>
the xml file is here in case you need it:

Code: Select all

<?xml version='1.0' encoding='iso-8859-1'?>
<example>
  <name>Example</name>
  <formats>
    <format ftype='1'>Image</format>
    <format ftype='2'>Text</format>
  </formats> 
</example>
The output:
Accepted Formats: ->format[0]
Accepted Formats: Image
Accepted Formats: Image

Any help would be greatly appreciated.

-Chris

Posted: Wed Jul 26, 2006 9:13 am
by klarinetking
Hi,

Double quotes doesn't work well with objects. Try this:

Code: Select all

echo 'Accepted formats: ' . $xml->formats->format[0] . '<br />';
klarinetking

Posted: Wed Jul 26, 2006 9:33 am
by jcbrand1
Hmm... that fixes it but it's a shame that doesn't work properly straight away. Can I assume that's a known bug? Hopefully they'll have it fixed in future releases of PHP.

Thank you for your help.

-Chris

Posted: Wed Jul 26, 2006 10:21 am
by onion2k
jcbrand1 wrote:Can I assume that's a known bug?
It's not a bug, so no you can't. The difference between single and double quoted strings is well documented.

http://www.zend.com/zend/tut/using-stri ... #variables