php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51283 defect stringconcatination with if term
Submitted: 2010-03-12 13:26 UTC Modified: 2010-03-12 13:44 UTC
From: easteregg at verfriemelt dot org Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.2 OS: windows 2008 & linux
Private report: No CVE-ID: None
 [2010-03-12 13:26 UTC] easteregg at verfriemelt dot org
Description:
------------
if i concat some strings like $string .= ' ' . (true)? asd : foo ;
the whitespace is always missing..
see code snipped!

Test script:
---------------
<?php
class test {
    public $data = array();
    public function __construct() {
      
      $this->data["sort"] = "erstellt";
      $this->data["order"] = "1";
      
      $order = null;
      if (isset($this->data["sort"])) {
          $order = $this->data["sort"];
          if (isset($this->data["order"])) $order .= ' ' . ($this->data["order"] == 1) ? "desc" : "asc";
      }
      echo $order;
      //returns erstelltdesc

      echo "\n------------------------------\n";
      
      $order = null;
      if (isset($this->data["sort"])) {
          $order = $this->data["sort"];
          if (isset($this->data["order"]))  {
              $order .= ' ';
              $order .= ($this->data["order"] == 1) ? "desc" : "asc";
          }
      }
      echo $order;
      //returns erstellt desc
    }
}
new test()
?> 

Expected result:
----------------
erstellt desc
------------------------------
erstellt desc

Actual result:
--------------
erstelltdesc
------------------------------
erstellt desc

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-12 13:44 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-03-12 13:44 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Please see php.net7operators for the Operator Precedence.What happensis that in

  $order .= ' ' . ($this->data["order"] == 1) ? "desc" : "asc";

The expression

  ' ' . ($this->data["order"] == 1)

will be evaluated first. Depending on the result "desc" or"asc" will be evaluated and returned.

Use parenthesis:

  $order .= ' ' . (($this->data["order"] == 1) ? "desc" : "asc");
 [2010-03-12 13:46 UTC] easteregg at verfriemelt dot org
but this behaviour isnt always the same, before i reported this here, i discussed this in a forum and some people got the right results with php5.3.1 and me not.
but thanks for this quick reply
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 21:01:33 2025 UTC