While reading a PHP article, I saw the type of string php double quoted
and was curious to know how it works while reading their English documentation on PHP.
I didn't find much information about it, String double quoted
is it just a String
simple and ordinary one or does it have any special or additional functionality?
The difference between one
string
wrapped in single quotes and one wrapped in double quotes is that the one with single quotes will always be interpreted as a literal and the one with double quotes will first be checked by the interpreter to check whether or not it should interpret something in its interior.This may sound somewhat confusing but with an example it will be much clearer:
Use of single quotes
Result:
Use of double quotes
As you can see, with the use of single quotes the interpreter understands that there is nothing within them that must be interpreted, so the name of the variable is literally written as if it were one more word.
In the case of doubles, it does check what is inside, finding the variable
$nombre
and obtaining its value.Reviewing the link you provide in your same question, there are 2 main scenarios (although they are not the only ones)
Scenario 1
it will be interpreted literally, that is, there will be no interpolation of the data and interpretation of the line break symbol, for example.
Example scenario 1
The following script is run from the console and behaves as follows:
$edad = 20
echo
, trying to apply a line break to it as follows:scenario 2
From the documentation:
Which could be translated as:
it is interpolated or interpreted dynamically, displaying it in the output printed from the console or from the browser.
Example scenario 2
Repeating the same exercise of scenario 1, we obtain the following:
Extra
You can also within a print of values, enclose a variable inside
{}
but there must not be empty spaces between them and the variable to be interpolated.What about the values
CONSTANTES
?Unlike variables, constants do not have a symbol that the PHP interpreter can take as a reference to carry out the interpolation process and print the associated value.
Example
As can be seen, the value of the constant is not interpreted and it only prints its name literally, so in order to achieve the above we can, for example, do a concatenation process:
Option 1
Giving an output like this:
Option 2
According to the PHP documentation , we can use the method
sprintf
which returns a string formatted as follows:Getting an output like this:
Where, as can be seen, the line break is interpreted and the constant would remain within the original text string.
References