using |= in php - php

I was reading some php code source and found the following:
$failed |= is_numeric( $key );
Other than if $key is numeric , what does |= mean?

$x |= $y; is the same as $x = $x | $y;
$x | $y is a bitwise operator which means it returns the result of a logical 'or' between the two variables.
In the context of the question, it allows $failed to store failure statuses for several actions in a single variable (each bit position representing an individual action).
If you need to know more about what this does, I suggest reading the PHP manual page for bitwise operators: http://www.php.net/manual/en/language.operators.bitwise.php

The notation $a |= $b means $a = $a | $b, similar to other x= notations. The | is a bitwise OR operation.

It's the equivalent of:
$failed = $failed | is_numeric($key);
| is the bitwise or operator.
Anytime you see x <something>= y, it can be rewritten as x = x <something> y, pretty much.

That's a bitwise OR so the line is the same as
$failed = $failed | is_numeric($key);
That means $failed is true if either $failed has been true before or is_numeric($key) is true.

Related

PHP check | (OR) operator in variable assignment

I want to check two variables a and b and assign both the variable to new variable "c" and want to get the OR result from both the variables. for example if a=1 and b=0, c must be 1, while if a=0 and b=1, c must be 1, if a=0 and b=0 then c=0, for this purpose i am using the following | operator, which returns the required result, but i am not sure if i am doing it correct or not
<?php
$a = 0;
$b = 1;
$c = $a | $b;
echo("Value in $c = ".$c);
?>
EDIT: i have gone through the PHP.NET website and find that:
$a | $b Or (inclusive or) Bits that are set in either $a or $b are set.
Reference: http://php.net/manual/en/language.operators.bitwise.php
Assuming you only have the states you have in your question, you can use a ternary to do this. It might help others understand what you're doing in the future
$c = ($a || $b) ? 1 : 0;
There's nothing wrong with the way you did it in your question, tho.

What is the difference between "&=" and "=&" when assigning a variable value in PHP?

I know that =& usually means "assign by reference", but what happens if we reverse these two characters, since I've seen this in plenty of PHP scripts?
$a &= $b is short for $a = $a & $b which is the bitwise-and operator.
It's the compound bitwise AND/assignment operator:
$x = 0x01;
$y = 0x11;
$y &= $x; // bitwise AND $y and $x, assign result back to $y
var_dump($y == 0x01); // true
&= is a compound assignment operator, whereas =& is actually two separate operators (= and &), pushed together. This is legal syntax because PHP doesn't demand whitespace between them.
&= performs a bitwise AND operation between the left hand side and right hand side, then assigns the result to the left hand side variable.
$x = 1;
$x &= 0; // $x === 0 now. A more verbose syntax would be "$x = $x & 0;"
On the other hand
=& should really be expanded to = & as the operators are seperate. This is known as assignment by reference. The = is your standard assignment operator, and the & when prefixed before a variable name returns the reference to the variable.
$y = "foobar";
$x = &$y; // $x now holds a reference to $y.

What is this PHP syntax with a caret called and what does it do?

I came across this syntax in a codebase and I can't find any more info on it. It looks like the caret operator (XOR operator), but because the statement below was executed when a certain condition was met I don't think that is it.
$this->m_flags ^= $flag;
Because I don't know what it's called I also can't search for it properly..
Update:
Because of Cletus' answer:
Are the following lines then functionally equal?
$a = $a ^ $b;
$a ^= $b; // the shorthand for the line above
It's bitwise XOR equals. It basically toggles a flag because I'm getting $flag is a power-of-2. To give you an example:
$a = 5; // binary 0101
$b = 4; // binary 0100
$a ^= $b; // now 1, binary 0001
So the third bit has been flipped. Again:
$a ^= $b; // now 5, binary 0101
Bitwise XOR and assign operator
http://php.net/manual/en/language.operators.bitwise.php

Operator precedence issue in Perl and PHP

