When to, and when not to use quotations in PHP

phpBB3 and MOD challenges setup by the staff to test and challenge your phpBB3 coding skills.

Re: When to, and when not to use quotations in PHP

Postby Highway of Life » 13 Feb 2007, 11:05

Lew, you are saying that if a script uses ' for strings, and it takes one second to parse... using " would take 100 seconds to parse... this is not the case.
The difference is actually only about 50%, or 2 times faster to use single quotes... not 100 times faster.

Here is a test I ran to show the difference...
Spoiler:
Code: Select all
<?php
function get_microtime($time)
{
       list (
$usec$sec) = explode(" "$time);
       return ((float) 
$usec + (float) $sec);
}

$start microtime();
$array = array();
for (
$i 0$i 100$i++)
{
    
array_push($array"Star Trek Guide");
}

$end microtime();
$double_quote = (get_microtime($end) - get_microtime($start));
print 
"<pre>double quotes: $double_quote<br>";

unset(
$array);

$start microtime();
$b_array = array();
for (
$i 0$i 100$i++)
{
     
array_push($b_array'Star Trek Guide');
}
$end microtime();
$single_quote = (get_microtime($end) - get_microtime($start));
$result $double_quote $single_quote;
print 
"single quotes: $single_quote<br />difference...: $result</pre>";
?>

Result:
Code: Select all
double quotes: 0.000489950180054
single quotes: 0.000220060348511
difference...: 0.000269889831543


The difference is negligible, but if you use " a lot in your code, you might start to notice a difference.
Again, it's still better to use ', but you can use " when you need to... I still don't think it's good practice to use a lot of \'... unless you have a lot of them... and in that case, you should escape the quotes.
phpBB3 actually uses double quotes on almost every SQL query.
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10




phpBB Academy at StarTrekGuide
Support STG
Using PayPal Donate

Re: When to, and when not to use quotations in PHP

Postby Michaelo » 13 Feb 2007, 11:06

Next day bump!

LEW21, if you use the ? in language file with phpBB3 it will produce a ? not a ?. This is, I assume, a phpBB problem ( Unicode UTF-8 decoding of an Apostrophe ), I note both IE and Firefox show the '?'... Sad but if you change IE to autoselect encoding it selects UTF-8 and displays correctly as too does FF when you select 8859-1... IE may add additional decoding for UTF8 to support several additional characters including the apostrophe...

I note, here it works... I bet its \' in the language file and not ? Clapping

And to think I have an apostrophe in my name... this is incorrect O'Toole and this is correct O?Toole. Now, what's the difference between ' and ? ?
Stargate Portal (Dev Site) phpBB 3.0.9 version II 99%.
Kiss Portal Engine (Dev Site) phpBB 3.0.10 99%.
User avatar
Michaelo    
Supporter
Supporter
 
Posts: 342
Joined: 17 Sep 2006, 09:02
Location: Dublin, Ireland
Favorite Team: Miami Dolphins
Gender: Male
phpBB Knowledge: 9

Re: When to, and when not to use quotations in PHP

Postby A_Jelly_Doughnut » 13 Feb 2007, 19:22

One thing to keep in mind: many (my self included) consider it bad form to do this:
Code: Select all
$foo "moo {$bar['asdf']};

I prefer to do concatenation.
Code: Select all
$foo 'moo' $bar['asdf'];


