php.net |  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
Votes:1
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
Description:
------------
Doing the following SimpleXML text comparison does not give the expected results.

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

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

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

Expected result:
----------------
123
They are equal.

Actual result:
--------------
123
They are not equal.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-02-27 02:16 UTC] sniper@php.net
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) {
  [0]=>
  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 http://docs.php.net/en/ref.simplexml.html 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] derick@php.net
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] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

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 http://docs.php.net/en/ref.simplexml.html says that what I'm trying to do should work.  That example then is invalid too.
 [2004-03-07 10:15 UTC] helly@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip
 [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] sniper@php.net
Either the docs are outdated or there is some bug here.
(is casting to string really necessary?)
 [2004-03-10 00:34 UTC] amt@php.net
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] amt@php.net
This now works as of latest CVS (and probably also RC1).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 06:01:29 2024 UTC