Home >Backend Development >PHP Tutorial >The difference between double quotes and single quotes in php strings_PHP tutorial

The difference between double quotes and single quotes in php strings_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:08:48808browse

[php]

/*
Strings have already been discussed when talking about variables, why do we need to talk about them again?
Answer: Previously, we only introduced the variable type string,
But there is no in-depth explanation of its definition, properties, functions, etc.
No. 2: String is a very, very important concept in PHP.
You can say this: When you are developing a website, you want to do some kind of operation on strings.
Basically, the system already has related functions for the string processing functions you think of.
Because PHP has been developed for WEB since its birth,
Because in website development, string processing is a highlight of PHP.
No. 3: Strings are also the most frequently used during development and interviews.
In PHP, 1 is a string and 2 is an array function. There are many functions and they are very easy to use.
*/
/*
What is a string?
Answer: String is a string of characters
Mutton kebab is a skewer of mutton.
*/
/*
1: How to declare a string variable
Answer:
Double quotes
Single quotes
Delimiter
*/
$str1 = "hello";
echo $str1,'
';
// $str2 = "hello,"jack" "; // This string is a news title, and double quotes can easily appear in the title.
/**
Thinking:
When using double quotes to declare a string,
No more double quotes can appear inside,
Because double quotes are treated as "boundaries" of strings,
Therefore, the presence of double quotes inside causes ambiguity in interpretation.
At this time, you can use escape characters,
That is, use "escape" to represent "
Are there any other escape characters?
Think: ", is used as "" I understand.
Then what if I really want to express the backslash?,
Answer: Use \, to represent backslash
Are there any other escape characters?
Answer: Yes
n, r, $, etc.
n escapes into newline character
r is converted into carriage return character
**/
$str2 = "hello ,"jack",
";
echo $str2;
$str3 = "hello \";
echo $str3,'
';
$str4 = "hellonrnrnrworld";
echo $str4,'
';
$str5 = "hello $str3";
echo $str5,'
'; // hello hello, that is, parse $str3 as a variable
$str6 = "world $str3"; //Here $ is converted into an ordinary string $ and is no longer understood as a variable flag.
echo $str6;
?>
// Look at the single quotes again
$str1 = 'hello';
echo $str1,'
';
// $str2 = 'hello 'jack''; // A certain string is a news title, and single quotes appear in the title,
// What to do if single quotes appear inside single quotes again?
//' is used as a string boundary,
// Therefore, if you want to appear in the string, you have to use escape to express it
$str2 = 'hello 'jack'';
echo $str2,'
';
// is used to escape single quotes, so what should I do if I really want to express it.
// \ to mean
$str3 = 'hello \';
echo $str3;
?>
// Common interview questions
// 1: What is the difference between single and double quotes to define a string?
$age = 29;
$str1 = 'hello nr t $age';
$str2 = "hello nr t $age";
echo $str1,'
',$str2;
/*
Difference in escaping:
Single quotes, the system does not perform complex escaping,
Only escape ',\, others---output them as they are.
With double quotes, there are more escapes, ",\,r,n,t,$, etc.
Differences in variable interpretation:
Single quotes, do not use $ in strings for variable name analysis,
The double quotes will try to interpret $ as a variable name for parsing.
*/
// 2: Single and double quotes define strings, which one is faster?
// Answer: Single quotes are parsed faster because there is no need for too much escaping and variable parsing
// Question: When should single/double quotes be used?
/* www.2cto.com
Answer: Large paragraphs of text, such as news titles, text content, self-introduction, etc.
Use ', the analysis is fast.
But sometimes, you really need to include variables in a string,
For example, when piecing together sql statements
$id = 5;
$sql = "select * from user where id = $id";
// If you use a single quote at this time, the statement becomes ..id= $id, and sql will go wrong.
// At this time, it is appropriate to use double quotes. Parse $id, and the statement is parsed into ..id = 5;
*/
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477762.htmlTechArticle[php] ?php /* Strings have already been discussed when talking about variables, why do we need to talk about them again? Answer :Before, I only introduced the variable type string, but did not go into depth about its definition, properties, functions, etc...
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