Double quote (") would handle variables inside the string.echo "Hello $name\n";
The output would be Hello John if $name = "John".
A line break (not shown in browser) is added with \n.
Single quote (') would not handle variables and special characters inside the string.echo 'Hello $name\n';
The output would be Hello $name\n.
When single quote is needed in the string, you can use double quote to define the string.
echo "Hello 'John'\n";
or vice versa.
echo 'Hello "John"\n';
<?php
$name = "John";
echo "Hello $name\n"; // Hello John
echo 'Hello $name\n'; // Hello $name\n
$var = 10;
echo "Value of var is $var\n"; // Value of var is 10
echo 'Value of var is $var\n'; // Value of var is $var\n
?>
<?php
$name = "John";
echo 'Hello ' . $name . "\n"; // Hello John
echo 'Hello $name \n'; // Hello $name \n
echo "Hello $name \n"; // Hello John
$var = 10;
echo 'Value of var is ' . $var . "\n"; // Value of var is 10
echo 'Value of var is $var \n'; // Value of var is $var \n
echo "Value of var is $var \n"; // Value of var is 10
?>
$firstName = "John";
$lastName = "Doe";
$age = 25;
$city = "New York";
1. Use ONLY single quote (') to echo the following:
Hello John Doe, you are 25 years old and you live in New York.
2. Use ONLY double quote (") to echo the following:
Hello John Doe, you are 25 years old and you live in New York.
And as double quote has to handle variables, in theory it is slower than single quote.