php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67753 Concat not number with number, without space between
Submitted: 2014-08-03 18:04 UTC Modified: 2014-10-07 13:35 UTC
From: david dot proweb at gmail dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.5.15 OS: Irrelevant
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2014-08-03 18:04 UTC] david dot proweb at gmail dot com
Description:
------------
Ideally we always need to use spaces between concatenations.

    $a = "Hello" . "World";

But in most cases, we can omit it.

    $a = "Hello"."World";
    $a = $b.$c;

Except when we have two numeric, for obvious reasons.
Speaking softly: it's because it becomes a float.

    $a = 1.1;

However, suppose you want to concatenate a variable with an integer.
What do you would think?

    $a = $b.1;

Exactly! The variable $a will get $b concatenated to the integer 1.
Err... Wait! Wait!

    Parse error: syntax error, unexpected '.1' (T_DNUMBER), expecting ',' or ';'.

This also affects the reverse:

    $a = 1.$b;

The problem occurs because PHP thinks that a dot and a number is a "simplified float" (without integer part, or zero). However, in this case is just (or should be) an integer concatenated, since we can not expect a T_VARIABLE T_LNUMBER side to side.

Test script:
---------------
$a = 1;

// Expect all is true.
var_dump( $a . 1 === "11" );
var_dump( 1 . $a === "11" );

var_dump( $a . .1 === "10.1" );
var_dump( .1 . $a === "0.11" );

var_dump( $a..1 === "10.1" );
var_dump( .1.$a === "0.11" );

// Bad guys.
var_dump( $a.1 === "11" );
var_dump( 1.$a === "11" );

Expected result:
----------------
true
true
true
true
true
true
true
true

Actual result:
--------------
true
true
true
true
true
true
Error
Error

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-10-07 13:35 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2014-10-07 13:35 UTC] nikic@php.net
This is a standard problem present in languages using a dot operator (e.g. JS has the same issue if you try to access a property of an integer). It can't be fixed without significant complication of lexical analysis.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 22:01:29 2024 UTC