Jump to content

IF statement not working


Dummy99
Go to solution Solved by mac_gyver,

Recommended Posts

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.

 

Link to comment
Share on other sites

  • Solution
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 ===

  • Like 1
Link to comment
Share on other sites

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>");
} 

 

  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.