This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
'AND' vs '&&' as operator
Sorry for very basic question but I started learning PHP just a week ago & couldn't find an answer to this question on google/stackoverflow.
I went through below program:
$one = true;
$two = null;
$a = isset($one) && isset($two);
$b = isset($one) and isset($two);
echo $a.'<br>';
echo $b;
Its output is:
false
true
I read &&/and are same. How is the result different for both of them? Can someone tell the real reason please?
The reason is operator precedence. Among three operators you used &&, and & =, precedence order is
&&
=
and
So $a in your program calculated as expected but for $b, statement $b = isset($one) was calculated first, giving unexpected result. It can be fixed as follow.
$b = (isset($one) and isset($two));
Thats how the grouping of operator takes place
$one = true;
$two = null;
$a = (isset($one) && isset($two));
($b = isset($one)) and isset($two);
echo $a.'<br>';
echo $b;
Thats why its returning false for first and true for second.
Please see:
http://www.php.net/manual/en/language.operators.logical.php
It explains that "and" is different from "&&" in that the order of operations is different. Assignment happens first in that case. So if you were to do:
$b = (isset($one) and isset($two));
You'd end up with the expected result.
Related
This question already has answers here:
Are there pointers in php?
(9 answers)
Closed 6 years ago.
Is there any difference between pointers and references?
When should I use references?
What actually this syntax does?
$a = &$b
Do not use references! They can lead to unexpected behavoiur.
As at PHP.net: One of the key-points of PHP 5 OOP that is often mentioned is that "objects are passed by references by default". This is not completely true.
There are pointers and references, you can see it it example below.
$a = new stdClass ($a now is a pointer to #168)
$b = $a ($b is now a new pointer, but it still points to #168)
$c = &$a ($c is now a reference to pointer $a)
you can't distinguish $b from $c
$a = null, it's just not a pointer anymore, just null
#168 is kept, because there are pointers to it left
$b is still a pointer to #168
$c is now null
This behavior is dangerous, because it alters variable you don't touch.
$b === $c // returns true
$youCantSayThisVariableIsRelated = null
$b === $c // returns false!
This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 6 years ago.
A friend of mine recently showed my the following snippet
<?php
$a = 0;
$b = 'x';
if(FALSE == $a && $a == $b && $b == TRUE) {
echo 'WTF!?';
}
?>
which ouputs WTF!?
I understand why FALSE == $a holds, because zero is considered to be FALSE. I also understand why $b == TRUE holds, because the string is not empty. But I didn't understand why $a == $b is true, could somebody explain to me what type coercion rules play together here to get this rather amusing result?
when you compare $a == $b you are comparing an int with a string so PHP tries to parse the string into an int if it fails which happened in this case, it changes its value to 0 and then 0 == 0 returns true. Check this:
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.
Change this condition $a == $b to $a === $b to compare the type as well. Hope this helps.
Today I have faced a question, I was unable to answer it,
I have tried by making a php program but was unable to find out the exact reason for it
if $a=5 then both($a==5 and 5==$a) are giving me output as boolean true and,
if $a != 5 then both ($a==5 and 5==$a ) are giving me output as boolean false
Can anyone tell me what is the difference between $a==5 and 5==$a from any language point of view.
**Program**
$a = 3;
var_dump( 5==$a );
var_dump( $a==5 );
$a = 5;
var_dump( 5==$a );
var_dump( $a==5 );
**Output**
boolean false
boolean false
boolean true
boolean true
Comparisons like that are not affected by which value you write first. However, it is best practice to put the literal first, e.g. 5 == $x because if you mess up and only enter one equals sign, you'll get an error instead of an accidental value assignment, which is far easier to debug.
No difference
but 5 == $a prevents some error if you forgot one '='.
For example $a = 1
if you write if ($a = 5) - $a value becomes 5
if you write if (5 = $a) - you got error
It's just a technique, that prevents you from accidentally using assignment instead of comparison. The if operator happily accepts $a = 5, while 5 = $a throws an error, preventing you from creating a nasty bug.
5 == $a is logically the same as $a == 5
This format, commonly referred to as yoda conditions and does not affect the logical comparison.
https://en.wikipedia.org/wiki/Yoda_conditions
It is preferred, commonly in PHP, to prevent accidental assignment in conditions, which will always evaluate as either truthy or falsy, but will not actually perform the indented condition check:
if ($a = 5) {
// always run... oops
} else {
// never run
}
The WordPress PHP Coding Standards have a good explanation of this as well
This question already has answers here:
'AND' vs '&&' as operator
(10 answers)
Closed 7 years ago.
In this example:
$x = $b && $c;
$y = $b and $c;
Why $x = true and $y = false ? are && not equal and ?
PHP:
The first code will set $x to the result of the comparison $b with $c, both have to be true,
The second code will set $y like $b and thant , compare the success of this with the value of $c
The precedence of operators is what makes the difference here.
PHP Operator Precedence
JavaScript Operator Precedence
This question already has answers here:
PHP why (null === $variable) and not ($variable === null) in comparison? [duplicate]
(3 answers)
Closed 8 years ago.
I have seen multiple examples of such comparison, some other example ( from wordpress core):
if ( '' != $qv['subpost'] )
$qv['attachment'] = $qv['subpost'];
Is code above same as:
if ( $qv['subpost'] != '' )
$qv['attachment'] = $qv['subpost'];
or they are different in functionality?
Some people prefer the constant == variable option, as it'll cause fatal errors if you accidentally type a = and try to do assignment:
e.g.
$a = 'foo'; // assigns 'foo' to $a
$a == 'foo'; // tests for equality
'foo' == $a // tests for equality
'foo' = $a // syntax error - assigning value to a string constant
But functionally, otherwise, there's no difference between both versions. a == b is fully equivalent to b == a.
There is no difference.
(A == B) == (B == A)
The only thing that someone can put the value first is the readability, for example:
if ( 'APPLE' == $var ) {
} else if ('BANANA' == $var) {
}
There is no functional difference. You are comparing equality, and they will either be equal or not, no matter what side of the operator the values are on.
This question comes down to code style. Personally, when comparing against static values I prefer to always have the variable on the left. Others disagree. Use whatever style is in place in the project you're working on.
Yes, they do the same thing. It checks to see if $qv['subpost'] contains a value in both examples. No difference at all unless you're Yoda.