Home  >  Article  >  Backend Development  >  The difference and function of single quotes and double quotes in PHP

The difference and function of single quotes and double quotes in PHP

伊谢尔伦
伊谢尔伦Original
2017-04-10 15:42:263451browse

The role of single quotes and double quotes in PHP

In PHP,The definition of string can use single quotes or double quotes. However, the same type of single or double quotes must be used to define the string, such as: 'Hello" and "Hello' are illegal String definition. ​

When defining a string, only one kind of quotation mark is considered as the delimiter, that is, single quotation mark or double quotation mark. Thus, if a string begins with a double quote, only the double quote is parsed by the parser. This way, you can include any other character, even single quotes, within the double-quoted string.

The following uses single quotes and double quotes to define a string. The example is as follows:

<?php
$str1 = "go  php";
$str2 = &#39;go  php&#39;;
echo $str1;
echo "<br>";
echo $str2;
?>

Output result:

go php
go php

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

The above example of using single quotes and double quotes to define a string cannot tell the difference between the two for ordinary strings. Now we can easily understand the difference between single quotes and double quotes through the processing of variables. The example is as follows:

<?php
$foo = "php";
$str1 = "study  $foo";
$srt2 = &#39;study  $foo&#39;;
echo $str1;
echo "<br>";
echo $str2;
?>

Output result:

study php
study $foo   //有的浏览器会出现  Notice: Undefined variable: str2 in。。。的错误

It can be seen that the content in double quotes has been parsed by PHP language, and any variable in double quotes will output its value. ;The content of single quotes is "what you see is what you get". Regardless of whether there are variables or not, it will be treated as an ordinary string and output as it is. Insert a variable within double quotes. If there are English or Chinese characters after the variable, it will concatenate the character and the variable and treat it as a whole variable. Be sure to follow the variable with special characters , such as spaces, etc. Single quotes are more efficient than double quotes. Use single quotes whenever possible. Double numbers and single quotes can be interspersed with each other! ! ! Insert single quotes between double quotes, insert variables between single quotes, and the variable will be parsed. If you insert a variable within double quotes and do not want spaces after it, you can wrap the variable in curly braces.

The above is the detailed content of The difference and function of single quotes and double quotes in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn