What is the difference between !($x == ' ' OR $y == ' ') and $x != ' ' OR $y != ' ' - php

What is the difference of between the following examples?
FIRST EXAMPLE
if($x != '' or $y != '' or $z!=''or $c!=''){
echo "<h3>YOUR INPUT:</h3>";
echo $x.'<br>'.$y.'<br>'.$z.'<br>'.$c.'<br>';
}
SECOND EXAMPLE
if(!($x == '' or $y == '' or $z==''or $c=='')){
echo "<h3>YOUR INPUT:</h3>";
echo $x.'<br>'.$y.'<br>'.$z.'<br>'.$c.'<br>';
}
Please explain. I'm newbie in programming. I couldn't get it when someone post it in my question and I saw the code I thought it was the same as the title, but I tried it and I saw the difference. Help me to understand this.

$x != '' or $y != '' or $z!=''or $c!='' is true if any of the variables are not empty. If any of the variables are abc or are otherwise not '', the condition is true.
!($x == '' or $y == '' or $z==''or $c=='') is true only if all of the variables are not empty. Another more readable expression of those conditions is:
$x != '' and $y != '' and $z != '' and $c != ''

use this code is better you know what is it mean x,y,z & c is not empty give result. || is mean or.
if(!empty($x) || !empty($y) || !empty($z) || !empty($c)){
echo "<h3>YOUR INPUT:</h3>";
echo $x.'<br>'.$y.'<br>'.$z.'<br>'.$c.'<br>';
}

The first example would work if any of the four variables is not empty.
The second example would work only when none of the four variables is empty.

Related

Why does if($x = 1 && $x == 1) throw error but if ($x == 1 && $x = 2) doesn't in php?

In php the following code gives a warning of undefined variable $x:
if($x = 1 && $x == 1)
I thought it was equivalent to if( ($x = 1) && ($x == 1) ), but that's not the case. I've been told, it's because && has higher precedence than =, which causes the expression to get converted to:
if($x = (1 && ($x == 1)))
So far so good, but now consider:
$x=1; if($x == 1 && $x = 2)
This doesn't throw error. Why doesn't it get converted to:
$x=1; if( (($x == 1) && $x) = 2 )
I've been told thats due to = being right assosiative, but https://www.php.net/manual/en/language.operators.precedence.php says When operators have equal precedence their associativity decides how the operators are grouped.. Here we have =, && and == all being of different precedence.
P.S; My actual code is if($result != false && $res = $stmt->get_result()), which has been copied from some other reputable source, so seems like not using unneeded parenthesis is common in php.
I've played with several conditions and below is what I've got.
First, let's consider that we init $x before if statements to avoid undefined variable notice.
Second, let's confirm the precedence for operators:
== is applied 1st
&& is applied 2nd
= is applied 3rd
This returns true:
$x = 1;
if ($x = 1 && $x == 1) {
echo 'true';
} else {
echo 'false';
}
It goes like ($x = (1 && ($x == 1))) -> ($x = (1 && true)) -> ($x = true) -> true.
If we compare $x to another value than the assigned one we will get false:
$x = 1;
if ($x = 2 && $x == 2) {
echo 'true';
} else {
echo 'false';
}
It goes like ($x = (2 && ($x == 2))) -> ($x = (2 && false)) -> ($x = false) -> false.
The last one returns true:
$x = 1;
if ($x == 1 && $x = 2) {
echo 'true';
} else {
echo 'false';
}
It goes like ((($x == 1) && $x) = 2) -> ((true && $x) = 2) -> (true = 2) -> true.
The last comparison can't be interpreted by PHP so it's an approximate view.
It looks like the last action (true = 2) totally depends on the left operand. If we put $x = 2; we will get (false = 2) -> false.
I'm not sure about the last one and here is the only place were some mistakes can happen.
Otherwise, it looks like precedence works as expected.
Anyway, I always put parenthesis for an assignment action inside if operator (especially inside ternary if) to be sure that I will get what I expect.
I don't think this affects performance or readability too much, but it may prevent some logical errors.
UPDATE:
Concering your code if($result != false && $res = $stmt->get_result()) it's not correct to compare it to if($x == 1 && $x = 2) because in your code are two different variables.
In this case logical operator will not call the second part at all if the fisrt one is false, see the 1st example here
UPDATE-2:
After the discussion under this answer we can see that the last conditions ($x == 1 && $x = 2) work like like (($x == 1) && ($x = 2)) -> ((1 == 1) && 2) -> true and $x becomes 2 after it.

