1

Can anyone explain why this:

if($xml->$ul !== ""){
echo $xml->$ul;
}
if($xml->$ul == ""){
echo "0";
}

does work, while

if($xml->$ul !== ""){
    echo $xml->$ul;
}else{
    echo "0";
}

does not work? Am i missing something?

Short explanation: if the xml contains $ul it will echo its value, if it is not contained it will echo 0. Works perfectly with first code, but second code just echos the values, the else is completely ignored.

I appreciate all answers!

ceriath
  • 13
  • 3
  • What do you mean by "it works"? With what values are you getting unexpected behaviour? – Shomz Jan 17 '16 at 03:44
  • thanks for the suggestions, the see the answer below, i just got confused with the difference between === and == (thought it was just some weird php syntax) – ceriath Jan 17 '16 at 03:54

1 Answers1

2

You are not doing the same equality check. In the first example you are first checking using !==, then in the second if you are using ==.

See this answer for an explanation of the difference between === equality and == equality. In short, === not only checks the values are equal, but also that the types of the variables being compared are the same as well.

Community
  • 1
  • 1
leeor
  • 17,041
  • 6
  • 34
  • 60
  • Ah thank you for the link! I did not know there is even a === check (this is my first time with php). Changing the if to `if ($xml->$ul != "")` made the version with else work :) – ceriath Jan 17 '16 at 03:47