PHP If Statement including OR and AND - php

Am I correct in thinking that the below statement will validate if $a is either '1' or '0', and $b is either 'x' or 'y'
if ($a == '1' && $b == 'x' || $b == 'y')
or do I have to be more specific, i.e.
if ($a == '1' && $b == 'x' || $a == '1' && $b == 'y')

You need to use parenthesis to specify precedence:
if ($a == '1' && ($b == 'x' || $b == 'y'))
if (($a == '1' && $b == 'x') || ($a == '1' && $b == 'y'))

You need this:
if (($a == '1') && ($b == 'x' || $b == 'y'))

The first would be sufficent for that, but you have to put a parenthesis around $b:
if ($a == '1' && ($b == 'x' || $b == 'y'))

Related

Need better simpler solution for conditional link

I have three condition/variable combination e.g. below called amounts:
$a = 15000; $b = 10000; $c = 5000;
or
$a = 10000; $b = 15000; $c = 0;
or
$a = 12000; $b = 0; $c = 15000;
etc.
At least each $a or $b or $c above is not 0 (zero).
If the amount of each not 0 (zero) it have its own associated array e.g. if $b == 0 then $b_array will not set/created, assume all is not 0(zero) then below arrays are created:
$a_array = array('id'=>1);
$b_array = array('id'=>2);
$c_array = array('id'=>3);
If $a / $b / $c is not 0 (zero) then if $b or $c not zero it needs to be linked to $a or $b (if not zero) as below:
if($a != 0 && $b != 0 && $c != 0){
$b_array['id_link'] = $a_array['id'];
$c_array['id_link'] = $a_array['id'];
} elseif ($a != 0 && $b != 0 && $c == 0){
$b_array['id_link'] = $a_array['id'];
} elseif ($a != 0 && $b == 0 && $c != 0){
$c_array['id_link'] = $a_array['id'];
} elseif ($a == 0 && $b != 0 && $c != 0){
$c_array['id_link'] = $b_array['id'];
}
The result for conditional statement above seems correct as you can check at the php sandbox
Is there any better idea for the conditional code and is there a missing condition (error handling). Any idea or solutions is greatly appreciated. Thanks!!
If I understand your question correctly, instead of checking for every combination possible, just get the first non zero number and assign it's id as id_link to all of them like below:
<?php
$id_link = -1;
if($a !== 0){
$id_link = $a_array['id'];
}else if($b !== 0){
$id_link = $b_array['id'];
}else if($c !== 0){
$id_link = $c_array['id'];
}
$a_array['id_link'] = ($b_array['id_link'] = ($c_array['id_link'] = $id_link));
Online Demo
(The brackets in the expression are added just for readability since they are redundant in case of PHP)
Update:
If you wish to leave the array with it's corresponding variable without the id_link key, then you can just use another 3 if conditions to take care of it.
<?php
$id_link = -1;
if($a !== 0){
$id_link = $a_array['id'];
}else if($b !== 0){
$id_link = $b_array['id'];
}else if($c !== 0){
$id_link = $c_array['id'];
}
if($a !== 0){
$a_array['id_link'] = $id_link;
}
if($b !== 0){
$b_array['id_link'] = $id_link;
}
if($c !== 0){
$c_array['id_link'] = $id_link;
}
Online Demo

IF statement checking null variable to use min

This seems pretty simple, but I can't figure that out.
I am checking a few variables before showing some stuff on the screen, and I have a variable which can be null at some point.
On my if statement I have:
if ($a != 'abc' && ($a == $b || $b == $c) && min($variable) > 3) { ... }
How can I set it true if min($variable) is null, if all the other statements are true?
As I understood from your question,
the min($variable) maybe null and you want to check it whether it's value less than 3 or null value ?
if ($a != 'abc' && ($a == $b || $b == $c)
&& (min($variable) > 3 || min($variable) == null)) { ... }
could be using a ternary operator inside ( )
if ($1 != 'abc' && ($1 == $2 || $2 == $3) &&
( $variable == null ? true : min($variable) > 3) ) { ... }

Using 'and' and 'or' in an if/else PHP statement

