Bug #79498 [Com]: Modifying and evaluating a variable in the same expression
| From: | maple_ at outlook dot com | Date: | Mon, 20 Apr 2020 03:22:46 +0000 |
| Subject: | Bug #79498 [Com]: Modifying and evaluating a variable in the same expression | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-226677@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79498&edit=1
ID: 79498
Comment by: maple_ at outlook dot com
Reported by: maple_ at outlook dot com
Summary: Modifying and evaluating a variable in the same
expression
Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: linux
PHP Version: master-Git-2020-04-20 (Git)
Block user comment: N
Private report: N
New Comment:
i finally know whats means 'undefined behavior', thank u!
Previous Comments:
------------------------------------------------------------------------
[2020-04-20 03:07:11] requinix@php.net
> $a[$b+1]=$b=2;
Trying to modify a variable and use it in the same statement results in undefined behavior, meaning
PHP makes no promises about whether the $b=2 evaluates before or after the $b+1.
If you want to find out more, learn about "sequence points". Most information will be
about C/C++ but it applies PHP as well.
------------------------------------------------------------------------
[2020-04-20 03:00:22] maple_ at outlook dot com
Description:
------------
The following two pieces of code have the same output.
i want to know is that the correct feature of php or something?
Test script:
---------------
$b=1;
$a=array(1=>"maple");
$a[$b+1]=$b=2;
var_dump($a);
$b=1;
$a=array(1=>"maple");
$a[$b]=$b=2;
var_dump($a);
Expected result:
----------------
array(2) {
[1]=>
string(5) "maple"
[3]=>
int(2)
}
array(2) {
[1]=>
string(5) "maple"
[2]=>
int(2)
}
Actual result:
--------------
array(2) {
[1]=>
string(5) "maple"
[2]=>
int(2)
}
array(2) {
[1]=>
string(5) "maple"
[2]=>
int(2)
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79498&edit=1