Single quote vs Double quote in php with examples
Almost all PHP programmers will get confusion on this atleast one time in their development carrier,
Single Quote:
1. Values will not be interpreted (\n will be displayed as \n only instead of printing in new line).
2. Values will not be evalued ($val will be printed $val only, instead of printing the $val variable value).
Eg:
Sample Program:
[php gutter=”false”]
<?php
$val = 2;
echo ‘$val’;
echo "\n";
echo ‘hello \n world’;
?>
[/php]
Output:
[plain gutter=”0″]
$val
hello \n world
[/plain]
Double Quote:
1. Values will be interpreted (\n will be printing in new line).
2. Values will be evalued ($val will be printing the $val variable value).
Eg:
[php gutter=”false”]
<?php
$val = 2;
echo "$val";
echo "\n";
echo "hello \n world";
?>
[/php]
Output:
[plain gutter=”0″]
2
hello
world
[/plain]
So When to use which ?
Single quote is recommended to use declaring/defining constants and double quote can be used while we need to interpret/evalute the values.
PHP Recommended Books: