php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19883 string concatenation bug
Submitted: 2002-10-12 20:13 UTC Modified: 2002-10-13 12:09 UTC
From: skaventruc at yahoo dot fr Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.2.0 OS: Windows NT
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: skaventruc at yahoo dot fr
New email:
PHP Version: OS:

 

 [2002-10-12 20:13 UTC] skaventruc at yahoo dot fr
concatenation hazard ... on :

$a = 2;
echo 'c='.$a*2 .' ok';

it produce a good result : "c=4 ok"

but :

$a = 2;
echo 'c='.$a*2.' ok';

produce a parse error :
unexpected T_CONSTANT_ENCAPSED_STRING ...

also the following works :
echo 'c='.$a*2..' ok';

so it should be a bug when trying to see
"2." as a float when it is an integer followed by a concatenation operator (that's the problem when using
operators that are also separators...)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-10-12 22:50 UTC] sniper@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
 [2002-10-13 09:41 UTC] skaventruc at yahoo dot fr
maybe you do not consider it as a bug but it is quite
annoying when contatenating arguments, I personnaly
think that this is a lack of the parser/compiler to
do not detect that the '.' is not a part of the '2'
but it is an operator :

putting a '.' after the 2 doesn't change it's display.
$e = 2.3.'km'; // the dot after 2 followed by a digit
               // indicate a float
'2.3km'

$e = 2..'km'; // is ok the the dot after 2 is useless
'2km'

$e = 2.'km'; // produce a parse error
$e = 2 .'km'; // is ok
'2km'
 [2002-10-13 09:46 UTC] skaventruc at yahoo dot fr
and also :

$e = 0x7C.'km'; // is ok '124km'
$e = 124.'km'; // parse error

why do you process decimal integers in different way
than hexadecimal integers ? If it is only to detect
decimal float then you should take care to the fact that
'.' is not only a decimal separator but also a concatenation
operator !!!
 [2002-10-13 09:47 UTC] derick@php.net
Why don't you just use '2km' instead of 2.'km' ?

Derick
 [2002-10-13 11:16 UTC] skaventruc at yahoo dot fr
and when you want to concatenate with dynamic
calculated infos ?

$i = 153;
echo $i*2.'km';
 [2002-10-13 11:38 UTC] spic@php.net
In that case, simply use

$i = 153;
echo $i*2
echo 'km';

or printf()...
 [2002-10-13 12:03 UTC] skaventruc at yahoo dot fr
Yes, of course,
I was only thinking that the advantage of PHP over C
was that we can concatenate easily strings with
expressions and functions results when setting variable,
calling functions ...

this syntax allow to do :

something('a='.$a.' and f(a)*2 = '.f($a)*2.' ...');

instead of :
$str = 'a=';
$str .= $a;
$str .= ' and f(a)*2 = ';
$str .= f($a)*2;
$str .= ' ...';
something($str);

or :
$str = sprintf('a=%d and f(a)*2 = %d ...', $a, f($a)*2);
something($str);

I really think that if you allow this kind of concatenation
to avoid complex string composition, then the 'float'
problem is a bug, standard C does not allow dynamic
string concatenation but in C++ you can overload the '+'
operator and there is no confusion with the '.'

I know there is always a way to overcome the problem
in syntax, my report is only about something that
appear to me to be an error.

Julien.
 [2002-10-13 12:09 UTC] sander@php.net
THIS IS NOT A BUG!
Read the docs about operator precedence (http://www.php.net/manual/en/language.operators.php#language.operators.precedence).
To avoid problems like these use parentheses ():
echo 'c='.($a*2).' ok';
 [2002-10-13 12:58 UTC] skaventruc at yahoo dot fr
ok, ok,
I understand, I also read carefully the docs,
I know there is 10000000 ways to avoid a syntax problem,

'.' operator have left associativity and that mean
that when the parser found a '.' it operate the
concatenation function with the left member first.
I talk about a very special case when the left member
is a decimal digit (maybe we meet a float ?) and there
is no digit on the right side :

is a float in the form 123. is a float ?
- yes because the dot say that it is a float (see IEEE ...)
- not really because there is no decimal part, only integer
  so it can be considered as a integer. (round integer)
- the php introduce '.' as an operator but it is in some
  way imcompatible with the '.' definition with floats.

so what is the good decision to make ?

only ask to a human reader what did he understand
when he see :

123.'km'

and

0x45.'km'
or 145.7.'km'

ask anyone why the first case is false and try
to explain that this is in the doc, that the '.' operator
have left associativity and that 123 is a very special
case. Syntax rules should avoid us to do 'meaning' errors
but i'm not sure that the decision to take 2. as a float
in this case is a good decision.

And ... that's only my opinion, sorry to hurt you ...
I'm programming script parser since more than 15 years
and sometime it is better to listen users than stopping
to the docs and technical difficulties...

have a nice day.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC