15

I got some PHP code here:

<?php
    echo 'hello ' . 1 + 2 . '34';
?>

which outputs 234,

But when I add a number 11 before "hello":

<?php
    echo '11hello ' . 1 + 2 . '34';
?>

It outputs 1334 rather than 245 (which I expected it to). Why is that?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JustinHo
  • 4,695
  • 3
  • 20
  • 20
  • Why isn't any answer accepted?@JetLaggy seems to be new...accept a answer so that the thread would be closed and answerer gain some reputation! ;) :) – coderunner Apr 30 '13 at 12:43

5 Answers5

16

That's strange...

But

<?php
    echo '11hello ' . (1 + 2) . '34';
?>

or

<?php
    echo '11hello ', 1 + 2, '34';
?>

fixes the issue.


UPDATE v1:

I finally managed to get the proper answer:

'hello' = 0 (contains no leading digits, so PHP assumes it is zero).

So 'hello' . 1 + 2 simplifies to 'hello1' + 2 is 2. Because there aren't any leading digits in 'hello1' it is zero too.


'11hello ' = 11 (contains leading digits, so PHP assumes it is eleven).

So '11hello ' . 1 + 2 simplifies to '11hello 1' + 2 as 11 + 2 is 13.


UPDATE v2:

From Strings:

The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
BlitZ
  • 12,038
  • 3
  • 49
  • 68
  • Hi,man. thanks for the quick reply. 245 is what I expected it to output, but it showed 1334. I just don't understand why the precedence changed if I add number before hello string. – JustinHo Apr 30 '13 at 07:56
  • PHP very like to convert everything to numbers. Read [here](http://stackoverflow.com/a/15856770/2269749). – BlitZ Apr 30 '13 at 07:58
  • @CORRUPT Thanks a lot! A complete answer and easy to understand. Cheers~ – JustinHo Apr 30 '13 at 11:52
6

The dot operator has the same precedence as + and -, which can yield unexpected results.

That technically answers your question... if you want numbers to be treated as numbers during concatenation, just wrap them in parentheses.

<?php
    echo '11hello ' . (1 + 2) . '34';
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
naththedeveloper
  • 4,503
  • 6
  • 36
  • 44
  • Cheers, this helps. I really should just stick to php manual. coz the one that I read earlier says concatenation has higher precedence than addition.... what a shame...... – JustinHo Apr 30 '13 at 08:03
  • 1
    In PHP 8 "+" and "-" now take a higher precedence. So the problem is resolved. – Rain May 16 '21 at 11:33
5

You have to use () in a mathematical operation:

echo 'hello ' . (1 + 2) . '34'; // output hello334
echo '11hello ' . (1 + 2) . '34'; // output 11hello334
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • 1
    Still, it does not explains why `php` ignores `concat` operator and typecast `string` to `int` while concating. Acting as sum, but not as concat. – BlitZ Apr 30 '13 at 07:55
1

You should check the PHP type conversion table to get a better idea of what's happening behind the scenes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kristian Vitozev
  • 5,791
  • 6
  • 36
  • 56
1

If you hate putting operators in between, assign them to a variable:

$var = 1 + 2;

echo 'hello ' . $var . '34';
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45