#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Simple if statement not working


    Hello all,

    I've come across a very strange problem which I don't succeed to resolve. The line in red below is the one which I encounter the problem. The $payernumber equals the $partnumber (I'm almost 100% sure of it because when I echo it, it looks exactly the same in the screen), however according to the if-statement it is not equal. Anyone an idea how I could proceed?



    Code:
    foreach($xml->mainxml[0]->children() as $child1)
    {
    
    if($child1->getName() == "Activity") 
      {
    
    
        foreach($xml->mainxml[0]->children() as $child2)
        {
          if($child2->getName() == "Participant") 
            {
            $partnumber = $child2->attributes();
            foreach($child1->Payer as $t) {			
            $payernumber = $t->attributes();
            if ($payernumber != $partnumber) {echo ' <h4>payerID' . $payernumber . 'partID' .$partnumber . 'blabla</h4>';}
        }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    721
    Rep Power
    7
    So, by your description, the echo IS being posted...? Another note: You have more opening curly brackets in your posted code than closing. Missing a few?

    As far as if the 2 DO look identical, but your code disagrees, they may be of different types. I'm not the best to define this, but kinda like one being a string, and the other being something else. Just a thought.
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    Try casting them to strings:
    PHP Code:
    if ((string)$payernumber != (string)$partnumber
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Thumbs up


    Yes! This did the trick! I had spent an awful lot of time (not) figuring this out :-)

    The reason I thought it couldn't be the data types was what I read on w3schools:
    - "==": simply looks if true
    - "===": looks if true and if the same data type

    ==>Guess that's not true ;-)

    Anyway, very glad it's solved. Thanks a lot!


    "Operator Name Description Example
    x == y Equal True if x is equal to y 5==8 returns false
    x === y Identical True if x is equal to y, and they are of same type 5==="5" returns false"
  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    ==>Guess that's not true ;-)
    Actually it is true.

    However, $payernumber and $partnumber are both objects, and they are not the same object, so they are not equal. Most of the XML handling functions in PHP return objects rather than primitives, which is fairly non-standard as far as PHP functions go. However, they implement automatic methods for converting them to strings, which is normally what you want to do when comparing them.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo