php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66277 Unable to compare two strings that should be equal
Submitted: 2013-12-12 15:56 UTC Modified: 2013-12-12 19:13 UTC
From: wmcgee at driverpipeline dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.5.3 OS: Windows Server 2012 R2
Private report: No CVE-ID: None
 [2013-12-12 15:56 UTC] wmcgee at driverpipeline dot com
Description:
------------
Server: Windows Server 2012 R2
Web Server: IIS 8.5

I am having an issue comparing two strings inside an IF statement. When I use var_dump to print out both string values this is what I get:
string(16) "Exceptiontrace"
string(14) "Exceptiontrace"

As far as I can tell they both equal the same value but for some reason the top one is equal to string(16) instead of 14 as would be expected. The value is generated by calling get_defined_vars() after catching an exception.

NOTE: $x may need to be adjusted to target the Exceptiontrace object

Test script:
---------------
Source View: http://www.wil-tec.com/src/test.html

Actual Script: http://www.wil-tec.com/src/test.php

Expected result:
----------------
I expect php to execute either A or B, but not C

A. die("I should be only Value Printed"); Line: 36

B. die("Or If that doesnt work I should be."); Line: 37

C. die('But i actually am the last and key does  = "' . $key . '"'); Line: 46

Actual result:
--------------
php ends up executing C and the output looks like:

[START OUTPUT]
string(16) "Exceptiontrace"
string(14) "Exceptiontrace"
But i actually am the last and key does  = "Exceptiontrace"
[END OUTPUT]


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-12 16:02 UTC] wmcgee at driverpipeline dot com
-: mcgee dot will at gmail dot com +: wmcgee at driverpipeline dot com -PHP Version: 5.5.6 +PHP Version: 5.5.3
 [2013-12-12 16:02 UTC] wmcgee at driverpipeline dot com
Changed Email
And PHP version to 5.5.3
 [2013-12-12 18:49 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2013-12-12 18:49 UTC] requinix@php.net
When you treat objects as arrays, PHP mangles the names of protected and private members - it's not like you're supposed to have access to them anyways. Private members are mangled to "\0(class)\0(name)" and protected members to "\0*\0(name)".

Your code needs
  $test = "\0Exception\0trace";
to match correctly.

I don't know the true purpose of your script nor what all obj2xml() needs to expose in the XML but you should probably be using get_object_vars [http://www.php.net/get-object-vars] instead of treating objects as arrays (like you're doing with the while/list/each). Such as

  function obj2xml($v, $indent='') {
      if (is_object($v)) { $v = get_object_vars($v); }

However it won't expose to you members you shouldn't have access to; calling it from outside the class will only give you the public members, and Exception does not have any.
 [2013-12-12 19:07 UTC] wmcgee at driverpipeline dot com
The purpose of this page is to expose as much information related to any error that may occur in XML format to my C# windows service that is making the calls.

I made a C# windows service to make HttpWebRequests to my PHP server on a specified schedule based on certain OS/Hardware parameters for instance if a Hard Drive starts getting full it will call a script to remove older records etc. I wanted to find a way if an error occurs in my PHP script to get as much information about the error to my service so it can log it in the windows event log

but my the XML returned was showing that Exceptiontrace or really "\0Exception\0trace" was malformed. 

Thanks for your help with this, After doing a little research would just running $key = preg_replace( '/[^[:print:]]/', '',$key); work for removing all hidden characters? or would you recommend completely changing the way I've got it set up
 [2013-12-12 19:13 UTC] requinix@php.net
My first instinct would be to use special handling for specific classes, such as Exception, and get_object_vars() for the rest.

If you'd like more ideas or suggestions then check out a mailing list or forum.
http://www.php.net/support.php
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Aug 04 15:01:25 2020 UTC