php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #10770 . (concatenation) preceeds mathematical operations in order of execution
Submitted: 2001-05-09 19:21 UTC Modified: 2001-05-09 19:57 UTC
From: vitaliyf at gameaholic dot com Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.0.5 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: vitaliyf at gameaholic dot com
New email:
PHP Version: OS:

 

 [2001-05-09 19:21 UTC] vitaliyf at gameaholic dot com
<?
  $i=10;
  print "hello " . $i-2 . " world";
?>

will output:   -2 world

("hello " . $i is evaluated into "hello 10", then 2 is subtracted (by casting "hello 10" into a 0, then subtracting 2).  Then "world" is appened, thus giving us "-2 world")

I think this might be way counter-intuitive to what should be happening: - (or all other operations) should be avaluated first, and concatenation last, producing "hello 8 world".

Or how about this example, which definately can confuse some people:

print "Hello is less than " . 1+strlen('hello') . " characters long.";

Workaround: use () to arrange things to be executed in proper order but that might not be obvious for someone who doesn't know about this situation.

PS:
Thanks PartyZan for finding this.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-05-09 19:57 UTC] sniper@php.net
http://www.php.net/manual/en/language.operators.precedence.php

. and - are equal. Use ()'s to override it.

--Jani

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 18:01:28 2024 UTC