TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-29-2008, 03:13 AM   #1 (permalink)
The Wanderer
 
kuigg's Avatar
 
Join Date: Nov 2008
Posts: 7
Thanks: 2
kuigg is on a distinguished road
Default who can tell me why?

hi,

PHP Code:
echo 'Testing ' '34'
and this code will output "234"("Testing 334" is expected),why?


thanks.

Last edited by kuigg : 11-29-2008 at 03:41 AM.
kuigg is offline  
Reply With Quote
Old 11-29-2008, 03:30 AM   #2 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

try $lol = + '34';
echo $lol + 3;
codefreek is offline  
Reply With Quote
Old 11-29-2008, 03:38 AM   #3 (permalink)
The Wanderer
 
kuigg's Avatar
 
Join Date: Nov 2008
Posts: 7
Thanks: 2
kuigg is on a distinguished road
Default

Quote:
Originally Posted by codefreek View Post
try $lol = + '34';
echo $lol + 3;
now output "37".

but it seemes that you have not answer my question.
kuigg is offline  
Reply With Quote
Old 11-29-2008, 03:46 AM   #4 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

do you want 334 to be the output ?
codefreek is offline  
Reply With Quote
Old 11-29-2008, 03:47 AM   #5 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

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.
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
Old 11-29-2008, 09:28 AM   #6 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Quote:
Originally Posted by t3st View Post
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.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.

Last edited by xenon : 11-30-2008 at 07:44 PM.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
kuigg (11-29-2008)
Old 11-29-2008, 10:34 AM   #7 (permalink)
The Wanderer
 
kuigg's Avatar
 
Join Date: Nov 2008
Posts: 7
Thanks: 2
kuigg is on a distinguished road
Default

Quote:
Originally Posted by xenon View Post
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.
kuigg is offline  
Reply With Quote
Old 11-29-2008, 12:10 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Old 11-29-2008, 12:58 PM   #9 (permalink)
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
Old 12-02-2008, 02:14 AM   #10 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

yeah thats it :)
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:47 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design