I have if statement with multiple condition,
what is the differece between this two conditon:
1.
if($province=="AB" || "NT" || "NU" || "YT")
{
$GST=5;
}
else if($province=="BC" || "MB")
{
$GST=5;
$PST=7;
}
else if($province=="NB" || "NF" || "ON")
{
$HST=13;
}
and second is:
2.
if($province=="AB" || $province=="NT" || $province=="NU" || $province=="YT")
{
$GST=5;
}
else if($province=="BC" || $province=="MB")
{
$GST=5;
$PST=7;
}
else if($province=="NB" || $province=="NF" || $province=="ON")
{
$HST=13;
}
The difference between the two is that the first one won’t work as expected, and the second is technically correct.
The code:
will always evaluate to true and execute the code in that conditional block.
The reason is because you are only checking if
$province == "AB"and then you are checking if"NT" == truewhich will evaluate to true.To check province against all of those values (AB, NT, NU, YT) you need to explicitly check
$provinceagainst each value, not just the first, which is what you are correctly doing in the second example.