Single quote (') is an ASCII character. Apostrophe is not. That's the short form of it.
A_Jelly_Doughnut
phpBB Team Member
phpBB Team Member
 
Posts: 543
Joined: 10 Feb 2007, 14:58
Location: 1- 800 - In - The - USA
Gender: Male

Re: When to, and when not to use quotations in PHP

Postby Michaelo » 13 Feb 2007, 19:25

Some info: http://www.fileformat.info/info/unicode ... /index.htm

It appears that there are three different single quotes...
See Also apostrophe U+0027
modifier letter apostrophe U+02BC
heavy single comma quotation mark ornament U+275C
I started off confused and now I am more confused than ever... Scratch
Stargate Portal (Dev Site) phpBB 3.0.9 version II 99%.
Kiss Portal Engine (Dev Site) phpBB 3.0.10 99%.
User avatar
Michaelo    
Supporter
Supporter
 
Posts: 342
Joined: 17 Sep 2006, 09:02
Location: Dublin, Ireland
Favorite Team: Miami Dolphins
Gender: Male
phpBB Knowledge: 9

Re: When to, and when not to use quotations in PHP

Postby Handyman » 13 Feb 2007, 19:36

Michaelo wrote:I started off confused and now I am more confused than ever... Scratch

I'm happy we could help Wink
Go with the first post Highway had in this topic Smile
It makes it all very clear and running the time script Highway had, sometimes double quotes come out faster than single quotes? just depends on the day Wink so the time difference of using them is negligible

I took Highways script and made it loop 1000 times and added up all the numbers and sometimes single is faster, other times double is faster? however, double is faster most of the time.
Here is the last time I refreshed the page
Single = 0.394691944122 ... Double = 0.2978079319 ... Result = 0.0968840122223

in that case, after going through it 1,000 times, double is faster by almost 1/100th of a second? not much either way.
The worst I have seen it for double is slower by almost 2/10ths of a second
and for single is almost 4/10ths of a second.
Spoiler:
Code: Select all

<?php
    
function get_microtime($time)
    {
           list (
$usec$sec) = explode(" "$time);
           return ((float) 
$usec + (float) $sec);
    }
function 
test_quotes()
{
    
$row = array();
    
$start microtime();
    
$array = array();
    for (
$i 0$i 100$i++)
    {
        
array_push($array"Star Trek Guide");
    }
    
    
$end microtime();
    
$double_quote = (get_microtime($end) - get_microtime($start));
    print 
"<pre>double quotes: $double_quote<br>";
    
    unset(
$array);
    
    
$start microtime();
    
$b_array = array();
    for (
$i 0$i 100$i++)
    {
         
array_push($b_array'Star Trek Guide');
    }
    
$end microtime();
    
$single_quote = (get_microtime($end) - get_microtime($start));
    
$result $double_quote $single_quote;
    print 
"single quotes: $single_quote<br />difference...: $result</pre>";
    
$row = array(
        
'double'    => $double_quote,
        
'single'    => $single_quote,
        
'result'    => $result,
    );
    return 
$row;
    unset(
$row);
}
$single $double $result 0;
for (
$i 0$i 1000$i++)
{
    
$row test_quotes();
    
$single += $row['single'];
    
$double += $row['double'];
}
$result $single $double;
echo 
'This is it<br />';
echo 
"Single - $single ... Double - $double ... Result - $result";
?>

A_Jelly_Doughnut wrote:One thing to keep in mind: many (my self included) consider it bad form to do this:
Code: Select all
$foo "moo {$bar['asdf']};


phpBB uses that a lot in the new phpBB3.
Here is a bit of code from posting.php

Code: Select all

$sql 
'UPDATE ' POSTS_TABLE "
SET post_time = $current_time
WHERE post_id = {$post_data['topic_last_post_id']}
AND topic_id = $topic_id"
;
$db->sql_query($sql);


As Highway pointed out, they use double quotes on almost every sql query, and using
Code: Select all
"{$foo['bar']}";
is not bad coding? it just saves space and looks cleaner.
And you don't have to keep breaking out of quotes to get it done.
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7454
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: When to, and when not to use quotations in PHP

Postby Highway of Life » 13 Feb 2007, 19:52

I should add, that using ? is actually desired over ' in the language files, since the language files are saved as UTF-8.
For note, StarTrekGuide language files are using 85 ? (that key is getting easier to hit all the time. Smile ) apostrophes in 71 language files.
If your text editor does not support UTF-8 OR saves files as Western by default, it will cause a problem with the language files, so be sure to save those files as UTF-8.

For more info, please see the olympus wiki Coding Guidelines (in progress Smile )
I have not added the section that deals with quotations, but a full explanation will be in there when I get to that part.
At that time, I'll spell it out in complete detail for everyone. Smile
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10

Re: When to, and when not to use quotations in PHP

Postby Michaelo » 13 Feb 2007, 20:21

There are two issues re apostrophe?s...
The first is the way your editor saves files and the second is the way your browser view pages...

Easy to fix... we should all use UTF-8 from now on (but I still don't have a key for apostrophe Sad)
Mike
Stargate Portal (Dev Site) phpBB 3.0.9 version II 99%.
Kiss Portal Engine (Dev Site) phpBB 3.0.10 99%.
User avatar
Michaelo    
Supporter
Supporter
 
Posts: 342
Joined: 17 Sep 2006, 09:02
Location: Dublin, Ireland
Favorite Team: Miami Dolphins
Gender: Male
phpBB Knowledge: 9

Re: When to, and when not to use quotations in PHP

Postby Highway of Life » 13 Feb 2007, 23:48

There must be a way!

Check here, this will help you a little... http://www.fileformat.info/tip/microsof ... nicode.htm

Do you have any key combinations for things like these?
Code: Select all
Option...
`¡?£¢?§¶?ªº??«???ø?¨¥?®´??åß??©???¬?æ÷??µ???ç??

Option + Shift...
`???????°·??±»???Ø?¨Á??´??ÅÍÎÏ?ÓÔ?ÒÚÆ¿?¯Â???Ç?¸
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10

Re: When to, and when not to use quotations in PHP

Postby Michaelo » 14 Feb 2007, 03:47

No options Key... I can produce all ascii using Alt (num Lock on) and entering acsii code in numeric pad...
All other combinations including Alt, Ctrl, Shift (or combinations) and any key are fine but none produce the (real) apostrophe... Sad
Stargate Portal (Dev Site) phpBB 3.0.9 version II 99%.
Kiss Portal Engine (Dev Site) phpBB 3.0.10 99%.
User avatar
Michaelo    
Supporter
Supporter
 
Posts: 342
Joined: 17 Sep 2006, 09:02
Location: Dublin, Ireland
Favorite Team: Miami Dolphins
Gender: Male
phpBB Knowledge: 9

Re: When to, and when not to use quotations in PHP

Postby Highway of Life » 15 Feb 2007, 01:44

ChrisRLG wrote:Try using 'alt' with the numeric keypad.

'alt' 0145 = ?
'alt' 0146 = ?
'alt' 0147 = ?
'alt' 0148 = ?

All the codes are available from the 'character map' application.
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10

PreviousNext

Return to phpBB3 Challenges at phpBB Academy

Who is online

Users browsing this forum: No registered users and 0 guests