php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79498 Modifying and evaluating a variable in the same expression
Submitted: 2020-04-20 03:00 UTC Modified: 2020-04-20 03:07 UTC
From: maple_ at outlook dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: master-Git-2020-04-20 (Git) OS: linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: maple_ at outlook dot com
New email:
PHP Version: OS:

 

 [2020-04-20 03:00 UTC] 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)
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-04-20 03:07 UTC] requinix@php.net
-Summary: zend_compile_assign +Summary: Modifying and evaluating a variable in the same expression -Status: Open +Status: Not a bug -Package: *Compile Issues +Package: *General Issues
 [2020-04-20 03:07 UTC] 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:22 UTC] maple_ at outlook dot com
i finally know whats means 'undefined behavior', thank u!
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Aug 15 22:05:43 2022 UTC