Following code is not reaching the else statement

Disclaimer: I'm new to programming, this is a simple exercise I'm doing so go easy on me.
The following code is not reaching (printing) the else statement. Any insight is much appreciated.
$number = '1120011';
// Decimal check
if($number[0] == '+' && $number[1] == '0'){
echo "Error: Can't assign a plus sign on a leading zero!";
// Leading zero check
} elseif($number[0] == '+' || $number[0] == '-'){
echo "Number is decimal";
// Binary check
} elseif (preg_match('~^[01]+$~', $number)) {
echo "Number is binary";
// Code not executing here. For e.g. $number = 1120011
} else {
"Number is non binary";
}
Another question:
Why is the following 'if' not working properly (if I replace it in the code above). I guess it has something to do with bad usage of operators.
if($number[0] == '+' || $number[0] == '-' && $number[1] == '0')
Thanks in advance! :)
For the second question:
if($number[0] == '+' || $number[0] == '-' && $number[1] == '0')
try
if(($number[0] == '+')||(($number[0] == '-')&&($number[1] == '0')))

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..

how to detect a whitespace on string in php program

I want to do treatments on two strings, and I must to know if there are spaces to stop my treatment and move on to the rest of characters
I tested that but it doesn't rule the problem :
for ($i = 0; $i < $lenght; $i++) {
if($text[$i] <> '' && $mask[$i] <> ''){
$nbrcrypted = $stringtonumber[$text[$i]] + $stringtonumber[$mask[$i]];
$resultat .= $numbertostring[$nbrcrypted];
}else{
$indice = false;
}
}
how can I achieve that, thank you in advance
if($text[$i] <> '' && $mask[$i] <> ''){ is useless, use if($text[$i] !== ' ' && $mask[$i] !== ' '){ for spaces
I don't get what you're actually trying to do but if you want to test your string for whitespaces:
if ( strpos( $yourString, ' ' ) !== false )
null; // string has whitespace(s)

give error if number is anything other than 1 or 0

<?php
$gender = 0;
if (($gender != 0) || ($gender != 1))
{
die('error:Must select a gender.');
}
?>
This should give a error if the gender is anything other than 1 or 0. So if i gave 5 it should die. If i gave it 1 it should not die. If i give it 0 it should not die.
I was thinking about a few work arounds
<?php
$gender = 0;
if ($gender == 0)
{
//number is okay
}
else if ($gender == 1)
{
//number is okay
}
else
{
die('error:Must select a gender.');
}
?>
Well that looks sloppy and it would work or i could create a array with 0 and 1 and check if its in it or not. Kinda overkill i think.
Not sure what i'm doing wrong.
You're using the wrong boolean operator:
if (($gender != 0) || ($gender != 1)) {
}
This will be entered if gender is 0, too, because it isn't 1, and vice versa. What you need to do is:
if (($gender != 0) && ($gender != 1)) {
}
Look at this table:
gender A (gender != 0) B (gender != 1) A || B A && B
----------------------------------------------------------------
0 false true true false
1 true false true false
5 true true true true
Also note Joshua's suggestion in the comments:
if (($gender == 0) || ($gender == 1)) {
/* number is ok */
} else {
die();
}
which is a bit longer, but more readable.
if (($gender != 0) && ($gender != 1))
^^
You want to die if both tests are true.
Change the or (||) to and (&&) and it should work, as you only want to fail, when it’s both not 0 and not 1.

Categories