|
|
|
Most of us use only Double Quotes ["] when writing PHP code. it seems like the trivial thing to do and most examples out there show this kind of syntax. However, there is also a 2nd way to go and it's the better way to go. Instead of Double Quotes, just use Simple Quotes.
Instead of :
<?
Echo "Visit http://www.weberblog.com";
?>
You can Use :
<?
Echo 'Visit http://www.weberblog.com';
?>
Both examples would work but one may ask, so what is the big deal? why is it better?
In the above example there is no difference but have a look at this example :
Double Quotes way :
<?
Echo "<TABLE BORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"0\">";
?>
Single Quotes way :
<?
Echo '<TABLE BORDER="1" CELLSPACING="0" CELLPADDING="0">';
?>
In this example you can see that we don't need to add escape characters for each Double Quote we want to echo as HTML. This, by its self is a good enough reason to migrate, however, a 2nd reason is performance. Using Single Quotes is always at least as fast as Double Quotes and in some cases faster by hundreds of percents. The next test is was taken from http://www.blueshoes.org/phpBench.php :
double (") vs. single (') quotes
Is a there a difference in using double (") and single (') quotes for strings. Call 1'000x
+ 101 % 1: single (') quotes. Just an empty string: $tmp[] = ''; Total time: 3[ms]
+ 100 % 2: double (") quotes. Just an empty string: $tmp[] = ""; Total time: 3[ms]
+ 111 % 3: single (') quotes. 20 bytes Text : $tmp[] = 'aaaaaaaaaaaaaaaaaaaa'; Total time: 3[ms]
+ 118 % 4: double (") quotes. 20 bytes Text : $tmp[] = "aaaaaaaaaaaaaaaaaaaa"; Total time: 3[ms]
+ 115 % 5: single (') quotes. 20 bytes Text and 3x a $ : $tmp[] = 'aa $ aaaa $ aaaa $ a'; Total time: 3[ms]
+ 461 % 6: double (") quotes. 20 bytes Text and 3x a $ : $tmp[] = "aa $ aaaa $ aaaa $ a"; Total time: 13[ms]
+ 113 % 7: double (") quotes. 20 bytes Text and 3x a \$ : $tmp[] = "aa \$ aaaa \$ aaaa \$ a"; Total time: 3[ms]
Conclusion:
Single and double quoted strings behave almost the same with one exception: Don't use the a lonely ($) in double quoted string unless you want to reference a PHP-var; or use (\$).
Note : When working with special formatting characters such as \n \r \t etc... Single Quotes are somewhat problematic because they ignore these formatting codes. What you need to do in case you need these formatting codes is :
<?
Echo 'check out http://www.weberblog.com' . "\n\r" . 'for the best PHP & MySQL Web Logs on the net';
?>
|
|
| Differences between two files Categories : PHP, Filesystem, Tip | | | Query2Report : Generating Html, Pdf and Csv Reports from SQL Query Categories : PHP, PHP, HTML, PDF, Excel | | | Retrieve text from table and email to your e-
address in pipe delimited format. Categories : PHP, MySQL | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | A PHP function to encrypt and decrypt a number or string or a combination of the two. Categories : PHP, Encryption, Security | | | Using $PHP_AUTH_USER and $PHP_AUTH_PW to authenticate. Categories : Authentication, PHP | | | file class , uploade file , download file already uploaded on another website Categories : PHP, PHP Classes, Filesystem, Web Services | | | Authorize.net AIM Interface Class v1.0.0 Categories : PHP, PHP Classes, Ecommerce, Payment Gateways | | | Cut your MySQL Connections to 1 line of code Categories : PHP, Beginner Guides, Databases, MySQL | | | A simple class with some HTML output functions that would come in handy for consistent page layout etc. Categories : PHP, PHP Classes, HTML and PHP, HTML, Navigation | | | readline -- Reads a line Categories : PHP, PHP Functions, Readline | | | a function that builds an HTML select list from any mysql table. Categories : PHP, MySQL, HTML and PHP | | | Math operations on big numbers Categories : PHP, Math. | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | |
| | | | jefferis peterson wrote : 1026
I think one of the hardest things to remember is using single and double quotes inside php inside javascript or html context. Everything always gets reversed. It is a real pain. Is there a mnemonic device for remembering this ? ??? Or a better way?
| | | | Timothy Bolton wrote : 1027
The simplest mnemonic I can think of is:
`S`ingle quotes for `S`cript.
That then only leaves the double quotes for HTML code.
I think ...!
| | | | Sarah King wrote : 1034
In addition use braces { around variable names in double quoted strings for faster and more accurate parsing. Braces allow you to embed arrays and objects into a string. eg
echo "Hello {$visitor->name}, welcome back";
A common error I see is to use double quotes around a variable when passing it to mysql or mail.
<?php
$email = `tom@mysite.com`;
mail("$email",`subject`,`body`);
?>
email is already a string so no need to force PHP to parse it again.
| | | | Joseph Crawford wrote :1195
one thing people like to do is add a \n at the end of thier html code that php outputs, both for clarity and debugging.
<?
Echo `<TABLE BORDER="1" CELLSPACING="0" CELLPADDING="0">\n`;
?>
will not work... the \n will be parsed as normal text
also
<?
Echo `<TD>My Name is $name</TD>\n`;
?>
will not work, in order to get variables or special charachters to work withing php output you need to have double quoted strings, this doesnt alway mean messy "\" stuff like that, for example.
<?
Echo "<TABLE BORDER=`1` CELLSPACING=`0` CELLPADDING=`0`>\n";
?>
and
<?
Echo "<TD>My Name is {$name}</TD>\n";
?>
will work just fine, also you dont need the {} around $name but it helps parse things faster, i always use them so that when i use an object etc.. i dont get errors as you need the {} to use them.
| |
|
|
|