PHP - Single Quotes and Double Quotes

PHP allows you to define string values using single quotes or double quotes.

The most significant difference between these two is that PHP parses a string enclosed in double quotations. Any variable in double quotes are expanded.


$var = 'tips';
echo 'Online Business' . $var; // Results in 'Online Business Tips'
echo "Online Business $var"; // Results in 'Online Business Tips'
echo 'Online Business $var'; // Results in 'Online Business $var' ($var in single quotes is not parsed.)

There are a couple of tips to remember when deciding to use single or double quotations.
1. Using single quotations results in slightly faster execution of PHP scripts.
2. Consider code readability.

in PHP scripts
echo 'They\'re looking for SEO tips.'; // Single quotes (escape needed)
echo "They're looking for SEO tips."; // Double quotes

echo '"Black Hat" search engine optimization'; // Single quotes
echo "\"Black Hat\" search engine optimization"; // Double quotes (escape needed)

in HTML pages
echo '<a href="' . $url . '">' . $name . '</a>'; // Single quotes
echo "<a href=\"$url\">$name</a>"; // Double quotes (escape needed)

No Comments

Leave a comment