PHP and MySQL Code

Start typing to search for PHP and MySQL Code Snippets and Articles Search

Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
Title : Single Quotes vs Double Quotes in PHP
Categories : PHP, Syntax, Tip
Boaz Yahav
Date : Oct 25th 2003
Grade : 3 of 5 (graded 6 times)
Viewed : 51592
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Boaz Yahav
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 

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";

You can Use :

Echo 'Visit';

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 :


Single Quotes way :


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 :

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]

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' . "\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
Directory Listing To XML : Outputs XML File of a Given Directory Listing
Categories : PHP, PHP Classes, XML, Directories
How to Get a character array from a string
Categories : PHP, Strings, Arrays
decoct -- Decimal to octal
Categories : PHP, PHP Functions, Math.
Stronghold 2.3 PHP3RC4 PHP3 COMPILE FAILURE parse error httpd.h
Categories : Installation, PHP
Problem with Database Connectivity in PHP and MS Access? Don't worry, The easy way is......
Categories : ADO, PHP, Databases
STR - a Perl-like string manipulator class - The str class provides 4 perl-like methods for manipulating strings and other scalar variables.
Categories : PHP, PHP Classes, Perl, Strings
Simple PHP cookie counter
Categories : PHP, Cookies, Beginner Guides
Array Insertion
Categories : PHP, PHP Classes, Arrays
PHPRecommend v1.0 - Recommend this page to a friend script written in PHP. Easy to install
Categories : PHP, URLs, Complete Programs, Email, Site Planning
Weighted Random - Random Scripts usually chose one out of each item, and each item have an equal chance to be chosen. But what if you want an item to be chosed more frequently than other?
Categories : PHP, Math., Arrays
Global Dump Highlighted
Categories : PHP, Variables, Global Variables
This functions makes it easy to use session-variables known from ASP. With one Cookie the array "session" will save and restore from a db-record. In this version MySQL is used but it's should very easy to change
Categories : PHP, Arrays, Cookies, MySQL, Databases
What is the best way to split a string that consists of two bits of data seperated by whitespace?
Categories : Regexps, Strings, PHP
 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-&gt;name}, welcome back";

A common error I see is to use double quotes around a variable when passing it to mysql or mail.
$email = `[email protected]`;
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.


will not work... the \n will be parsed as normal text


Echo `&lt;TD&gt;My Name is $name&lt;/TD&gt;\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 "&lt;TD&gt;My Name is {$name}&lt;/TD&gt;\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.