1
<?php
$st="Success";
if(($st=="SUCCESS" || $st=="Success" || $st=="success") && $st!=0 && $st!="0" && $st!="FAILURE" && $st!="Queued" && $st!="Pending" && $st!="queued" && $st!="pending" && $st!="QUEUED" && $st!="PENDING" )
{
    echo 'success';
}else
if(($st=="FAILURE" || $st=="failure" || $st=="0" || $st==0 || $st=="Failure") && $st!="Queued" && $st!="Pending" && $st!="queued" && $st!="pending" && $st!="QUEUED" && $st!="PENDING" && $st!="SUCCESS" && $st!="Success" && $st!="success" )
{
    echo 'failed';
}else
{
    echo 'nill';
}
?>

Why does my code print nill when I pass Success value in $st ? How does && , || priority work in if-else statement ?

Rifky Niyas
  • 1,737
  • 10
  • 25
Adern
  • 37
  • 6
  • 1
    [Unable to replicate the problem.](https://paiza.io/projects/hZVCI-yzYtahBT41iin8PQ) – David Dec 29 '21 at 13:45
  • 1
    You're adding a bunch of unnecessary checks. If `$st` is any form of success, then it will never be any form of 0 or anything else. Same goes for your else/if check. `$st=="0" || $st==0` is also exactly the same since you're doing a soft-comparison and not a type comparison (which would be a `===` comparison) – aynber Dec 29 '21 at 13:45
  • sometimes i receives 0 and sometimes i receive value in $st as failure or pending, thats why i have used those checks. But though i cant find an error in my code it still shows me failure when i pass value "Success", though when it should print success. – Adern Dec 29 '21 at 13:47
  • 1
    Then add those as separate lines. Check for what it is, there is no need to check for what it is not. – aynber Dec 29 '21 at 13:48
  • @David , Sir i am learning in w3 schools php editor i tried there copied same code, there i see output 'nill', and in localhost it print 'nill' – Adern Dec 29 '21 at 13:48
  • @David , i copied same code in 2 different online php editors it produces different results, in w3schools editor it says 'nill' , i link provided by david sir it says 'success', though code is same. how ? – Adern Dec 29 '21 at 13:51
  • 3
    It works in PHP 8.0+ https://3v4l.org/IGBL3 When PHP <8.0 is casting "Success" to integer it turns into 0 so 0!=0 is false. https://stackoverflow.com/a/672051/562359 – Hendrik Dec 29 '21 at 13:54

1 Answers1

2

As I mentioned, you should be checking for what it is, not what it isn't, since you're only checking one variable. If $st is success it can't be anything else. Same goes for failure/0. You can also eliminate a few checks by converting the variable to lowercase.

if(strtolower($st) == 'success') { // Convert to lowercase and check against "success"
    echo 'Success';
}
else if(strtolower($st) == 'Failure' || (is_integer($st) && $st == 0)) { // convert to lowercase and check if it's "failure" or it's a number and 0
    echo 'Failed';
}
else {
    echo 'nill';
}

If you know you're passing in success, you can add a var_dump($st); inside of your else check to see what it contains. Make sure to count the characters, as "success " is not the same as "success". You can also add a trim($st); before your if checks to remove any extra whitespace from the ends of the value .

aynber
  • 22,380
  • 8
  • 50
  • 63
  • converting string to lowercase is a smart solution, dumb of me why didn't i thought of it sooner. Also strtolower() produces a string hence i dont need to worry for When PHP <8.0 is casting "Success" to integer it turns into 0 so 0!=0 is false. as mentioned by @Hendrik in comments. – Adern Dec 29 '21 at 14:06
  • Also sir i found out != compares just values, and !== compares value and data type, hence when i used !== it runs and produces result as expected. But though i liked your solution as it is very smart to convert to lower case. Thank you sir. – Adern Dec 29 '21 at 14:08
  • That, and this is eliminating a bunch of checks. Your code is hitting that conversion because you're checking for `$st != 0` on the success line when you don't need to. **That** is why you're getting nill in your code. If you're only checking for what the variable is instead of what it isn't, it removes a lot of headaches. – aynber Dec 29 '21 at 14:09
  • sir if we pass ```$st="Pending"``` it prints "Failed" – Adern Dec 29 '21 at 14:31
  • Huh, you are right, it should be nill. That comes back to the conversion, so I'm going to update the answer to fix that. It will check for the lowercase string failure, and if $st is a number and 0. – aynber Dec 29 '21 at 14:36