TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   who can tell me why? (http://www.talkphp.com/absolute-beginners/3680-who-can-tell-me-why.html)

kuigg 11-29-2008 03:13 AM

who can tell me why?
 
hi,

PHP Code:

echo 'Testing ' '34'

and this code will output "234"("Testing 334" is expected),why?


thanks.

codefreek 11-29-2008 03:30 AM

try $lol = + '34';
echo $lol + 3;

kuigg 11-29-2008 03:38 AM

Quote:

Originally Posted by codefreek (Post 20034)
try $lol = + '34';
echo $lol + 3;

now output "37".

but it seemes that you have not answer my question.

codefreek 11-29-2008 03:46 AM

do you want 334 to be the output ?

zxt3st 11-29-2008 03:47 AM

have it this way.

PHP Code:

<?php

echo "Testing " . (2) . "34"

?>

I think one of the reason why it will displays 234 is because php reads each line from right to left. And "34" is a string, and when we try to concatenate 2 to 34, 2 will be included as a string also. And bear in mind that we can't do operations like addition, subtraction, etc both strings and integers right?

Im not quite sure about it, but maybe its one of the reason.

xenon 11-29-2008 09:28 AM

Quote:

Originally Posted by t3st (Post 20038)
have it this way.

PHP Code:

<?php

echo "Testing " . (2) . "34"

?>

I think one of the reason why it will displays 234 is because php reads each line from right to left. And "34" is a string, and when we try to concatenate 2 to 34, 2 will be included as a string also. And bear in mind that we can't do operations like addition, subtraction, etc both strings and integers right?

Im not quite sure about it, but maybe its one of the reason.

I think you've gone far enough. I've detailed below how PHP does the calculations:

PHP Code:

echo "Testing " "34";

step1: echo "Testing1" "34";
step2: echo "34";
step3: echo "34";
step4"234" (string!); 

I think you've got it...hopefully. You might want to ask why have I wrote a 0 in there. Well, it's simple enough: any string has the integer value of 0. When doing math between a string and a number, the string always has the value 0.

That's why you need to put your calculations in paranthesis when doing calculations like that.

kuigg 11-29-2008 10:34 AM

Quote:

Originally Posted by xenon (Post 20049)
I think you've gone far enough. I've detailed below how PHP does the calculations:

PHP Code:

echo "Testing " "34";

step1: echo "Testing1" "34";
step2: echo "34";
step3: echo "34";
step4"234" (string!); 

I think you've got it...hopefully. You might why have I wrote a 0 in there. Well, it's simple enough: any string has the integer value of 0. When doing math between a string and a number, the string always has the value 0.

That's why you need to put your calculations in paranthesis when doing calculations like that.

your explanation is clear and splendid,thank you.

Salathe 11-29-2008 12:10 PM

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.

kuigg 11-29-2008 12:58 PM

Quote:

Originally Posted by Salathe (Post 20053)
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!

zxt3st 12-02-2008 02:14 AM

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