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.
Related
Can anybody explain why these 2 produce the same result?
$a = 1;
$c = $a + $a++;
var_dump($c);//int(3)
and
$a = 1;
$c = $a + $a + $a++;
var_dump($c);//int(3)
Tested in PHP 7.1. Reviewed Opcode dumps for both cases but still cant get the point. If we add more $a vars to the expression, it produces the expected result.
From PHP: Operator Precedence:
Operator precedence and associativity only determine how expressions
are grouped, they do not specify an order of evaluation. PHP does not
(in the general case) specify in which order an expression is
evaluated and code that assumes a specific order of evaluation should
be avoided, because the behavior can change between versions of PHP or
depending on the surrounding code.
Example #2 Undefined order of evaluation
$a = 1;
echo $a + $a++; // may print either 2 or 3
$i = 1;
$array[$i] = $i++; // may set either index 1 or 2
So in your first example, PHP is obviously returning 1 for $a++ then incrementing it to 2 and then adding the new $a, which is 2.
In your second example, PHP is returning 1 for $a then adding $a then adding $a and then incrementing it to 2.
As can be seen here: https://3v4l.org/kvrTr:
PHP 5.1.0 - 7.1.0
int(3)
int(3)
PHP 4.3.0 - 5.0.5
int(2)
int(3)
Operator precedence tells that order should be: +, &, =. But this code execution shows that order is: &, =, +
$b = 1;
$a = & $b + print('print executed');
if ($a == 1)
echo ' but one was not added and error was not raised';
Output print executed but one was not added and error was not raised
Why precedence is changed for this case?
P.S.
$a = new stdClass();
$c = &$a instanceof $a;
var_dump($c); // class stdClass#1 (0) {}
$b = $a instanceof $a;
var_dump($b); // bool(true)
Arguably, this doesn't really answer your question but consider this code:
$b = 1;
$a = &$b + 123;
The opcodes reveal the following execution strategy:
compiled vars: !0 = $b, !1 = $a
line # * op fetch ext return operands
-----------------------------------------------------------------------------
3 0 > ASSIGN !0, 1
4 1 ASSIGN_REF $1 !1, !0
2 ADD ~2 $1, 123
3 FREE ~2
As you can see, the assignment by reference takes place and the addition gets stored in a temporary variable and then freed; basically, a no-op.
Perhaps the documentation could be clearer, but I can't imagine a scenario in which this particular code would ever make sense :)
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.
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
Explain this interview question to me:
Q: If the variable $a is equal to 5 and variable $b is equal to character a, what’s the value of $$b?
A: 5, it’s a reference to existing variable.
That's a variable variable. PHP will look up the variable with the name stored in the string $b. So if $b == 'a' then $$b == $a.
It's a lot like pointers in C, except they use variable name strings instead of memory addresses to point to each other. And you can dereference as many times as you want:
$a = 5;
foreach (range('b', 'z') as $L) {
$$L = chr(ord($L) - 1);
}
echo $$$$$$$$$$$$$$$$$$$$$$$$$$z;
Output:
5
-95 is the answer as if u will echo $b u will get output as
"a"
and if u echo $a u will get out but as "5"
hence in this sense when u $(echo $b) which same as $(a) hence u will get it as "5-100" which is "-95"
$$b - 100
= $a - 100 // substituting $b=a
= 5 - 100
= -95
I don't know if the '?' is erroneous in the statement '$$b? - 100' but I don't think that will compile.
However:
$a = 5
$b = 'a';
$c = $$b - 100;
$c will equal -95, because $$b is a variable variable reference and given that $a = 5 it resolves to $a (5) - 100, or -95.
the answer is -95
$a - 100
The following is a good reference on PHP variables
http://php.net/manual/en/language.variables.variable.php