php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65589 PHP Language BUG
Submitted: 2013-08-30 08:17 UTC Modified: 2013-10-06 13:53 UTC
From: wwwgying at qq dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.5.3 OS: Windows/Mac/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: wwwgying at qq dot com
New email:
PHP Version: OS:

 

 [2013-08-30 08:17 UTC] wwwgying at qq dot com
Description:
------------
<?php
$a=1;
$c=$a+$a+$a++;
var_dump($c);

$a=1;
$c=$a+$a++;
var_dump($c);
?>

we will get int(3) int(3)
and the same code,php result is not the same with other program languages.

Test script:
---------------
<?php
$a=1;
$c=$a+$a+$a++;
var_dump($c);

$a=1;
$c=$a+$a++;
var_dump($c);
?>

Expected result:
----------------
int(3) int(3)

Actual result:
--------------
int(3) int(2)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-08-30 20:45 UTC] aharvey@php.net
-Status: Open +Status: Not a bug -Package: PHPScript +Package: Scripting Engine problem
 [2013-08-30 20:45 UTC] aharvey@php.net
This is undefined behaviour in PHP, as in most languages with C-derived syntaxes.

Mailing list discussion from last month:
http://marc.info/?t=137427934300002&r=1&w=2

This is also documented, see example 1 on:
http://www.php.net/manual/en/language.operators.precedence.php
 [2013-10-06 08:35 UTC] gautam dot nishchal at gmail dot com
I would like to say something to reporter, i think that's a post increment expression which will give int(3) int(3). That's not bug...
 [2013-10-06 08:44 UTC] gautam dot nishchal at gmail dot com
And who tells the program gives int(3) int(3)? I just wrote a program in C which gave me 3 and 2,
#include <stdio.h>
int main() 
{
int a, c;
a=1;
c=a+a+a++;
printf("%d\n",c);
a=1;
c=a+a++;
printf("%d", c);
}
This program prints 3 2 not 3 3
 [2013-10-06 08:48 UTC] gautam dot nishchal at gmail dot com
Expected result is int(3) int(2) I suggest you to read about pre and post increment / decrement :p
 [2013-10-06 09:44 UTC] gautam dot nishchal at gmail dot com
Yep, there's a bug, but way of expressing is wrong, expected output is 3,2 but gives you 3,3
 [2013-10-06 13:53 UTC] rasmus@php.net
You should read this:

http://c-faq.com/expr/seqpoints.html

Just because your particular C compiler gave you 3,2, there is no guarantee that another one will produce the same result for that code. This is well-known and documented to be undefined in both C and PHP and you cannot rely on the result of an expression like that.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 23:01:31 2024 UTC