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
$val = 2;
echo '$val';
echo "\n";
echo 'hello \n world';
?>

Output:

$val
hello \n world

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
$val = 2;
echo "$val";
echo "\n";
echo "hello \n world";
?>

Output:

2
hello
 world

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:

339 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *