It may all seem a little trivial to be discussing which kinds of quotes to be using. However, there is a difference in PHP and it's important to know what those differences are. Really, it will save a lot of bills on boxes of aspirins in the long-run.
There are 3 types of quotes, but we will be looking at the 2. I will list the 3 below just so you know for future reference.
'This is the first type of quotes' - Everything inside these are taken literally.
"This is the second type of quotes" - These can include variables which will be resolved to show the data they contain.
`This is the third type of quotes` - Used only for executing commands on the system. Do NOT use these unless you wish to execute system commands. Technically called the backtick operator.
The first 2 are the types of quotes we will be concentrating on. For readability I use the single quotes for all text and the double quotes for new lines (of course unless I use PHP's constant, PHP_EOL). The new line and carriage return characters (\n and \r) will not be resolved inside single quotes and will be displayed literally. They MUST be encapsulated in double quotes for them to appear as new lines.
Take the following examples as a good reference to the difference between the 2:
$szBuddha = 'Buddha'; echo 'This is $szBuddha';
Result: This is $szBuddha
$szBuddha = 'Buddha'; echo "This is $szBuddha";
Result: This is Buddha
As you can see the former outputs the variable as it is, not resolving its value of Buddha, whilst the latter does.
Personally, I always use the single quotes unless I need to output any variables or new line characters. I've always been brought up to never encapsulate PHP variables in any types of quotes and therefore I always concatenate them like so:
$szFoo = 'Foo never never be shown without ' . $szBar;
To me it looks a lot neater than:
$szFoo = "Foo never never be shown without $szBar";
Naturally it's entirely up to your good selfs which you use, but placing new line characters and variables inside single quotes is definitely the way to go if you're a big fan of headaches.
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Last edited by Wildhoney : 09-11-2007 at 10:14 PM.