Re: [PHP] String format problem
In fact, this is a poor example since the difference gets larger with longer string and more arguments. When you use dots, the interpreter has to actually concatenate the string, looking for memory to do so and freeing it up afterwards. This takes time. With commas, each argument is sent to the output stream as soon as it is found, no further processing is needed in between. I have been wondering about this topic for a few months now, so thanks for this fascinating explanation. Is this the only difference between using . and , as concatenation operators Then the single vs. double quotes: echo 'uno ' , ' dos ' , ' tres ': 0.94 echo uno , dos , tres : 6.76 Notice that when variables are involved, the difference in between echoing with arguments separated with commas and separated with dots is more than 9 times faster for the commas.Using double quotes with variable expansion is almost 4 times slower than the commas, but is still faster than concatenating them externaly with dots. Using heredoc-style strings is not so bad compared to double quotes. Never heard of heredoc before. What is it for? I have read http://uk.php.net/types.string and can only imagine that it is for laying out complex or long strings more clearly So, if you are sending out the rule would be: Use echo, not print. Separate arguments with commas. Now, if you are not using echo, for example, concatenating to a variable, the best is to use variable expansion inside double quoted or heredoc strings. Concatenating with dots is more than twice as slow. Satyam -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] String format problem
On 31 August 2005 16:56, [EMAIL PROTECTED] wrote: $varname = '$firstname $lastname told me to find the file in folder C:\newtext\'; echo $varname; Yields.. $firstname $lastname told me to find the file in folder C:\newtext\ Actually, that'll give you an error as well, since \' is also an escape sequence (to get a single quote into a single-quoted string!). Currently, \' and \\ are the only escape sequences recognized in a single-quoted string, although this may change with the advent of native Unicode in PHP 6. Bottom line: always double your backslashes, even in single-quoted strings, to be completely safe and future-proof. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String format problem
Wow! Fantastic rundown Satyam! Thanks for posting such a complete analysis. I had no idea that you could use commas instead of periods to join multiple strings much less do it without concatinating, that's very interesting. I don't think I've ever seen that used in any sample code before. I'm wondering if it's common enough that it'll stay around or if it'll become deprecated at some point (or is it new to PHP5? I havn't upgraded yet). I'd hate to start using it then have to go back and fix all my code when it gets cut. (Any thoughts/info anyone?) I feel somewhat vindicated now at my use of echo blah $something blah versus print 'blah ' . $something . ' blah', which is the standard used in the code I inherited. hah Anyway, thanks again Satyam for the rundown. It's greatly appreciated. -TG = = = Original message = = = [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] To elaborate on Philip's response (which is correct)... [] I've read never use double quotes unless you have to because it could potentially speed up PHP a little because it won't be trying to interpret every string. I once did some trials with thousand repetitions of segments of code with different alternatives, so let me go through all of them since I have the numbers anyway. The original was in Spanish so I'll just give the results. The question started with using echo or print and, as the manual says, echo is, indeed, faster: echo uno $f tres : 3.04 print uno $f tres : 3.70 The numbers after the colons are the execution times of one against the other for a lot of repetitions, anyhow, they should be read as relative to one another. Next, most people don't know that echo accepts multiple arguments separated by commas. Separating the arguments with commas is faster. echo 'uno ' . ' dos ' . ' tres ': 1.32 echo 'uno ' , ' dos ' , ' tres ': 0.94 In fact, this is a poor example since the difference gets larger with longer string and more arguments. When you use dots, the interpreter has to actually concatenate the string, looking for memory to do so and freeing it up afterwards. This takes time. With commas, each argument is sent to the output stream as soon as it is found, no further processing is needed in between. Then the single vs. double quotes: echo 'uno ' , ' dos ' , ' tres ': 0.94 echo uno , dos , tres : 6.76 Once again, these are relative times but it means using double quotes is 7 times slower than single quotes Several different alternatives with variables involved: echo 'uno ' . $f . ' tres ': 7.38 echo 'uno ' , $f , ' tres ': 0.80 echo uno $f tres : 3.04 echo EOT uno $f tres EOT: 3.29 Notice that when variables are involved, the difference in between echoing with arguments separated with commas and separated with dots is more than 9 times faster for the commas.Using double quotes with variable expansion is almost 4 times slower than the commas, but is still faster than concatenating them externaly with dots. Using heredoc-style strings is not so bad compared to double quotes. So, if you are sending out the rule would be: Use echo, not print. Separate arguments with commas. Now, if you are not using echo, for example, concatenating to a variable, the best is to use variable expansion inside double quoted or heredoc strings. Concatenating with dots is more than twice as slow. Satyam ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String format problem
Please keep in mind you are speaking about microseconds of performance improvements. Optimising on this level is complete and utter nonsense. Using output buffering may give your application a much higher performance boost. And then you can still use a cache (opcode cache, page cache, ...) which greatly enhances the performance. AllOlli References: - Buffering: http://php.net/ob_start - Caching for starters: http://derickrethans.nl/files/perf-hungary.pdf -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String format problem
On 8/31/05, Ahmed Abdel-Aliem [EMAIL PROTECTED] wrote: hi i have a problem when i am formating a string the problem is it converts the \n in the string to a new line here is the code ? $Text = D:\AppServ\www\intranet\admin\store\nodirectory\sub; $Replace = D:\AppServ\www\intranet\admin\store; $with = http://localhost/bank/admin/store;; Don't use double quotes if you don't want escape sequences to be expanded. $Text = 'D:\AppServ\www\intranet\admin\store\nodirectory\sub'; $Replace = 'D:\AppServ\www\intranet\admin\store'; $with = 'http://localhost/bank/admin/store'; -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String format problem
hi i have a problem when i am formating a string the problem is it converts the \n in the string to a new line here is the code ? $Text = D:\AppServ\www\intranet\admin\store\nodirectory\sub; $Replace = D:\AppServ\www\intranet\admin\store; $with = http://localhost/bank/admin/store;; //$doc = str_replace ($Replace, $with, $Text); $doc = str_replace( '\\', '/', str_replace( $Replace, $with, $Text ) ); echo $doc; //i need $Doc to look like this http://localhost/bank/admin/store/nodirectory/sub //but it outputs http://localhost/bank/admin/store odirectory/sub Replace your double quotes with single quotes... that is: $Text = 'D:\AppServ\www\intranet\admin\store\nodirectory\sub'; $Replace = 'D:\AppServ\www\intranet\admin\store'; $with = 'http://localhost/bank/admin/store'; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String format problem
To elaborate on Philip's response (which is correct)... Anything in double quotes ( ) will be interpreted by PHP before figuring out what the actual value is. Items like \n, \t, etc are therefore converted to a newline (\n) or a tab (\t) before assigning to the variable. Variables within the double quotes are also evaluated before assigning. So.. $firstname = Bob; $lastname = Smith; $varname = $firstname $lastname told me to find the file in folder C:\newtext\; echo $varname; Will yield: Bob Smith told me to find the file in folder C: ewtext; Actually it'll give you an error because the double quotes aren't properly terminated.. \ is also interpreted. Whereas.. $varname = '$firstname $lastname told me to find the file in folder C:\newtext\'; echo $varname; Yields.. $firstname $lastname told me to find the file in folder C:\newtext\ In this case, you have two options to get what you want: $varname = $firstname $lastname told me to find the file in folder C:\\newtext\\; ..or.. $varname = $firstname . ' ' . $lastname told me to find the file in folder C:\newtext\'; I've read never use double quotes unless you have to because it could potentially speed up PHP a little because it won't be trying to interpret every string. I'm not sure exactly how much savings this is going to give you so I drop this in the whatever your style dictates category. I prefer double quotes because I think it visually delineates the code better and I think $firstname $lastname looks tidier than $firstname . ' ' . $lastname even though I do consider it slightly looser coding practices. One's better for the human, one's better for the machine (imo) and since I'm a human. I think. :) -TG = = = Original message = = = hi i have a problem when i am formating a string the problem is it converts the \n in the string to a new line here is the code ? $Text = D:\AppServ\www\intranet\admin\store\nodirectory\sub; $Replace = D:\AppServ\www\intranet\admin\store; $with = http://localhost/bank/admin/store;; //$doc = str_replace ($Replace, $with, $Text); $doc = str_replace( '\\', '/', str_replace( $Replace, $with, $Text ) ); echo $doc; //i need $Doc to look like this http://localhost/bank/admin/store/nodirectory/sub //but it outputs http://localhost/bank/admin/store odirectory/sub Replace your double quotes with single quotes... that is: $Text = 'D:\AppServ\www\intranet\admin\store\nodirectory\sub'; $Replace = 'D:\AppServ\www\intranet\admin\store'; $with = 'http://localhost/bank/admin/store'; ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String format problem
[EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] To elaborate on Philip's response (which is correct)... [] I've read never use double quotes unless you have to because it could potentially speed up PHP a little because it won't be trying to interpret every string. I once did some trials with thousand repetitions of segments of code with different alternatives, so let me go through all of them since I have the numbers anyway. The original was in Spanish so I'll just give the results. The question started with using echo or print and, as the manual says, echo is, indeed, faster: echo uno $f tres : 3.04 print uno $f tres : 3.70 The numbers after the colons are the execution times of one against the other for a lot of repetitions, anyhow, they should be read as relative to one another. Next, most people don't know that echo accepts multiple arguments separated by commas. Separating the arguments with commas is faster. echo 'uno ' . ' dos ' . ' tres ': 1.32 echo 'uno ' , ' dos ' , ' tres ': 0.94 In fact, this is a poor example since the difference gets larger with longer string and more arguments. When you use dots, the interpreter has to actually concatenate the string, looking for memory to do so and freeing it up afterwards. This takes time. With commas, each argument is sent to the output stream as soon as it is found, no further processing is needed in between. Then the single vs. double quotes: echo 'uno ' , ' dos ' , ' tres ': 0.94 echo uno , dos , tres : 6.76 Once again, these are relative times but it means using double quotes is 7 times slower than single quotes Several different alternatives with variables involved: echo 'uno ' . $f . ' tres ': 7.38 echo 'uno ' , $f , ' tres ': 0.80 echo uno $f tres : 3.04 echo EOT uno $f tres EOT: 3.29 Notice that when variables are involved, the difference in between echoing with arguments separated with commas and separated with dots is more than 9 times faster for the commas.Using double quotes with variable expansion is almost 4 times slower than the commas, but is still faster than concatenating them externaly with dots. Using heredoc-style strings is not so bad compared to double quotes. So, if you are sending out the rule would be: Use echo, not print. Separate arguments with commas. Now, if you are not using echo, for example, concatenating to a variable, the best is to use variable expansion inside double quoted or heredoc strings. Concatenating with dots is more than twice as slow. Satyam -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] String format problem
hi i have a problem when i am formating a string the problem is it converts the \n in the string to a new line here is the code ? $Text = D:\AppServ\www\intranet\admin\store\nodirectory\sub; $Replace = D:\AppServ\www\intranet\admin\store; $with = http://localhost/bank/admin/store;; Use single quotes instead of double quotes: $Text = 'D:\AppServ\www\intranet\admin\store\nodirectory\sub'; $Replace = 'D:\AppServ\www\intranet\admin\store'; $with = 'http://localhost/bank/admin/store'; JM -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php