What does a single ampersand mean in a PHP conditional statement? [duplicate] - php

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.

Related

PHP numbers logic [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
What does the percent sign mean in PHP?
(6 answers)
Closed 3 years ago.
I just started to learn PHP deeper for Zend PHP certification and I found this code, which actually works. Can someone explain me the logic behind this?
<?php
$num = 20% - 8;
echo $num; // 4
What you're seeing is the modulus operator, which in essence asks "What is the remainder of 20 divided by -8".
So you might ask, why isn't it negative 4? From the manual,
The result of the modulo operator % has the same sign as the dividend — that is, the result of $a % $b will have the same sign as $a.
See the PHP: Arithmetic Operators for more documentation.
% is not per cent, but modulus, cf. https://en.m.wikipedia.org/wiki/Modular_arithmetic
20 = 2 * 8 + 4, therefore, 20 % 8 = 4

Not sure which character it is [duplicate]

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.

Literal in front if condition [duplicate]

This question already has answers here:
Why does one often see "null != variable" instead of "variable != null" in C#?
(9 answers)
Closed 8 years ago.
Recently I saw programmer put literal in front of if condition
if ( -99 == $poll_id )
Why they did that? What is the difference with normal if e.g.:
if ( $poll_id == -99 )
This an idiom that is borrowed from C. It is called Yoda Conditions.
The idea behind the idiom is avoiding errors when you type one equal sign instead of two.
When you write
if ( x = 3 )
instead of
if ( x == 3 )
The code would compile, but work incorrectly.
If you get in the habit of turning the condition around, the code with a single equal sign would not compile.

How to get values inside nested parenthesis expression [duplicate]

This question already has answers here:
Regex break string based on and/or in a search concept
(2 answers)
Closed 8 years ago.
I am trying to get the values within parenthesis in a given expression,
Example :
((1 or 2) and (3 and 4))
Output should be like this :
1 => 1 or 2
2 => 3 and 4
3 => 1 or 2 and 3 and 4
Please can anyone help me to get regexp to get the above result ?
((?<=\()[^)]+)
should do the job for you. But for the third result you will have to
substitute [()]
with nothing.
worth mentioning is the use of g modifier
demo here : http://regex101.com/r/eE7hU3

What does this PHP syntax mean [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
Can someone tell me what's the << 0 for? and other more common alternatives if they exist
$newvalue += 1 << 0;
<< is the bitwise left-shift operator.
In this case is seems pretty pointless, since left-shifting 1 by 0 equals 1.
<< 0 does nothing. It is probably there to indicate that the value is some sort of flag. If it was something other than 0, than it would shift the value (1) left by x bits, where is is the value replacing 0.

Categories