Single Quotes vs Double Quotes in PHP

In PHP, strings in double quotes are parsed and evaluated at run time, strings in single quotes are not parsed, what you have in single quotes will show up as it is. For example, in the code snippet below

<?php
$greeting="Hello World!!!";
echo 'Greeting: $greeting\n'.'<br/>'; // prints Greeting: $greeting\n
echo "Greeting: $greeting\n";// prints Greeting: Hello World!!!

Notice, with the single quotes, the variable $greeting and the new line \n got printed as it is. For the double quotes, the variable $greeting and the new line \n were evaluated, the actual string stored in the $greeting was printed.

Rule of thumb, if the string you are composing doesn’t require any variables, then use single quotes better for performance because there is no parsing or evalation with single quotes.

Search within Codexpedia

Custom Search

Search the entire web

Custom Search