A PHP Basics question

Hi all,

Just a simple question. I am wondering why the result of

echo "test " . 1 + 10 . " 123";

is “10 123”

“test 1” becomes 0… that’s right
But… Is “10 123” considered an integer? :rolleyes:

Thanks!

Good point
Thanks

Yes, you are right. The left most “.” is evaluated first and since it is a string operator it expects both operands to be a strings and the result is a string either. “+” interprets both operands as integers and the result is an integer too. Intval of “test 1” is 0.

But in case:

echo "0.1 test " . 1 + 10 . " 123";

The result is 10.1 123, though (int)“0.1 test” is 0, but now the “+” operator treats both operands as float values.

Well, I would say it goes like this:

step 1.
(string) "test " and (int) 1 is concatenated
result (string) “Test 1”

step 2.
(string) “Test 1” and (int) 10 will be added together.
result (int) 10.
since “test 1” doesnt start with a number, the value will be evaluated as 0

step 3
(int) 10 gets concatenated with (string) " 123", and thus converted into a string.
Result (string) “10 123”

Sure, I know!
The code I’ve posted is a sample of PHP5 Certification “PHP Basics” questions… I was just trying to understand the logic about it, not the sense :smiley:

The way I see this, would be like this:

1 string “Test "
1 calculation 1 + 10
1 string " 123”

The . concatenates strings, and the + add numbers together
Mixing the two doesnt really make any sense.

I would have expected you wanted to execute the line like this :

echo "test ". (1 + 10) ." 123";

resulting in “test 11 123”

In fact…

echo (int) "10 123";

results “10”

So, I can answer myself stating both + and . operators have same priority
Then starting from the left…

“test 1” + 10 = 0 + 10 = 10

And then…

10 . " 123" = “10 123”

Am I right?