|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27413 SimpleXML text comparison
Submitted: 2004-02-27 00:11 UTC Modified: 2004-03-26 18:13 UTC
Avg. Score:5.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: rbro at hotmail dot com Assigned: sterling (profile)
Status: Closed Package: SimpleXML related
PHP Version: 5CVS-2004-03-07 OS: *
Private report: No CVE-ID: None
 [2004-02-27 00:11 UTC] rbro at hotmail dot com
Doing the following SimpleXML text comparison does not give the expected results.

Reproduce code:
$sxe = simplexml_load_string('<root a="123" />');

echo $sxe['a']."\n";

if ($sxe['a'] == '123')
	echo 'They are equal.'."\n";
	echo 'They are not equal.'."\n";

Expected result:
They are equal.

Actual result:
They are not equal.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-02-27 02:16 UTC]
Try doing a 'var_dump($sxe['a']);'...comparing string to an object can't really be true in any case?

 [2004-02-27 09:00 UTC] rbro at hotmail dot com
Yes, doing 'var_dump($sxe['a']);' gives:

object(simplexml_element)#2 (1) {
  string(3) "123"

But then changing my if line to:

if ($sxe['a'][0] == '123')

doesn't work either.

How does one then compare an attribute to another value? The documentation at states that my original if statement should work (the example under 'Using attributes').  That example doesn't work locally either because of the same issue on the switch statement used.
 [2004-02-27 09:04 UTC]
If you cast to a string, it works:

if ((string) $sxe['a'] == '123')

I do think that should be necessary though.
 [2004-02-27 13:34 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

As Derick said, the explicit conversion is necessary because php tries to convert the second argument to the first argument\'s type which is an object. Generally it could work when you switch the order of arguments but the explicit conversion makes it visible what you do.
 [2004-02-27 13:51 UTC] rbro at hotmail dot com
Thanks for your reply, but if that's the intended behavior (that echo'ing the attribute prints it out as a string, but trying to compare it doesn't work without a cast to a string), then the documentation then needs to be updated.  The 'Using attributes' example on says that what I'm trying to do should work.  That example then is invalid too.
 [2004-03-07 10:15 UTC]
Please try using this CVS snapshot:
For Windows:
 [2004-03-07 12:17 UTC] rbro at hotmail dot com
I just tried running my original example from my initial bug report, but I got the same results.  I got the "They are not equal." line.  I'm running on php5-200403071630.
 [2004-03-09 19:22 UTC]
Either the docs are outdated or there is some bug here.
(is casting to string really necessary?)
 [2004-03-10 00:34 UTC]
The docs are incorrect. Right now you really need to 
cast to a string. I thought I remember reading that 
Sterling and Zeev (?) may have worked out a solution on 
the php|cruise. However, I have yet to see anything 
checked in. Hopefully, this will be fixed to eliminate 
the need for a cast.
 [2004-03-26 18:13 UTC]
This now works as of latest CVS (and probably also RC1).
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 30 11:01:31 2024 UTC