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.

The problem with quotes in PHP | Learn PHP & MySQL | 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):

  1. 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
  2. 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 Nov 03, 2024, from
https://disenowebakus.net/en/php-quotes

Participates!

Share it with your friends in Social Networks!

Professor at the University of Guadalajara

Hugo Delgado Desarrollador y Diseñador Web en Puerto Vallarta

Professional in Web Development and SEO Positioning for more than 10 continuous years.
We have more than 200 certificates and recognitions in the Academic and Professional trajectory, including diploma certificates certified by Google.

CONTINUE LEARNING

IT ALSO DESERVES TO PAY TO VISIT:

Not finding what you need?

Use our internal search to discover more information
Related content:

Would you like to learn more about Web Design?

Meet all the courses and tutorials that we have for you completely free
Learn Web Design
 

Leave your Comment

SPONSOR

Your business can also appear here. More information

Next article: