Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Echo with single and double quote in PHP [duplicate]

Tags:

php

echo

quotes

Possible Duplicate:
Difference between single quote and double quote string in php

Hi,

What is the difference between echo 'Test Data'; and echo "Test Data"; in PHP.

Both statements give me same output.

like image 775
Pradip Avatar asked Mar 11 '11 13:03

Pradip


People also ask

Can we use both single and double quotes in PHP?

In PHP, people use single quote to define a constant string, like 'a' , 'my name' , 'abc xyz' , while using double quote to define a string contain identifier like "a $b $c $d" . echo "my $a"; This is true for other used of string.

How do you escape a double quote in a title attribute?

Using " is the way to do it.


2 Answers

I believe that double quotes allow variables to be replaced by the value :

echo "test = $test";

displays :

test = 2

echo 'test = $test';

displays :

test = $test

like image 167
Réjôme Avatar answered Sep 20 '22 07:09

Réjôme


Single-quoted strings will not have variables or escape sequences expanded by the interpreter, whereas double-quoted strings will - look at the different output of:

$foo = 'bar';
echo 'This is a $foo';
echo "This is a $foo";

Single-quoted strings are hence marginally 'better' to use as the interpreter won't have to check the contents of the string for a variable reference.

like image 42
Gavin Ballard Avatar answered Sep 20 '22 07:09

Gavin Ballard