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之第九天
Oct 09 PHP
《PHP边学边教》(02.Apache+PHP环境配置――下篇)
Dec 13 PHP
用PHP的超级变量$_GET获取HTML表单(Form) 数据
May 07 PHP
php开启安全模式后禁用的函数集合
Jun 26 PHP
php eval函数用法总结
Oct 31 PHP
php读取本地文件常用函数(fopen与file_get_contents)
Sep 09 PHP
CI框架给视图添加动态数据
Dec 01 PHP
PHP中preg_match函数正则匹配的字符串长度问题
May 27 PHP
浅谈PHP检查数组中是否存在某个值 in_array 函数
Jun 13 PHP
浅谈PHP eval()函数定义和用法
Jun 21 PHP
php中的单引号、双引号和转义字符详解
Feb 16 PHP
PHP获取路径和目录的方法总结【必看篇】
Mar 04 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实现的sqlite数据库连接类
2014/12/12 PHP
PHP使用正则表达式获取微博中的话题和对象名
2015/07/18 PHP
apache集成php7.3.5的详细步骤
2019/06/20 PHP
Laravel 5+ .env环境配置文件详解
2020/04/06 PHP
使表格的标题列可左右拉伸jquery插件封装
2014/11/24 Javascript
纯JavaScript实现的兼容各浏览器的添加和移除事件封装
2015/03/28 Javascript
JS常见问题之为什么点击弹出的i总是最后一个
2016/01/05 Javascript
JavaScript动态生成二维码图片
2016/04/20 Javascript
vue修改对象的属性值后页面不重新渲染的实例
2018/08/09 Javascript
详解JS中统计函数执行次数与执行时间
2018/09/04 Javascript
在vue中使用echarts图表实例代码详解
2018/10/22 Javascript
Vue将页面导出为图片或者PDF
2020/08/17 Javascript
nodejs中各种加密算法的实现详解
2019/07/11 NodeJs
微信小程序以7天为周期连续签到7天功能效果的示例代码
2020/08/20 Javascript
Python3中的json模块使用详解
2018/05/05 Python
Python运维开发之psutil库的使用详解
2018/10/18 Python
Python3 max()函数基础用法
2019/02/19 Python
详解Python的三种可变参数
2019/05/08 Python
python交易记录链的实现过程详解
2019/07/03 Python
python中的colorlog库使用详解
2019/07/05 Python
用Python批量把文件复制到另一个文件夹的实现方法
2019/08/16 Python
基于python调用psutil模块过程解析
2019/12/20 Python
python爬虫数据保存到mongoDB的实例方法
2020/07/28 Python
详解python定时简单爬取网页新闻存入数据库并发送邮件
2020/11/27 Python
python绘制高斯曲线
2021/02/19 Python
X/HTML5 和 XHTML2
2008/10/17 HTML / CSS
HTML5 Canvas 绘图——使用 Canvas 绘制图形图文教程 使用html5 canvas 绘制精美的图
2015/08/31 HTML / CSS
加拿大健康、婴儿和美容产品在线购物:Well.ca
2016/11/30 全球购物
全球速卖通:AliExpress(国际版淘宝)
2017/09/20 全球购物
VisionPros美国站:加拿大在线隐形眼镜和眼镜零售商
2020/02/11 全球购物
销售业务员岗位职责
2014/01/29 职场文书
检讨书范文1000字
2015/01/28 职场文书
退货证明模板
2015/06/23 职场文书
2015年党务工作者个人工作总结
2015/10/22 职场文书
CSS实现章节添加自增序号的方法
2021/06/23 HTML / CSS
怎么禁用Win11输入法 最新Win11输入法关闭教程
2022/08/05 数码科技