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 相关文章推荐
一次编写,随处运行
Oct 09 PHP
PHP新手上路(十三)
Oct 09 PHP
使用swoole扩展php websocket示例
Feb 13 PHP
php判断用户是否手机访问代码
Jun 08 PHP
PHP文件生成的图片无法使用CDN缓存的解决方法
Jun 20 PHP
PHP中$_SERVER使用说明
Jul 05 PHP
验证token、回复图文\文本、推送消息的实用微信类php代码
Jun 28 PHP
在Thinkphp中使用ajax实现无刷新分页的方法
Oct 25 PHP
php base64 编码与解码实例代码
Mar 21 PHP
PHP仿tp实现mvc框架基本设计思路与实现方法分析
May 23 PHP
PHP设计模式之抽象工厂模式实例分析
Mar 25 PHP
Thinkphp5.0框架的Db操作实例分析【连接、增删改查、链式操作等】
Oct 11 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
我的论坛源代码(六)
2006/10/09 PHP
在Linux系统的服务器上隐藏PHP版本号的方法
2015/06/06 PHP
PHP微信红包生成代码分享
2016/10/06 PHP
js 判断脚本加载完毕的代码
2011/07/13 Javascript
jQuery中对节点进行操作的相关介绍
2013/04/16 Javascript
多种方法实现360浏览器下禁止自动填写用户名密码
2014/06/16 Javascript
原生javascript实现的分页插件pagenav
2014/08/28 Javascript
项目中常用的JS方法整理
2015/01/30 Javascript
jQuery实现仿路边灯箱广告图片轮播效果
2015/04/15 Javascript
JavaScript中isPrototypeOf函数作用和使用实例
2015/06/01 Javascript
基于jQuery实现的美观星级评论打分组件代码
2015/10/30 Javascript
jQuery简单实现提交数据出现loading进度条的方法
2016/03/29 Javascript
深入浅析javascript中的作用域(推荐)
2016/07/19 Javascript
网页挂马方式整理及详细介绍
2016/11/03 Javascript
AngularJS ng-template寄宿方式用法分析
2016/11/07 Javascript
Vue单页式应用(Hash模式下)实现微信分享的实例
2017/07/21 Javascript
微信小程序实现页面跳转传值的方法
2017/10/12 Javascript
简易Vue评论框架的实现(父组件的实现)
2018/01/08 Javascript
vue学习之Vue-Router用法实例分析
2020/01/06 Javascript
Vue列表如何实现滚动到指定位置样式改变效果
2020/05/09 Javascript
js闭包的9个使用场景
2020/12/29 Javascript
树莓派中python获取GY-85九轴模块信息示例
2013/12/05 Python
Python map和reduce函数用法示例
2015/02/26 Python
python 随机打乱 图片和对应的标签方法
2018/12/14 Python
运用Python的webbrowser实现定时打开特定网页
2019/02/21 Python
Django给admin添加Action的步骤详解
2019/05/01 Python
Python OpenCV调用摄像头检测人脸并截图
2020/08/20 Python
Python字典常见操作实例小结【定义、添加、删除、遍历】
2019/10/25 Python
python隐藏类中属性的3种实现方法
2019/12/19 Python
Pandas时间序列:时期(period)及其算术运算详解
2020/02/25 Python
消防安全标语
2014/06/07 职场文书
出差报告格式模板
2014/11/06 职场文书
装修公司管理制度
2015/08/05 职场文书
施工现场安全管理制度
2015/08/05 职场文书
2016教师廉洁教育心得体会
2016/01/13 职场文书
使用 Apache 反向代理的设置技巧
2022/01/18 Servers