![]() |
who can tell me why?
hi,
PHP Code:
thanks. |
try $lol = + '34';
echo $lol + 3; |
Quote:
but it seemes that you have not answer my question. |
do you want 334 to be the output ?
|
have it this way.
PHP Code:
Im not quite sure about it, but maybe its one of the reason. |
Quote:
PHP Code:
That's why you need to put your calculations in paranthesis when doing calculations like that. |
Quote:
|
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:
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. |
Quote:
|
yeah thats it :)
|
| All times are GMT. The time now is 06:19 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0