单引号与双引号
PHP字符串中单引号与双引号的区别:
- 在双引号中可以解析变量,在单引号中不可解析变量。
- 在双引号中可以使用转义字符(n 回车 r 空格 t TAB键 反斜线 $ 美元符 ” 双引号 正则),在单引号中不可使用。
- 在单引号中只能转义单引号和转义号。
- 因为功能单一,所以单引号速度高。
$int = 10; $str1 = "my name is k, $int"; // my name is k, 10 $str2 = 'my name is k, $int'; // my name is k, $int $str3 = "mynameis{$int}abc"; // mynameis10abc $str4 = 'my name is 'k''; // my name is 'k' $str5 = "$int = {$int}"; // $int = 10
定界符声明
定界符以 <<<
开始,紧接定界的字符串,然后直接回车(空格都不可有);在定界符的结尾处要顶格写与之前相同的字符串,后跟分号,然后回车(空格都不可有)。
定界符开始的字符串,若加上单引号,则和单引号的功能一样,不会解析变量;若不加,则和双引号的功能一样,会解析变量。
$int = 10; $str = <<<maosiji this is website this is demo this is blue this is char this is maosiji this is $int maosiji; echo $str; // $int 会输出为10 和双引号的功能一样 //------------- $str2 = <<<'maosiji2' this is website this is demo this is blue this is char this is maosiji this is $int maosiji2; echo $str2; // $int 不会被解析 和单引号的功能一样
本文由 猫斯基 原创发布。
著作权均归用户本人所有。独家文章转载,请联系本站管理员。获得授权后,须注明本文地址! 本文地址:https://maosiji.com/php/php-5.html