I am attempting to use both AND and OR statements in my IF/ELSE statement, but I cannot get the desired effect.
What I would like to achieve is that if either 'a' or 'b' has a value of '1' but both 'c' and 'd' must be 1 then I get 'Yes'.
All my attempts have given me either 'Yes' or have not worked (blank screen).
<?php
$a = "0";
$b = "1";
$c = "1";
$d = "1";
if (($a == "1") || ($b == "1") && ($c == "1") && ($d == "1")) {
echo "Yes";
}
else {
echo "No";
}
?>
Thank you.
You need and extra parenthesis, to make sure the evaluation order will be done correctly, like in math:
if ( ( ($a == "1") || ($b == "1") ) && ($c == "1") && ($d == "1")) {
^ ^
That way, let's say for example:
$a = 1;
$b = 2;
$c = 1;
$d = 2;
The first parenthesis will be evaluated as true || false. The final result will be true.
So now you have true && ($c == "1") && ($d == "1")
$c = 1, so again, the next evaluation will be true && true && ($d == 1)
$d = 2, so the next round will be true && true && false, final result, in this example, will be false.
You need to add parenthesis.
Why?
Because inner parenthesis are evaluated first before outer parenthesis. Take this example:
((1 == 1 && (2 == 2)) || 3 == 3)
What will be evaluated first? The 2 == 2 then the 1 == 1 and then the 3 == 3. In your if condition, because you are mixing AND's and OR's, you will not get the desired affect.
( (($a == "1") || ($b == "1")) && ($c == "1") && ($d == "1") )
Should work for you. In fact you can do this so that it looks even better:
(($a == 1 || $b == 1) && $c == 1 && $d == 1)
Because it is not necessary to put 1 in quotes ie: "1". PHP's truthiness will evaluate 1 == "1" to be true. However if you wanted to check for an actual string that contains 1, then you would use the === operator.
$a = 1;
$b = "1"
$a == "1"; // true
$b == 1; // true
$a === "1"; // false
$b === "1"; // true
However for more information go here: http://php.net/manual/en/language.operators.precedence.php
The equality operators will be evaluated first, then &&, then ||. Parentheses will be evaluated before anything else, so adding them can change the order.
Check the answer In Java, what are the boolean "order of operations"?
It will always echo a Yes because PHP interpreter places The AND operation before the OR operation.
So your if statement interpretes like this:
If
a = 1 or b = 1 and c = 1 and d = 1
then
echo 'Yes'
else
echo 'No'
That's why you always get a yes..

Php OR operator gives wrong result?

I am surprised with the output of OR operator in php
$a = 5;
echo $b = ((intval($a) == 8 || 2) && intval($a) != 0 ? $a : NULL);
It echo 5 but i expect NULL
It should be like this.
$a = 5;
echo $b = ((intval($a) == 8 || intval($a) == 2) && intval($a) != 0 ? $a : NULL);
As you stated, your $a is not 8 or 2. So assume it's 5.
How your evaluation works:
((intval($a) == 8 || 2) && intval($a) != 0 ? $a : NULL);
Compare intval($a) == 8.
No matter what you get from #1, do || 2 which leads to true (non-zero number is loosely equals true)
Compare intval($a) != 0 - leads to true.
Compare true && true => true
Answer is $a.
Step-by-step:
(intval($a) == 8 || 2) && intval($a) != 0 ? $a : NULL;
(false || 2) && intval($a) != 0 ? $a : NULL);
(false || true) && intval($a) != 0 ? $a : NULL;
true && intval($a) != 0 ? $a : NULL;
true && true ? $a : NULL;
$a;
TL;DR
To get NULL, change || 2 to || intval($a) == 2
change this
intval($a) == 8 || 2
to
intval($a) == 8 || intval($a) == 2
i.e
echo $b = ((intval($a) == 8 || intval($a) == 2) && intval($a) != 0 ? $a : NULL);
The way you write comparison for OR is not possible in PHP. Use below code.
echo $b = ((intval($a) == 8 || intval($a) == 2) && intval($a) != 0 ? $a : NULL);

Elegant way to shorten if statement

Any ideas how to shorten if statment in an elegant way.
My if statement:
if(getfoo1() == getfoo2() && getfoo2() == 1)
{
}
EDIT:
I'm looking for something like:
if(getfoo1() == getfoo2() ==1)
{
}
But I suppose we can't do this.
$a = getfoo1();
$b = getfoo2(); // less operations, while it not produces duplicate calls
if($a == $b && $b == 1){
// do something
}
$variable = ((getfoo1() == getfoo2() && getfoo2() == 1) ? $value1 : $value2);
More elegant, combined:
$a = getfoo1();
$b = getfoo2();
$variable = (($a == $b && $b == 1) ? $value1 : $value2);
Since we don't know the possible return values from the functions, if you assume they are integers then you can say:
$a = getfoo1();
$b = getfoo2();
if (($a * $b) === 1) { // strict equality for the win
echo 'hi';
}
The result would only be true iff both $a AND $b are 1.
Another way:
$both = array(getfoo1(), getfoo2());
// use array_diff_assoc so it checks multiple occurrences of the same value
$diffCount = count(array_diff_assoc($both, array(1, 1)));
if ($diffCount === 0) {
echo 'hi';
}
Since anyway getfoo2() == 1 must be true, a better approach is to first check whether getfoo2() is equal to 1. If it false no matter about 2nd condition. But If you first check getfoo1() == getfoo2() and and then check getfoo2() == 1 you have to check 2 conditions all the times.
Therefore go for
$a = getfoo1();
$b = getfoo2();
if($b == 1 && $a == $b)
{
// logiv
}
else
{
}
Try this.
$a = getfoo1();
$b = getfoo2();
if( intval($a && $b) === 1) {
echo 'hi';
}

Categories