Rachel
Rachel

Reputation: 103447

What difference does it makes when I use '' against ""?

What difference does it makes when I use '' against "" ?

For example:

$example = 'Merry Christmas in Advance';
$eg = "Merry Christmas";

echo "$example";
echo '$example';


echo "$eg";
echo '$eg';

What would the output for each echo statements and what can we infer about '' vs "" in PHP ?

Upvotes: 0

Views: 97

Answers (4)

zombat
zombat

Reputation: 94177

$example = 'Merry Christmas in Advance';
$eg = "Merry Christmas";

echo "$example";
echo '$example';

echo "$eg";
echo '$eg';

would produce:

Merry Christmas in Advance$exampleMerry Christmas$eg

Single quoted strings are handled literally. No special characters (such as \n) or variables are interpolated.

Double quoted strings will interpolate your variables and special characters and render them accordingly.

Upvotes: 6

Gal
Gal

Reputation: 23662

You can also include variables inside double quotes and those will be interpreted as variables, rather than strings

So:

$variable = 1;
echo 'this $variable' ==> will output 'this $variable'
echo "this $variable" ==> will output 'this 1'

Upvotes: 2

Alan Haggai Alavi
Alan Haggai Alavi

Reputation: 74252

Double quotes interpolate variables.

Upvotes: 0

danben
danben

Reputation: 83250

Single-quoted variables take input literally, double-quotes interpret escape sequences for special chars and expand variables.

You can see some good examples here: http://php.net/manual/en/language.types.string.php

Note that SOME escape sequences are still interpreted within single quotes. Example:

// Outputs: Arnold once said: "I'll be back"

echo 'Arnold once said: "I\'ll be back"';

Upvotes: 3

Related Questions