PHP中的string类型使用说明


Posted in PHP onJuly 27, 2010

注意:PHP没有对string的长度做限制。唯一限制的就是PHP在计算机中的可用内存(php.ini文件中的memory_limit变量的值)
限定字符串范围的方法有4中:
1、单引号;
2、双引号;
3、原型文档语法;
4、nowdoc syntax(PHP5.3.0开始)

1、如果字符串使用单引号“‘”包裹,字符串中如果出现单引号“,”和反斜杠“\”符号,需要进行转义。

// Outputs: Arnold once said: "I'll be back" 
echo 'Arnold once said: "I\'ll be back"'; 
// Outputs: You deleted C:\*.*? 
echo 'You deleted C:\\*.*?'; 
// Outputs: You deleted C:\*.*? 
echo 'You deleted C:\*.*?';

(有待验证 单引号包裹的字符串反斜杠是否需要转义)

2、如果字符串被双引号包裹 一下字符都会被转义:
Escaped characters Sequence Meaning
\n linefeed (LF or 0x0A (10) in ASCII)
\r carriage return (CR or 0x0D (13) in ASCII)
\t horizontal tab (HT or 0x09 (9) in ASCII)
\v vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
\f form feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\\ backslash
\$ dollar sign
\" double-quote
\[0-7]{1,3} the sequence of characters matching the regular expression is a character in octal notation
\x[0-9A-Fa-f]{1,2} the sequence of characters matching the regular expression is a character in hexadecimal notation

如果字符串 使用双引号“"”或者原形文档语法的形式包裹的话,在字符串中的变量会被解析。
1、简单语法:
因为解析器会贪婪匹配$后面的字符,所以,为了不出什么以外,应该使用"{"和"}"来表名变量的边界。

<?php 
$beer = 'Heineken'; 
echo "$beer's taste is great"; // works; "'" is an invalid character for variable names 
echo "He drank some $beers"; // won't work; 's' is a valid character for variable names but the variable is "$beer" 
echo "He drank some ${beer}s"; // works 
echo "He drank some {$beer}s"; // works 
?>

同样,数组的下标和对象的属性也会不解析。
<?php 
// These examples are specific to using arrays inside of strings. 
// When outside of a string, always quote array string keys and do not use 
// {braces}. 
// Show all errors 
error_reporting(E_ALL); 
$fruits = array('strawberry' => 'red', 'banana' => 'yellow'); 
// Works, but note that this works differently outside a string 
echo "A banana is $fruits[banana]."; 
// Works 
echo "A banana is {$fruits['banana']}."; 
// Works, but PHP looks for a constant named banana first, as described below. 
echo "A banana is {$fruits[banana]}."; 
// Won't work, use braces. This results in a parse error. 
echo "A banana is $fruits['banana']."; 
// Works 
echo "A banana is " . $fruits['banana'] . "."; 
// Works 
echo "This square is $square->width meters broad."; 
// Won't work. For a solution, see the complex syntax. 
echo "This square is $square->width00 centimeters broad."; 
?>

2、复合语法:
<?php 
// Show all errors 
error_reporting(E_ALL); 
$great = 'fantastic'; 
// Won't work, outputs: This is { fantastic} 
echo "This is { $great}"; 
// Works, outputs: This is fantastic 
echo "This is {$great}"; 
echo "This is ${great}"; 
// Works 
echo "This square is {$square->width}00 centimeters broad."; 
// Works 
echo "This works: {$arr[4][3]}"; 
// This is wrong for the same reason as $foo[bar] is wrong outside a string. 
// In other words, it will still work, but only because PHP first looks for a 
// constant named foo; an error of level E_NOTICE (undefined constant) will be 
// thrown. 
echo "This is wrong: {$arr[foo][3]}"; 
// Works. When using multi-dimensional arrays, always use braces around arrays 
// when inside of strings 
echo "This works: {$arr['foo'][3]}"; 
// Works. 
echo "This works: " . $arr['foo'][3]; 
echo "This works too: {$obj->values[3]->name}"; 
echo "This is the value of the var named $name: {${$name}}"; 
echo "This is the value of the var named by the return value of getName(): {${getName()}}"; 
echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}";

