I am always in doubt on how to concatenate strings with variables in PHP. I have some difficulty understanding the question of double quotes.
I would like an answer that clarifies why you use one or the other and how to put it into practice.
I am always in doubt on how to concatenate strings with variables in PHP. I have some difficulty understanding the question of double quotes.
I would like an answer that clarifies why you use one or the other and how to put it into practice.
The concatenation operator of strings is the point .
For example:
$ddd = '34';
$fone = '32105691';
echo '(' . $ddd . ') ' . $fone;
This example works just like double quotes:
echo "(" . $ddd . ") " . $fone;
This is how you concatenate strings in PHP.
Another question is the difference between single and double quotation marks. The string between single quotes is not processed, except for escape characters. For example, if you want the string to contain a single quotation mark character, you will need to use the escape character \
, like this:
$texto_com_aspas = 'Temos aspas aqui => \' <= e isto é bom.';
If you use the $
fault token or the {
and }
square brackets with single quotation marks, they will not be interpreted (as with double quotation marks). They will remain literal, as they are:
$texto_normal = 'A variável $texto_normal contém cifrão e {colchetes}.';
If you use double quotes you will have to use the escape for double quotation marks, for the dollar sign and for the square brackets:
$texto_com_aspas = "Temos aspas aqui => \" <= e isto é bom.";
$outro_texto = "Temos cifrão e colchetes aqui => \{ \$ \} <= e isto é bom.";
In both cases, to get a backslash you will need to duplicate it, because on its first occurrence the \
character is understood as "escape":
$texto_com_barra_invertida = 'Uma barra: \ e mais' . " outra barra: \ beleza?";
Now, the main difference is that using double quotation marks , both the $
and the {
and }
brackets enable interpolation processing in string , through which the contents of variables can be inserted into the text.
For example:
$ddd = '34';
$fone = '32105691';
echo "($ddd) $fone";
// resultado => (34) 3210-5691
This can only be done with double quotation marks. With single quotation marks it would look like this:
echo '($ddd) $fone';
// resultado => ($ddd) $fone
However, in some cases we need to use brackets, because without them it would be difficult to tell where it starts and where the variable to be interpolated ends. See:
$singular = 'peixe';
$texto = "Eu comprei muitos $singulars!";
I want to appear " I bought many fish! "
But how does PHP guess that my variable calls $singular
and not $singulars
(with "s" at the end)?
For this case, and more complex ones, we use the brackets:
$texto1 = "Eu comprei muitos {$singular}s!";
$texto2 = "Seja bem vindo, {$usuario->nome}!";
$texto3 = "Quantidade: {$item['quantidade']} Preço: {$item['preco']}";
$texto4 = "$propriedade => {$item[$propriedade]}";
All of these interpolation examples require double quotation marks.
I particularly never use double quotation marks. I always use single quotes. I've seen lots of double quotes being abused - with strings that are unreadable without syntax highlight . I usually use the sprintf
function when I want to interpolate. My advice is: be friends with the other developers who will work with you, and avoid double quotes.
Basically, single quotes and double quotes have the same effect as string concatenation. In terms of performance, single quotation marks are more efficient than double quotation marks (it simply concatenates strings, whereas double quotation marks have a different dynamic of including each substring in a variable and then concatenating them). This difference, however, has little relevance today, as this additional 'delay' by concatenating with double quotation marks is basically irrelevant.
$nomeCompleto = 'Nome'.'Sobrenome';
or
$nome = 'Teste1';
$sobrenome = 'Teste2';
$completo = $nome . $sobrenome;
The advantage of using double quotation marks is that they interpret values. For example:
<?php
$nome = "Juca";
echo "Olá {$nome}!"; // Olá Juca! ?>
All of this is described in the PHP manual.
By making a complement, the comma can also be used as a concatenation character.
In benchmark tests, the use of the comma, rather than a point, is more performative. It is usually used when you want to apply micro-optimizations.
On the use of single quotes or double quotation marks, I prefer single quotation mark because, performatically, it is better.