php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26624 cannot get the right value when i use + operator
Submitted: 2003-12-15 02:45 UTC Modified: 2003-12-15 04:08 UTC
From: mlsx at cie dot xtu dot edu dot cn Assigned:
Status: Not a bug Package: Math related
PHP Version: 4.3.3 OS: windows server 2003
Private report: No CVE-ID: None
 [2003-12-15 02:45 UTC] mlsx at cie dot xtu dot edu dot cn
Description:
------------
first i define a is 8 and b is 6.
When i use echo "a+b=".$a+$b."<br>"; 
the MS IE show the result is 2.
While i use echo "a+b="; echo $a+$b."<br>";
the result is correct!
the same result when use "-" operator  use a line command.
but the result is correct when i use * and /
i cannnot fix it!

Reproduce code:
---------------
<?php
$a = 8;
$b = 2;
$c = 3;
echo "a+b=".$a+$b."<br>";
echo "a-b=".$a-$b."<br>";
echo "a*b=".$a*$b."<br>";
echo "a/b=".$a/$b."<br>";
echo "a%c=".$a%$c."<br>";
echo "a++ is ".$a++;
?>

Expected result:
----------------
2
-2
a*b=16
a/b=4
a%c=2
a++ is 8 

Actual result:
--------------
a+b=10
a-b=6
a*b=16
a/b=4
a%c=2
a++ is 8 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-12-15 03:56 UTC] benjcarson at digitaljunkies dot ca
I believe you need to put the math expression in parentheses because '.' and '+' have the same precedence.  What is happening is that you end up adding the value 2 to the string "a+b=2".  If I do:

echo "a+b=".($a+$b)."<br>";  

it comes out correctly.
 [2003-12-15 04:08 UTC] derick@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.

Indeed, you need to use parenthesis
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Oct 07 07:01:26 2024 UTC