This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
I'm following a PHP tutorial, but I've come across a chracter and I have no idea what it is.
Here's a picture of it http://prntscr.com/7wktf6
Please tell me what it is and how to get it
That is the "&" symbol, a.k.a. "and" or ampersand
In the picture you took, two ampersands (&) next to each other in a logic statement means "and". Example,
(0 == 1 && 2 == 2) and (0 == 1 && 2 == 3)
evaluiate to false because one or more of the statments on either side of the "&&" is false. While,
(0 == 0 && 1 == 1)
evaluates to true because they are both true.
Here is more information on the matter.
Related
This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 6 years ago.
I've got some weird actions from my php script and finally figured out that this following statement makes problems. Because 0 is equal some string in php.
if(0 == "whats Up?")
{
echo 42;
}
With triple "=" it do what I expected. It is possible for you to give me a briefly answer what is the reason and idea behind this behavior of php? Why did they implement php like this?
I mean I know that 1 == "1" is true and 1 === "1" is not. This is also in python. I also learn from somewhere that 0 could be understandable as false but this example above has no explication for me. But I am sure that you know it.
Thank you in advance
That's because of Type Juggling. The second operand gets converted to an integer and 0 == 0 is true.
var_dump((int) "whats Up?"); // int(0)
This question already has answers here:
What is the difference between these (bCondition == NULL) and (NULL==bCondition)? [duplicate]
(6 answers)
Closed 6 years ago.
Can anyone explain what is the benefit of comparing constant and variable with constant on the left e.g.
if (0 == variable)
Instead of
if (variable == 0)
If you say
if (variable == 0)
you are under the risk of omiting one = and saying
if (variable = 0)
which will set the variable variable to 0.
So putting 0 to the left makes it more typo-safe, because 0 = variable won't be able to set any value to the variable.
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
x = 1
if(x) {
x equals true
}
but what happens when you put the ! infront:
if(!x) {
x equals ?
}
I see it being used lots in tutorials I read and felt I understood it. But I saw it today and it confused me again.
What does it do?
What is it's purpose? why would you use it?
The exclamation mark merely means not that is a boolean negation, so
if(!x)
{
(not x) is true, which means x is false
}
For ordinal types of x that means x == 0, for pointers x == NULL.
This means if x==0 or x== false or not x as where !x
This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 8 years ago.
When I run the follow code in PHP
if('yes' == 0)
echo 'a';
else
echo 'b';
The output is a.
I don't understand what happen?
And can I convert the php code to C source code to have a look what real happening?
PHP is a dynamically typed language, and == is a loose comparison operator, meaning it will first cast values it compares to one type, int for that matter, and then compare them; strings are being cast to integers by taking numericals from the left part, so 1abc casts to 1. By that logic yes cast to 0, and 0 == 0 yields true.
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
The following code uses a single & in a conditional check. What does the single ampersand mean there?
if( $some_array_or_other_var & SOME_CONSTANT_VARIABLE ){
//do something here
}
It does not look like a reference, that's what confuses me.
That is a bitwise AND operation: http://www.php.net/manual/en/language.operators.bitwise.php
If, after the bitwise AND, the result is "truthy", the clause will be satisfied.
For example:
3 & 2 == 2 // because, in base 2, 3 is 011 and 2 is 010
4 & 1 == 0 // because, in base 2, 4 is 100 and 1 is 001
This is commonly used to check a single bit in a bitset, by testing powers of two, you are actually checking if a specific bit is set.