I have a question about IF statements with multiple logical OR operators.
If we let:
$x=1;
A. I typical would write a IF statement comparing two items like this:
if($x == 1 || $x == 2) echo 'good';
else echo 'bad';
B. But, is this a valid IF statement? If not, why? (because it seems to work)
if($x == (1 || 2)) echo 'good';
else echo 'bad';
C. I typical would write a third comparison like this:
if($x == 1 || $x == 2 || $x == 3) echo 'good';
else echo 'bad';
D. What about this, following suit with B, above? (it does not seem to work)
if($x == (1 || 2 || 3)) echo 'good';
else echo 'bad';
The example in B, above works, but not the example in D. Why?
I cannot find any PHP documentation as to why.
Here is what happens for every version:
A. $x == 1 || $x == 2
PHP will compare $x with the value 1, this is true so it can short-circuit the if and echo 'good'.
B. $x == (1 || 2)
PHP will evaluate 1 || 2 because parentheses indicate the priority, as the result should be a boolean expression it will cast 1 to a boolean which evaluates to true so the expression becomes $x == true.
Now PHP will evaluate this expression. First it will cast both types to the same, according to the documentation, it will "Convert both sides to bool". So, same as above, as $x is 1 it will be cast to true and then the expression becomes true == true which is true.
C. $x == 1 || $x == 2 || $x == 3
It is the same as A.
D. $x == (1 || 2 || 3)
It is quite the same as B.
And, for the record 1 == (1 || 2 || 3) evaluates to true.
Related
Within a for loop, i need to add some HTML that outputs only when the loop is on a [(multiple of 3) minus 1].
For example, what i could do is:
for($i=0; $i<count($imagearray); $i++)
{
if($i=="0" || $i=="2" || $i=="5" || $i=="8" || $i=="11")
{
echo 'string';
}
}
but this isnt very elegant and extremely useless for big for loops, is there a proper way to do this?
if ( $i==0 || ($i+1)%3 == 0 )
{
//do stuff
}
What this will do, is go to the next index, divide it by 3, and see if there is a remainder. If there is none, then that means that the current index is one less than a number that is divisible by 3
Use the modulus operator.
if (! (($i+1) % 3) ) {
If $i+1 divides into 3 with no remainder, the result will be zero. Then you just need a boolean not.
If you want to match 0 as well (since you use it in your example, but it doesn't match your description) then you will have to special case it with an ||.
You want to use the modulo for that:
(1 % 3) == 1
(2 % 3) == 2
(3 % 3) == 0
(4 % 3) == 1
Good luck
Modulo is the same thing as saying, give me the remainder of a division. So 1 / 3 equals 0 remainder 1, and so on.
if(($i+1)%3 == 0){
//do something
}
The % operator is known as the modulus operator and returns the remainder of a division.
the most elegent method is thus
if ($i % 3 === 2) {
//do stuff
}
as it doesn't add things to the $i value, but all answers are essentially correct!
I am trying to consolidate a few different if statements. What I am trying to accomplish would read something like this:
If (this is true and this is true) OR (this is true and this is true) AND (This is true)
So, one at least one of the first two sets of parentheses would need to be true, and if one of those is true, then also the last set of parentheses would need to be true, in order for the code inside to be executed.
Here is the specific code I am (unsuccessfully) trying to make work:
if(($calc->number % 2 == 1 && $calc->doubleColor == 'b2' | $calc->number % 2 == 0 && $calc->doubleColor = 'r2') && in_array($calc->number, $backToBet)){
}
Is there a way to do this? A possibility? Is there any drawback to getting a lot into a single if statement?
EDIT
$blackMatch = $calc->number % 2 == 1 && $calc->doubleColor == 'b2';
$redMatch = $calc->number % 2 == 0 && $calc->doubleColor = 'r2';
$numberMatch = in_array($calc->number, $backToBet);
if(($blackMatch || $redMatch) && $numberMatch){
}
/ ** Calc->number = 2, $blackMatch = false, $redMatch = false,
$numberMatch array contains 2 **/
Basically what I end with is a 'true' result, even though neither of the conditions within the inner parentheses are satisfied.
to make code easier to read, I'd suggest to use separate variables, like this:
$condition1 = ($calc->number % 2 == 1) && ($calc->doubleColor == 'b2');
$condition2 = ($calc->number % 2 == 0) && ($calc->doubleColor == 'r2');
$condition3 = in_array($calc->number, $backToBet);
if (($condition1 || $condition2) && $condition3) {
}
two things to note:
|| is logical OR, | is bitwise OR
== is comparison, = is assignment
My code always executes if statement, even if the condition is false, it doesn't go to else.
<?php
$link = $count;
if ($link == 8 || 10) {
echo "<a href='files/".$link.".pdf'>Link</a>";
} else {
echo "Contact HES sales representative for detailed layout model";
}
?>
Because 10 is a "truthy" value, the condition will always be true. You basically have
if($link == 8 || true) {
because 10 == true is, in fact, true.
You should adapt it to either
if ($link == 8 || $link == 10) {
or you can use in_array() if you start to get many values
if (in_array($link, array(8, 10)) {
If you want, you can use strict comparison - if (and only if) $link is an integer. Then you'd have three equalities, which requires the same value and the same type. This is because PHP is weakly typed, so it doesn't compare types by default (and as a result you can compare different types and have a valid result). Using strict comparison allows you to better control what type of variables you compare.
if ($link === 8 || $link === 10) {
Proof that 10 == true
in_array()
Comparison operators in PHP
The 3 different equals
The condition $link == 8 || 10 will always return true. If $link isn't 8, then it checks if 10 is true. Any non-zero value is true.
Instead:
if ($link == 8 || $link == 10) ...
Or if you prefer to check a value in a list:
if (in_array($link, [8, 10])) ...
if($link == 8 || 10)
// this statement will always be true
because 10 is a non-zero which is asserted as true.
you can do like this if($link == 8 || $link == 10) to get this working
You need to modify your if part you are defining your if wrong
if ($link == 8 || $link == 10) {
echo "<a href='files/".$link.".pdf'>Link</a>";}
} else {
echo "Contact HES sales representative for detailed layout model";
}
Can someone explain what's happening here:
if(2 && 5 < 4)
If i've got for example
$x = 2 && 3;
and var_dump($x) it gives boolean(true) no matter what numbers are.
But here it looks like numbers are comparing to 4 one by one.
Look at the PHP comparison table for PHP http://php.net/manual/en/types.comparisons.php
For integers a number other than 0 returns true in comparison.
if (2 && 5 < 4) => if (true && false) => false
$x = 2 && 3 = 1 && 1 = 1
because if a variable has an integer value, true becomes 1 because of type conversion.
I am trying to make a statement with multiple ANDS and ORS, is this possible?
This is what i currently have:
elseif ($a == 'promo' && strstr(ucwords($promo_name),'30% Off') && $b == 'yes')
{echo 'this is a 30% off promotion'; }
I am looking to echo the same message if the $promo_name has either '30% Off' OR 'Save 30%'.
Would I be correct with this?
elseif ($a == 'promo' && strstr(ucwords($promo_name),'30% Off') ||
strstr(ucwords($promo_name),'Save 30%') && $b == 'yes')
{echo 'this is a 30% off promotion'; }
Im getting a little confused with what will take precedence etc. I need both $a == 'promo' and $b == 'yes' to be true at all times, with any 1 of the 2 strstr being true.
Any help much appreciated.
thanks
m,ike
The precedence of && is higher than ||. So you need to use some parentheses to make your statement work, as you expect:
elseif ($a == 'promo' && (strstr(ucwords($promo_name),'30% Off') || strstr(ucwords($promo_name),'Save 30%')) && $b == 'yes')
Try something like
if ((($a == "a") || ($b == "b")) && ($foo == "bar")) {
When I first studied boolean algebra in electronics, we were told that and was like a × and or like a +. This is true for operator precedence (a and b or c translates to a × b + c and hence (a × b) + c), and also helps for truth tables or complicated boolean expressions, expression development, etc.
a b a×b a+b
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 2=1
This is a good mnemonic, but don't ever say that to a mathematician ;-)
So, just to be clear, and has higher precedence than or so yo need parenthesis to do what you'd like : bool1 and (bool2 or bool3)