0

I have encountered a very weird and concerning problem in some of my PHP code. A variable that I have returns true in an IF statement when it clearly should return false.

$pr = $_SESSION['fin_print_printer']; //this should equal 0     
print $pr; //this returns 0, as it should
if($pr == "L"){
   print "local";
} else {
       print "serve";
} 
print $pr; //this returns 0 again, as it should

This prints "local" in my script (in between the two zeros) and does not print "serve". With over 100,000 lines of code in my project, I've not experienced this issue yet, and now I can't figure out what is going on.

If I do if($pr === "L"), then it works as expected, but the above does not.

10
  • Show a sample output, if $pr IS 0 as you state, it's logically impossible for 0 to == "L" Commented Aug 31, 2013 at 1:02
  • Use triple equals there if ($pr === "L") Commented Aug 31, 2013 at 1:02
  • == or === shouldn't matter in this case Commented Aug 31, 2013 at 1:03
  • As a last resort, my CStatementTracer found here might help: stackoverflow.com/questions/10574614/… Commented Aug 31, 2013 at 1:03
  • 1
    $pr is an integer variable. If you compare with == PHP will attempt to cast "L" to an integer, obtaining a value of 0, which is equal to the value of $pr. If you compare with === the test will fail because the types differ. Commented Aug 31, 2013 at 1:04

3 Answers 3

1

PHP is trying to typecast 'L' into an int, which results in 0.

intval('L'); // 0

Change your code into the following so it will take types into account:

if($pr === "L")
{
    print "local";
} 
else 
{
    print "serve";
} 

Or manually typecast $pr to a string.

// You can also to (string)$pr ("0" instead of 0)
if(strval($pr) == "L")
{
    print "local";
} 
else 
{
    print "serve";
} 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! As another user commented, it is logically impossible for L to equal 0, that's why I found this so mind-boggling. But thank you for the help. Using strval() works great (as with === as was expected).
@KyleLawrence You can also use (string)$pr to typecast it. Just as an additional note. About the L being 0, it's just the PHP Interpreter trying to compare L to an integer because $pr in an int.
0

Maybe if you use typecasting (I did't check it):

if ( (string)$pr == "L" ) {
   print "local";
} else {
       print "serve";
}

1 Comment

It works. 3v4l.org/G6Rg5 Though I might say, using === would be easier.
0

Little known approach: You can also do the casting like

if ("L" == $pr) {

Because with loose comparisons, PHP casts the right value to the left value's type, and as you've already been made aware, while string(1)"L" is casted to int(0), int(0) is casted to string(1)"0".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.