访问,修改字符串中的指定字符:
字符串可以使用"[]"和"{}"进行访问。(注意:php5.3.0以后不建议使用“{}”访问)
注意:使用其他类型(非integer)类型访问字符串指定的字符,都会返回NULL
警告:
Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits E_NOTICE. Negative offset emits E_NOTICE in write but reads empty string. Only the first character of an assigned string is used. Assigning empty string assigns NUL byte。
PHP 相关文章推荐
php二维数组排序与默认自然排序的方法介绍
Apr 27 PHP
php登陆页的密码处理方式分享
Oct 14 PHP
php批量更改数据库表前缀实现方法
Oct 26 PHP
基于preg_match_all采集后数据处理的一点心得笔记(编码转换和正则匹配)
Jan 31 PHP
php常量详细解析
Oct 27 PHP
PHP 下载文件时如何自动添加bom头及解释BOM头和去掉bom头的方法
Jan 04 PHP
PHP编写学校网站上新生注册登陆程序的实例分享
Mar 21 PHP
微信利用PHP创建自定义菜单的方法
Aug 01 PHP
PHP Mysqli 常用代码集合
Nov 12 PHP
PHP互换两个变量值的方法(不用第三变量)
Nov 14 PHP
Laravel中正确地返回HTTP状态码方法示例
Sep 10 PHP
PHPstorm启用自动换行的方法详解(IDE)
Sep 17 PHP
PHP中的array数组类型分析说明
Jul 27 #PHP
ionCube 一款类似zend的PHP加密/解密工具
Jul 25 #PHP
PHP array 的加法操作代码
Jul 24 #PHP
PHP IN_ARRAY 函数使用注意事项
Jul 24 #PHP
PHP STRING 陷阱原理说明
Jul 24 #PHP
PHP下操作Linux消息队列完成进程间通信的方法
Jul 24 #PHP
php抓取页面与代码解析 推荐
Jul 23 #PHP
You might like
php自动获取目录下的模板的代码
2010/08/08 PHP
Yii调试查看执行SQL语句的方法
2016/07/15 PHP
jQuery 研究心得 取得属性的值
2007/11/30 Javascript
javascript 子窗体父窗体相互传值方法
2010/05/31 Javascript
jQuery的实现原理的模拟代码 -2 数据部分
2010/08/01 Javascript
javascript获取select的当前值示例代码(兼容IE/Firefox/Opera/Chrome)
2013/12/17 Javascript
javascript单引号和双引号的区别和处理
2014/05/14 Javascript
简介可以自动完成UI的AngularJS工具angular-smarty
2015/06/23 Javascript
JavaScript编写一个简易购物车功能
2016/09/17 Javascript
Windows系统下安装Node.js的步骤图文详解
2016/11/15 Javascript
使用vue.js2.0 + ElementUI开发后台管理系统详细教程(一)
2017/01/21 Javascript
jQuery ajax实现省市县三级联动
2021/03/07 Javascript
微信小程序中子页面向父页面传值实例详解
2017/03/20 Javascript
基于vue和react的spa进行按需加载的实现方法
2018/09/29 Javascript
JavaScript判断对象和数组的两种方法
2019/05/31 Javascript
Vue实现商品飞入购物车效果(电商项目)
2019/11/26 Javascript
javascript中的with语句学习笔记及用法
2020/02/17 Javascript
JSONP解决JS跨域问题的实现
2020/05/25 Javascript
[07:27]DOTA2卡尔工作室 英雄介绍水晶室女篇
2013/06/21 DOTA
[03:43]TI9战队采访——PSG.LGD
2019/08/22 DOTA
Linux CentOS7下安装python3 的方法
2018/01/21 Python
python+opencv实现霍夫变换检测直线
2020/10/23 Python
Python可迭代对象操作示例
2019/05/07 Python
pybind11在Windows下的使用教程
2019/07/04 Python
详解基于python-django框架的支付宝支付案例
2019/09/23 Python
Python简易计算器制作方法代码详解
2019/10/31 Python
Django {{ MEDIA_URL }}无法显示图片的解决方式
2020/04/07 Python
如何通过Python3和ssl实现加密通信功能
2020/05/09 Python
使用豆瓣源来安装python中的第三方库方法
2021/01/26 Python
canvas实现滑动验证的实现示例
2020/08/11 HTML / CSS
巴西最大的家电和百货零售商:Casas Bahia
2016/11/22 全球购物
服务之星获奖感言
2014/01/21 职场文书
教师研修随笔感言
2014/01/23 职场文书
老公保证书范文
2014/04/29 职场文书
婚礼迎宾词大全
2015/08/10 职场文书
导游词之黄帝陵景区
2019/09/16 职场文书