This question already has answers here:
'AND' vs '&&' as operator
(10 answers)
Closed 9 years ago.
I have the following code and don't think and is needed, i.e. && should be used, as there is nothing to assign the left part to?
if($_REQUEST['foo'] != 'abc' and $_REQUEST['bar'] == 'def')
{
echo "all your base";
}
So it should be:
if($_REQUEST['foo'] != 'abc' && $_REQUEST['bar'] == 'def')
{
echo "all your base";
}
In your case, && and and do the same thing, but take a look at the operator precedence. You can see that && and and are not on the same level, so mixing that could give you unexpected results in some cases - I recommend always using &&, but that's your choice.
&& and "and" are two different things, because of their precedence.
The reason for the two different variations of "and" and "or" operators is that they operate at different precedences. (See Operator Precedence.)
See Logical Operators
And Operator Precedence
'and' and '&&' is the same operator, apart from precedence differences.
They are different operators, but they operate the same, apart from precedence differences.
Related
This question already has answers here:
Short-circuit evaluation via the AND operator in PHP
(5 answers)
Closed 6 years ago.
This is just more of a "why does it work" and "why or why not use it" type question.
We all know PHP expressions using ternary operations
$var = (isset($i) ? true:false);
But in PHP something like the following works too. (It is not ternary, 3 parts, it is more of a binary operation, 2parts.)
$var = true;
isset($i) || $var = false;
Which may not be so practical :) but a more useful construction is
isset($i) || exit();
So the above (much better looking imo) would have the same result as
if(!isset($i)) exit();
But other than the common
defined('CONSTANT') || define('CONSTANT','FOO');
I rarely see this type of construct used in PHP. Why does this work? What is it called. Why is it not taught or used more. Are there cons to using it? And is there practical ways to use && in the same way?
This way of writing statements also exists in C, Perl and other languages, it plays with how the compiler evaluates statements chained with logical operators.
Imagine you have an expression if (a() or b()), then when a() returns true you don't have to evaluate b(), because the entire statement is true already. So b() is only called when a() is false. You can write this without the if, it still works the same way.
This shorthand is usually found like you described, to define default values or exit if a condition is not met.
This question already has answers here:
What's the meaning of the reverse entry (null === $value) when checking the value of the variable? [duplicate]
(4 answers)
Closed 6 years ago.
Often, I see in if statements for php something like this:
if (null === $variable) {
// do stuff
}
What does it do and what is it for?
EDIT: I totally get that it is a comparison operator, I just wonder why not $variable === null.
It's not an assignment, it's a comparison for equality. It determines if the variable $variable contains the value null.
More in the documentation:
Assignment Operators
Comparison Operators
why not to check $variable === null
Some people like to use the form with the constant on the left (a "Yoda condition", it is called) so that if they have a typo and only type a single =, it causes a syntax error rather than doing an assignment.
That is the Yoda style usually used as a trick by programmers to prevent accidental assignments which always give some silent bugs.
Example:
var a = dosomething();
if(a = null){
//more here
}
Note that the if block will always not execute regardless of the result of doSomething method since we assign then check for equality. This assignment nullifies the possibly non-deterministic nature of doSomething
This question already has answers here:
'AND' vs '&&' as operator
(10 answers)
Closed 7 years ago.
Why is $x true in this statment?
$x = true and false;
I've got the problem with some variables but I could reduce the problem to the primitive boolean values.
Update:
As you see in the replies the effect has to do with the operator precedense in PHP. There is also a good explanation about the problem in this question, which I couldn't find in the net before since I didn't know that I have a problem with this and I didn't know that there is a difference between '&&'/'||' and 'and'/'or'.
After some search I found out that this is caused by the operator precedence in PHP. The '=' operator is stronger than 'and'.
If you want to get the expected result you have to use braces:
$x = (true and false); //correct: $x is false now!
Without braces the expression is equal to ($x = true) and false; . $x will get the 'true' value. After that the PHP interpreter 'replaces' this assignment with the value that $x has just got. So true and false; remains. And that does not do anything. 'false' is lost and didn't influence the expression.
Be aware that the braces are NOT required if you use '&&' or '||'! They are stronger than '=' and thus stronger than 'and' and 'or'. Strange...
This is different to e.g. $x = 5 + 6; since here '+' is stronger than '=' it will be resolved first. In the end it affects only the boolean operators 'and', 'or' and 'xor'. Only with them you have to watch out.
Note also that this is different compared to other languages like JavaScript or Java. In those languages you don't need the braces since the logical operators are stronger than the equal operator (Operators in JavaScript, Operators in Java).
More on this:
$x = true and false;
So we have the following. If you guessed that $x is true, you'd be right. Since = has a higher precedent than and.
Rewriting the expression above it would be equal to this:
($x = true) and false;
Here's where it gets funny.
If you were to do:
$x = true && false;
and you'd guess that $x is true, you'd be wrong. $x is actually false in this case.
The above expression is actually equal to:
$x = (true and false);
Why? Because && has a higher precedent than and.
This question already has answers here:
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
(13 answers)
Closed 9 years ago.
I was working on some code made by someone else and came across in if statement that looks like this
IF($res === true){
do_something();}
This code is called many times and seems to be working. What is it doing?
I know one equals is assignment and two is comparison. What does the three equals sign operator do?
The easiest way to see it, is that == checks equality and === checks identicality. Equality will check the value, but identicality will check the variable type too.
Examples:
var_dump('true' == true); // bool(true)
var_dump('true' === true); // bool(false)
This is the "identical" operator. They'll need to be exactly the same to pass the condition.
The 3 different equals
Three === means that result has to be identical. Worded it wrong first time :)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does “===” mean?
i am seeing === often in php statements, but don't know what it mean.
e.g if ($pwd === PwdHash($pass,substr($pwd,0,9))).
thanks
It tests equality, but unlike == it requires that the two operands be of the same type as well as value.
For instance, "1" == 1 will be true, but "1" === 1 is false because the type is different.
php has two types of equal comparison operator == and ===
== check for the equalization but not strict mean it will return true for ('123'==123)
=== is a strict equal operator it will return false for the ('123'===123)
read more about these from here