Unusual echo bug? What am I screwing up?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jcbrand1
Forum Newbie
Posts: 2
Joined: Wed Jul 26, 2006 8:44 am

Unusual echo bug? What am I screwing up?

Post 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
klarinetking
Forum Commoner
Posts: 59
Joined: Mon Jul 24, 2006 9:43 am

Post 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
Last edited by klarinetking on Wed Jul 26, 2006 9:37 am, edited 1 time in total.
jcbrand1
Forum Newbie
Posts: 2
Joined: Wed Jul 26, 2006 8:44 am

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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
Post Reply