PHP:
$a = 2;
$b = 3;
if($b=1 && $a=5)
{
$a++;
$b++;
}
echo $a.'-'.$b;
$a = 2;
$b = 3;
if($a=5 and $b=1)
{
$a++;
$b++;
}
echo $a.'-'.$b;
Output 6-16-2.I don't understand the 1 here.
Perl :
$a = 2;
$b = 3;
if($b=1 && $a=5)
{
$a++;
$b++;
}
print $a.'-'.$b;
$a = 2;
$b = 3;
if($a=5 and $b=1)
{
$a++;
$b++;
}
print $a.'-'.$b;
Output 6-66-2, I don't understand the second 6 here.
Anyone knows the reason?
Actually I know && has higher precedence than and,but I still has the doubt when knowing this before hand.
UPDATE
Now I understand the PHP one,what about the Perl one?
Regarding Perl:
Unlike PHP (but like Python, JavaScript, etc.) the boolean operators don't return a boolean value but the value that made the expression true (or the last value) determines the final result of the expression† (source).
$b=1 && $a=5
is evaluated as
$b = (1 && $a=5) // same as in PHP
which is the same as $b = (1 && 5) (assignment "returns" the assigned value) and assigns 5 to $b.
The bottom line is: The operator precedence is the same in Perl and PHP (at least in this case), but they differ in what value is returned by the boolean operators.
FWIW, PHP's operator precedence can be found here.
What's more interesting (at least this was new to me) is that PHP does not perform type conversion for the increment/decrement operators.
So if $b is true, then $b++ leaves the value as true, while e.g. $b += 1 assigns 2 to $b.
†: What I mean with this is that it returns the first (leftmost) value which
evaluates to false in case of &&
evaluates to true in case of ||
or the last value of the expression.
First example
$a = 2;
$b = 3;
if($b=1 && $a=5) // means $b = (1 && $a=5)
{
var_dump($b); //bool(true) because of &&
$a++;
$b++; //bool(true)++ ==true, ok
}
echo $a.'-'.$b;
hope you will not use those codes in production)
I'm noob in perl but i can suggest a&&b returns a or b (last of them if all of them converted to bool), not boolean, then $b = (1 && $a=5) returns $b=5 (is 5)
here's the issue: 1 && 5 returns 5 in perl. you get the result you expect if you code the conditional as if(($b=1) && ($a=5))
For Perl, fig. 2: and has a very low priority in perl, it's not a synonym of &&'s. Therefore the sample is executed as (($a = 5) and ($b = 1)) which sets $a and $b to 5 and 1 respectively and returns a value of the last argument (i.e. 1).
After ++'s you get 6-2.
refer to http://sillythingsthatmatter.in/PHP/operators.php for good examples

What do the '&=' and '=&' operators do?

Finding the answer to this is turning out to be much more difficult than I would have thought. Since I don't have a clue what you'd call this, it's hard to run a Google search since it will ignore those characters.
I tried browsing the PHP Assignment Operators page, and even the other operators pages, and found nothing that told me exactly what they do. I don't just want to guess based on the single function I have that uses it. So what exactly do the '&=' and '=&' operators do?
All I know is it sets a variable, which would be the '=' part, so I really need to know what the '&' part is doing.
Please don't say the obvious; I need someone to explain exactly what they do. I know one of them is 'bitwise', but that doesn't mean anything to me.
=& assigns by reference
$a = 1;
$b =& $a;
$a++;
echo $b; // 2
From PHP Manual on References:
References in PHP are a means to access the same variable content by different names.
&= is a bitwise AND assignment
$a = 1;
$a &= 1; // is the same as
$a = $a & 1;
echo $a; // 1
From Wikipedia on Bitwise AND:
A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. In each pair, the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise, the result is 0. For example:
0101
AND 0011
= 0001
EDIT: For a practical example on bitwise operations, see my answer to Bitwise Operations in PHP
=& is assigning by reference.
It assigns a variable not by value but by reference.
Example:
$a = 'foo';
$b =& $a;
$b = 'bar';
echo $a;
prints bar because $b has a reference to $a and therefore changing $b also changes the value of $a.
&= is bitwise AND.
Example:
$a = 4 // binary representation: 100
$b = 1 // binary representation: 001
Then $a &= $b is just short for $a = $a & $b and means: Take every bit and perform the AND operation, that is:
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
0 & 0 = 0
Therefore
1 0 0
AND 0 0 1
-----
0 0 0
=> $a = 0 // bit representation 0 ;)
&=
is the bitwise "AND" assignment operator. It performs an "AND" on the variable and stores the result. (more information is in Bitwise Operators and more general information is in Bitwise Operations in C ).
The
=&
operator is an assignment by reference, which makes the variable point not to the value of the other variable or constant, but rather to that memory location (more information is in What References Are).
'&=' and '=&' are very different operators.
'&=' is a bitwise assignment operator:
$var = false;
$var &= foo(); // will call foo()
$var = false & foo(); // will call foo()
$var = $var && foo(); // will not call foo()
'=&' returns a reference:
$a = $b; //$a points to $b
$a =& $b; //$a does NOT point to $b... both point to the same thing.
"=&" means input a value, that the content will adapt with the previous variable
the example
the output is =
satu
satu

Categories