Dummy99 Posted July 8, 2023 Share Posted July 8, 2023 My code: if($sw=='B') {print("sw=$sw<br>"); print("<font color=maroon>Buy $amt <font color=red>$sw<br>");} Results: sw=0 Buy $0 0 Neither of these result lines should print. Searched and searched and tested and tested but can't see any error in code. Thanks for you help. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 8, 2023 Share Posted July 8, 2023 It works as expected when I run the code Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted July 8, 2023 Solution Share Posted July 8, 2023 Quote Warning Prior to PHP 8.0.0, if a string is compared to a number or a numeric string then the string was converted to a number before performing the comparison. as of php8 - Quote Non-strict comparisons between numbers and non-numeric strings now work by casting the number to string and comparing the strings. either update to php8+ or use a strict comparison === 1 Quote Link to comment Share on other sites More sharing options...
kicken Posted July 8, 2023 Share Posted July 8, 2023 You're probably running a version of PHP before 8.0 and suffering from it's poor handling of comparing integers and strings. This was changed in PHP 8.0 to something that makes more sense. Compare the output if you run the code on various versions of PHP: https://3v4l.org/vLB4b The simple fix is to perform a strict comparison using the === operator instead. if($sw === 'B'){ print("sw=$sw<br>"); print("<font color=maroon>Buy $amt <font color=red>$sw<br>"); } 1 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted July 8, 2023 Share Posted July 8, 2023 Since you are not happy how about telling us what you have set the value of $sw to? Quote Link to comment Share on other sites More sharing options...
Dummy99 Posted July 8, 2023 Author Share Posted July 8, 2023 That's what is expected but it doesn't work on my laptop nor on my phone - screen shot below. Any idea why? I doubt it but could it be my web service provider? Quote Link to comment Share on other sites More sharing options...
Dummy99 Posted July 8, 2023 Author Share Posted July 8, 2023 Many thanks to all and in particular to Mac_Gyver who hit it right on. I change my == to === and everything works fine. But I must say it is very peculiar because all of my if's wit == were working fine except this particular one and I'm pretty sure I had similar situations. For the one giving trouble, I had set the $sw='' (which I thought was a blank or nothing) but when I printed it, it was a numeric 0 (which obviously gave me the trouble when I was checking for a 'B"). However, I had at least one other situation where I set $WL='' and when testing for a W, if($WL=='W') ,I was getting correct results (or so it seems - maybe the results were incorrect and I didn't notice it - that's the only explanation I have.) Again, many thanks. Quote Link to comment Share on other sites More sharing options...
Dummy99 Posted July 8, 2023 Author Share Posted July 8, 2023 Sorry. Special thanks to kicken who said to use === instead of ==, are also in order. Quote Link to comment Share on other sites More sharing options...
kicken Posted July 8, 2023 Share Posted July 8, 2023 37 minutes ago, Dummy99 said: However, I had at least one other situation where I set $WL='' and when testing for a W, if($WL=='W') ,I was getting correct results The empty string ('') will compare correctly using ==, because it's a string. This issue is specifically when you are testing a numeric value against a string. Somewhere in your code $sw is getting set to the numeric value 0 and not the empty string as you thought and that's what lead to the problem. In general, using strict comparison is a good habit to develop to save help ensure you're dealing with the correct types. 1 Quote Link to comment Share on other sites More sharing options...
Dummy99 Posted July 8, 2023 Author Share Posted July 8, 2023 You are absolutely correct. In my initialization of $sw in a start-up file, I was inadvertently setting $sw=0 and of the many times I checked, I missed it. As you said, with $sw='' it would have worked. I thought I had initialized with $sw='' and I therefore couldn't understand why it was not working. After you last statement, I checked again and corrected it. Thank you so much. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.