Single quote vs Double quote in php with examples

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

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:

748 total views, 2 views today

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.