Difference between single and double quotation marks in PHP

68

What is the difference between single quotes and double quotation marks in PHP?

Yesterday I was working with a JSON string from google calendar I used explode('\n', .. to separate a string.

When I used the explode in "Mon Jul 7, 2014 \n\u003cbr" , with " it gave me:

array(1) { [0]=> string(25) "Mon Jul 7, 2014 \u003cbr" }

When I used the explode in 'Mon Jul 7, 2014 \n\u003cbr' , with ' it gave me:

array(2) { [0]=> string(16) "Mon Jul 7, 2014 " [1]=> string(8) "\u003cbr" }

The above is just a practical example that I came across. Would it be interesting to know the differences in PHP between ' and " ?

    
asked by anonymous 06.02.2014 / 20:24

5 answers

56

Simple Quotes

The PHP documentation defines that Simple quotes are simple, unprocessed literals. The only exceptions to escape are the single quotes (% with%) and the slash (% with%).

Example:

$teste = 1;
echo 'A caixa d\'água está vazia. \ $teste';

The output will be:

  

The water tank is empty. \ test

Double quotes

Double quotes strong> will be processed, supporting more escape characters, such as '\'' , '\' , \n and others. In addition, variables will be expanded (or interpolated), for example: \r .

Example:

$teste = 1;
echo "A caixa d\'água está vazia. \ $teste $teste2";

The output will be:

  

The water tank is empty. \ 1 $ test2

Note that \t is not defined, so the String will not be expanded.

Heredoc and Nowdoc

In addition to these two ways of representing strings in the code, there are still heredoc and the nowdoc , which allow you to add String blocks more easily. Note that "Meu nome é $nome!" works like double quotes, whereas $teste2 works like single quotes.

Example of heredoc :

$interpolacao = 'INTERPOLAÇÃO';

$str = <<<EOD
Exemplo de String
$interpolacao funciona aqui dentro
EOD;

Output:

  

String example

     

INTERPOLATION works here

Example of nowdoc :

$interpolacao = 'INTERPOLAÇÃO';

$str = <<<'EOD'
Exemplo de String
$interpolacao não funciona aqui dentro
EOD;

Output:

  

String example

     

$ interpolation does not work here in

    
06.02.2014 / 20:35
13

The difference between single quotation marks and double quotation marks is in use.

Double quotes allow:

a) what variables are interpreted within it:

$nome = "bob";
echo "meu nome é $nome";

b) Use of leaks like: \n ,\r, \t, \v, \e, \f, \, \$, \" ;

c) invoking methods / properties using the complete syntax , that is, {} :

echo "{$pessoa->getNome()}";

Simple quotes allow:

a) only escapes \' and \ :

echo 'I\'m here'; //i'm here
echo 'I\'m \r\n here'//não vai gerar a quebra de linha.
    
06.02.2014 / 20:58
10

As already mentioned in the other answers, single quotation marks are literals and double quotation marks are interpretive.

By having a different memory consumption, an important point in choosing whether to use single or double quotation marks is the content that will be added to them.

If you are going to use plain text, you may want to use the single quotation marks that the memory consumption will be less as there is no need for PHP to attempt to interpret the content.

$simples = 'Meu nome é Raul'; //consome menos memória
$duplas = "Meu nome é Raul"; //consome mais memória

Now, if you need to concatenate this text with variables it is better to use the double quotes instead of using period (.).

$simples = 'Meu nome é '.$nome; //consome mais memória
$duplas = "Meu nome é $nome"; //consome menos memória
    
06.02.2014 / 23:09
8

Single quotes

Recognize the content literally, everything will be treated as text:

<?php
$valor = 10;
$variavel = 'meu número é $valor';
echo $variavel;
// Saida: meu número é $valor

Double quotes

Recognizes escape characters \n \t \r and variáveis in content:

<?php
$valor = 10;
$variavel = "meu número é $valor";
echo $variavel;
// Saida: meu número é 10
    
06.02.2014 / 20:33
6

In PHP, the single quotation mark is not interpretive, it is a string and only that, the double is already processed, so it can contain variables that will be converted at the time of executing the string.

For example:

$foo = 'bar';

echo "Variavel $foo"; // Variavel bar
echo 'Variavel $foo'; // Variavel $foo
    
06.02.2014 / 20:32