Skip to content Skip to sidebar Skip to footer

What Is The Difference Between Double And Single Quotes In Php?

Possible Duplicate: Difference between single quote and double quote string in php I am new to PHP and in programming i have seen use of both '' and ' '. What is the difference

Solution 1:

You want to keep the text inside of your string:

$abc_output .='<ahref="abc.com">Back to Main Menu</a>';

The difference between ' and " is that you can embed variables inside of a double quoted string.

For example:

$name = 'John';
$sentence = "$name just left"; // John just left

If you were to use single quotes, then you'd have to concatenate:

$name = 'John';
$sentence = $name.' just left'; // John just left

PS: Don't forget that you always have to escape your quotes. The following 2 are the same:

$double = "I'm going out"; // I'm going out
$single = 'I\'m going out'; // I'm going out

Same applies the other way around:

$single = 'I said "Get out!!"'; // I said "Get out!!"
$double = "I said \"Get out!!\""; // I said "Get out!!"

Solution 2:

Double quotes allow additional expressions, such as "$variable \n", which single quotes don't. Compare:

$variable = 42;
echo"double: $variable,\n 43\n";
echo'single: $variable,\n 43';

This outputs:

double: 42,
 43
single: $variable,\n 43

For more information, refer to the official documentation.

Solution 3:

Text in double quotes are parsed.

For example:

$test = 'something';

print('This is $test');
print("This is $something");

would result in:

Thisis$testThisis something

If you don't need the string to be parsed you should use single quotes since it's better performance wise.

In your case you need to do:

$abc_output .='<a href="abc.com">Back to Main Menu</a>';
echo$abc_output;  

Or you will get an error.

The Back to Main Menu wasn't in the string.

Post a Comment for "What Is The Difference Between Double And Single Quotes In Php?"