php字符串中的双引号与单引号区别
[php]
/*
字符串在讲变量的时候已经讲了,怎么又要讲?
答:之前,只是介绍字符串这种变量类型,
但对于其定义,性质,函数等等,没有深入的说.
第2: 字符串,在PHP中,是非常非常重要的一个概念.
可以这么说: 你在开发网站中,想对字符串做某种操作.
基本上你想到的字符串的处理功能,系统都已有相关函数了.
因为PHP自诞生之初,就是为WEB开发而来的,
因为网站开发中,字符串处理 ,是PHP的一大亮点.
第3: 字符串在开发和面试时,也是最频繁用到的.
PHP中,1是字符串,2是数组函数,都非常多,非常好用.
*/
/*
什么是字符串?
答:字符串,就是一串字符
羊肉串就是一串羊肉.
*/
/*
1:如何声明一个字符串变量
答:
双引号
单引号
定界符
*/
$str1 = "hello";
echo $str1,'
';
// $str2 = "hello,"jack" "; // 这个字符串,是一个新闻标题,标题里很容易又出现双引号.
/**
思考:
用双引号,来声明字符串的时候,
内部不能再出现双引号,
因为,双引号被当成字符串的"边界"来对待,
因此,内部出现双引号,就引起了解释上的歧义.
这时,可以用转义字符,
即,用\" 转义 来表示 "
还有没有其他的转义字符呢?
想: \", 被用来当成 " 理解了.
那我要是确实想表示 反斜线\, 又怎么办呢?,
答: 用\\, 表示 反斜线\
还有没有其他的转义字符呢?
答:有
\n, \r, \$,等
\n 转义成 换行符
\r 转义成 回车符
**/
$str2 = "hello ,\"jack\",
";
echo $str2;
$str3 = "hello \\";
echo $str3,'
';
$str4 = "hello\n\r\n\r\n\rworld";
echo $str4,'
';
$str5 = "hello $str3";
echo $str5,'
'; // hello hello \, 即把$str3当变量给解析出来
$str6 = "world \$str3"; //此处 \$被转成了普通字符串$,而不再理解为变量标志.
echo $str6;
?>
// 再看单引号
$str1 = 'hello';
echo $str1,'
';
// $str2 = 'hello 'jack''; // 某字符串是新闻标题,标题又出现单引号,
// 单引号里又出现的单引号怎么办呢?
// '被用来做字符串的边界了,
// 因此,串内想出现',只好用转义来表示
$str2 = 'hello \'jack\'';
echo $str2,'
';
// \被用来转义单引号用了,那么如果我确实想表示\,怎么办呢.
// \\ 来 表示 \
$str3 = 'hello \\';
echo $str3;
?>
// 常见的面试题
// 1:单双引号定义字符串有何区别?
$age = 29;
$str1 = 'hello \n\r \t $age';
$str2 = "hello \n\r \t $age";
echo $str1,'
',$str2;
/*
转义上的区别:
单引号,系统不做复杂的转义,
只转义\',\\, 其他的---一律原样输出.
双引号,则转义的比较多,\",\\,\r,\n,\t,\$等.
对于变量解释的区别:
单引号,不对字符串的$作变量名解析,
而双引号,会试着$理解成变量名,来解析.
*/
// 2:单双引号定义字符串,哪个速度更快一些?
// 答:单引号解析的更快,因为不需要进行过多的转义和变量解析
// 问:什么时间选用单/双引号呢?
/* www.2cto.com
答:大段文本,比如新闻标题,文本内容,自我介绍等这样大段话,
用',解析快.
但有时候,确实需要在一段字符串夹杂变量,
比如拼凑sql语句时
$id = 5;
$sql = "select * from user where id = $id";
// 如果此时用单引,则语句成为了 ..id= $id,sql就出错了.
// 此时,用双引合适. 解析$id,语句解析成 ..id = 5;
*/
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function