Blogger Information
Blog 11
fans 0
comment 0
visits 10135
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP初识字符串
小杂鱼
Original
506 people have browsed it

字符串

1.单引号定义字符串
2.双引号定义字符串
3.定界符定义字符串

单引号定义字符串

1.单引号不能解析 转义符 \n 换行 \r 回车 \t tab

  1. echo 'this is \n a simple \n \r \t string';
  2. //this is \n a simple \n \r \t string

2.单引号不能解析变量

  1. $name = '苹果';
  2. $price = '6666';
  3. echo '我想买一台 $name手机,价格是$price';
  4. //我想买一台 $name手机,价格是$price

双引号定义字符串

1.双引号可以解析 转义符 \n 换行 \r 回车 \t tab

  1. echo "this is \n a simple \n \r \t string";
  2. //this is
  3. //a simple
  4. //
  5. // string

2.双引号可以解析变量

  1. $name = '苹果';
  2. $price = '6666';
  3. echo "我想买一台$name 手机(变量后面不加空格连接字符串会报错),价格是$price";
  4. //我想买一台苹果 手机,价格是6666 (有空格)
  5. //加花括号避免空格和报错
  6. echo "我想买一台{$name}手机,价格是{$price}";
  7. //我想买一台苹果手机,价格是6666

定界符定义字符串

1.可以解析转义符,变量
2.适合输出大量的多行的内部存在多个变量的PHP字符串

  1. //定界符格式:string为任意字符串,但是格式要统一
  2. // <<<string
  3. // code
  4. // string;
  5. $name = '苹果';
  6. $price = '6666';
  7. echo <<<DJ
  8. <table border="1">
  9. <tr>
  10. <td>{$name}</td>
  11. <td>{$price}</td>
  12. </tr>
  13. </table>
  14. DJ;
  15. echo <<<nm
  16. <table border="1">
  17. <tr>
  18. <td>{$name}</td>
  19. <td>{$price}</td>
  20. </tr>
  21. </table>
  22. nm;

字符串索引

  1. $name = '苹果';
  2. $price = 'apple';
  3. echo $price[0]; //a
  4. echo $price{1}; //p PHP8.0版本不再支持
  5. //因为一个汉字占3个字符
  6. echo $name[0].$name[1].$name[2]; //苹
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post