The problem with quotes in PHP
To store an alphanumeric data within a variable, we must understand how these data are affected when its start and end are delimited by the two types of existing quotation marks: double quotes and single quotes.
When to use single quotes and when double quotes
Alphanumeric values should be wrapped in quotation marks, either single or double; and instead, numeric values should not be wrapped in quotes.
To store an alphanumeric datum within a variable, we must understand how these data are affected by defining its start and end by the two types of existing quotes: double quotes and single quotes.
But let's see how this affects when we add variables to those texts.
The interpretation of variables within quotes.
We have already seen how the echo command behaves and the print function (both are the same in this):
- When we want the PHP interpreter to write a text literally, we will use single quotes to delimit the start and end of that text. The only limitation is that we can not include single quotes within that text (we must escape them), and any variable that we include within something wrapped in single quotes will not be replaced by its value
- On the other hand, when we want the variables to be interpreted and replaced by their value, we will use double quotes to delimit the start and end of the text block
Let's see an example:
<?php
$doubleQuotes="Text in double quotes, may contain 'single quotes' without problems";
$quotesSimple='Text in single quotes, may contain" double quotes "but without variables inside, because it uses single quotes to delimit the beginning and end of the block ';
$escapeDouble = "Text with \"quotes\" double escapes";
$espapeSimple = "Text with \'quotes\' simples escapadas";
$doubleVariables = "Text with variables such as $ name and $ last name interspersed with double quotes, which will be replaced by their value";
$variablesSimples = 'Text with variables such as $name and $lastName interspersed in single quotes, which will not be replaced by their value, will be written $ name and $ last name as is'';
?>
ChatGPT Free
Ask questions on any topic
CITE ARTICLE
For homework, research, thesis, books, magazines, blogs or academic articles
APA Format Reference:
Delgado, Hugo. (2019).
The problem with quotes in PHP.
Retrieved Dec 07, 2024, from
https://disenowebakus.net/en/php-quotes