php same result adding different number of vars - php

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)

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.

Trying to understand operator precedence with assign by reference

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 :)

PHP operator precedence "Undefined order of evaluation"?

http://www.php.net/manual/en/language.operators.precedence.php#example-115
<?php
$a = 1;
echo $a + $a++; // may print either 2 or 3
?>
The example from the php manual doesn't explain very well. Why isn't $a++ evaluated to 2, and then added to 1, so that it always becomes echo 1 + 2 // equals 3? I don't understand how it "may print either 2 or 3". I thought incremental ++ has "higher precedence" than addition +?
In other words, I don't understand why isn't it...
$a = 1;
1) echo $a + $a++;
2) echo 1 + ($a = 1 + 1);
3) echo 1 + (2);
4) echo 3;
It can be either 2 or 3. However in most of the time it will be 3. So why it MIGHT be 2? Because PHP is not describing in which order expressions are evaluated, since it might depends on the PHP version.
Operator precedence in PHP is a mess, and it's liable to change between versions. For that reason, it's always best to use parentheses to group your in-line equations so that there is no ambiguity in their execution.
The example I usually give when asked this question is to ask in turn what the answer to this equation would be:
$a = 2;
$b = 4;
$c = 6;
$val = $a++ + ++$b - 0 - $c - -++$a;
echo $val;
:)
Depending where I run it now, I get anything between 4 and 7, or a parser error.
This will load $a (1) into memory, then load it into memory again and increment it (1 + 1), then it will add the two together, giving you 3:
$a = 1;
$val = $a + ($a++);
This, however, is a parser error:
$a = 1;
$val = ($a + $a)++;
Anyway, long story short, your example 2) is the way that most versions will interpret it unless you add parenthesis around ($a++) as in the example above, which will make it run the same way in all PHP versions that support the incrementation operator. :)
Order of evaluation isn't a precedence issue. It has nothing to do with operators. The problem also happens with function calls.
By the way, $a++ returns the old value of $a. In your example, $a++ evaluates to 1, not 2.
In the following example, PHP does not define which subexpression is evaluated first: $a or $a++.
$a = 1;
f($a, $a++); //either f(1,1) or f(2,1)
Precedence is about where you put in parentheses. Order of evaluation can't be changed by parentheses. To fix order of evaluation problems, you need to break the code up into multiple lines.
$a = 1;
$a0 = $a;
$a1 = $a++;
f($a0, $a1); //only f(1,1)
Order of evaluation only matters when your subexpressions can have side-effects on each other: the value of one subexpression can change if another subexpression is evaluated first.

Adding string with number in php

$a = "3dollars";
$b = 20;
echo $a += $b;
print($a += $b);
Result:
23
43
I have a question from this calculation.$a is a string and $b is number.I am adding both and print using echo its print 23 and print using print return 43.How is it
It casts '3dollars' as a number, getting $a = 3.
When you echo, you add 20, to $a, so it prints 23 and $a = 23.
Then, when you print, you again add 20, so now $a = 43.
The right way to add (which is technically concatenating) strings is
$a = 7;
$b = "3 dollars";
print ($a . $b); // 73 dollars
The + operator in php automatically converts string into numbers, which explains why your code carried out arimethic instead of concatenation
PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.
In PHP 7, type declarations were added. This gives us an option to specify the expected data type when declaring a function, and by adding the strict declaration, it will throw a "Fatal Error" if the data type mismatches.
To specify strict we need to set declare(strict_types=1);. This must be on the very first line of the PHP file. Then it will show fatal error and if you didn't declare this strict then it convert string into integer.
If you need both the values, return them in an array
PHP treats '3dollars' as a integer 3 because string starting with integer and participating in arithmetic operation, so
$a = "3dollars";
$b = 20;
echo $a += $b;
it echo 23; //$a=$a+$b;
now $a = 23 + 20;
print($a += $b); //$a=$a+$b;
it print 43;
Since You have created a variable for the two, it stores the result of each, so when you added $a to 20 it will echo 23 which stores in the system, them when you print $a which is now 23 in addition to $b which is 20. You will get 43.

If $a = 5, $b = 'a', what is the value of $$b?

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

Categories