View Single Post
Old 11-29-2008, 12:58 PM   #9 (permalink)
kuigg
The Wanderer
 
kuigg's Avatar
 
Join Date: Nov 2008
Posts: 7
Thanks: 2
kuigg is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
It really boils down to what's called operator precedence, meaning the order in which operators (such as =, +, ., etc.) are evaluated.

Operator precedence
Firstly, a table of the order of precedence is available on the operators page of the PHP Manual. Given the original code snippet echo 'Testing ' . 1 + 2 . '34'; we have two operators to concentrate on, addition (+) and concatenation (.).

As you can see from the table that I linked to (go look at it again), both of these operators have the same precedence level (remember highest at the top of the table, lowest at the bottom). They also both have what's called left associativity meaning that the expression is evaluated from left to right. So, putting all that together, we can see how that line of code is evaluated (ignoring the echo for now).

Everything is read left to right which makes things simple for this example.

Operation #1: String concatenation
The first expression ('Testing ' . 1) is evaluated to give a result of (string) "Testing 1" (Note: I'll be prepending the type just to help clarify things, strings are wrapped in double quotes just to clearly show what exactly is in the string).

Operation #2: Arithmetic addition
The next expression uses this result in an addition ('Testing 1' + 2). Now, we have to think about how PHP handles additions of string values as well as regular numbers. In this case the string is first interpreted as an integer to be (integer) 0 (Note: for a full explanation of why this is the case read the section on string conversion to numbers in the PHP Manual). Now that we know what 'Testing 1' is as a number, we carry out the addition operation 0 + 2 resulting in (integer) 2. So the overall result from the line of code so far ('Testing ' . 1 + 2) is (integer) 2.

Operation #3: String concatenation
The next (and final) step is a simple string concatenation (joining of two strings) between our latest result and the string '34', in other words 2 . '34'. Because the string concatenation operator works on strings, our result (which is an integer) needs to be converted to a string type before moving forward. Simply enough it changes to (string) "2". Now that both sides of the operation are strings, we can concatenate them: '2' . '34' to give the final result (string) "234".

Output
Now when this result is echoed, all that we'll see on the page is 234. Done.

Homework
Now, try and work out what the following will output:
  1. echo 'Hello W' . 1 * 0 . 'rld';
  2. echo 10 - 2 * 4;
  3. echo 2 + 'Two' . ' is ' . 2 + 2;

P.S. xenon originally posted any string has the integer value of 0. When doing math between a string and a number, the string always has the value 0. This is not strictly true, strings do not always have the value 0 as demonstrated on the string conversion to numbers manual page.
oh,so expert,thanks!
kuigg is offline  
Reply With Quote