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 相关文章推荐
动态网站web开发 PHP、ASP还是ASP.NET
Oct 09 PHP
mysql中存储过程、函数的一些问题
Feb 14 PHP
PHP中动态显示签名和ip原理
Mar 28 PHP
php字符串截取中文截取2,单字节截取模式
Dec 10 PHP
PHP4中session登录页面的应用
Jul 25 PHP
php一行代码获取文件后缀名实例分析
Nov 12 PHP
php实现两表合并成新表并且有序排列的方法
Dec 05 PHP
PHP在线书签系统分享
Jan 04 PHP
Laravel使用消息队列需要注意的一些问题
Dec 13 PHP
Laravel框架路由设置与使用示例
Jun 12 PHP
PHP中Static(静态)关键字功能与用法实例分析
Apr 05 PHP
php使用goto实现自动重启swoole、reactphp、workerman服务的代码
Apr 13 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
yii框架配置默认controller和action示例
2014/04/30 PHP
PHP实现的多文件上传类及用法示例
2016/05/06 PHP
PHP+iframe图片上传实现即时刷新效果
2016/11/18 PHP
CodeIgniter框架实现的整合Smarty引擎DEMO示例
2019/03/28 PHP
javasciprt下jquery函数$.post执行无响应的解决方法
2014/03/13 Javascript
js实现同一页面多个运动效果的方法
2015/04/10 Javascript
js检测判断日期大于多少天的方法
2015/05/04 Javascript
jQuery实现模拟marquee标签效果
2015/07/14 Javascript
Prototype框架详解
2015/11/25 Javascript
jquery实现简单的banner轮播效果【实例】
2016/03/30 Javascript
微信小程序-图片、录音、音频播放、音乐播放、视频、文件代码实例
2016/11/22 Javascript
mui开发中获取单选按钮、复选框的值(实例讲解)
2017/07/24 Javascript
JS手机端touch事件计算滑动距离的方法示例
2017/10/26 Javascript
手动用webpack搭建第一个ReactApp的示例
2018/04/11 Javascript
vue强制刷新组件的方法示例
2019/02/28 Javascript
layui实现数据表格table分页功能(ajax异步)
2019/07/27 Javascript
js中!和!!的区别与用法
2020/05/09 Javascript
[52:14]VG vs Serenity 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
Python程序设计入门(2)变量类型简介
2014/06/16 Python
Python批量按比例缩小图片脚本分享
2015/05/21 Python
Python IDE PyCharm的基本快捷键和配置简介
2015/11/04 Python
python 写入csv乱码问题解决方法
2016/10/23 Python
PyTorch学习笔记之回归实战
2018/05/28 Python
Numpy之random函数使用学习
2019/01/29 Python
如何用Python 加密文件
2020/09/10 Python
Autopep8的使用(python自动编排工具)
2021/03/02 Python
CSS3 实现弹跳的小球动画
2020/10/26 HTML / CSS
使用spring mvc+localResizeIMG实现HTML5端图片压缩上传的功能
2016/12/16 HTML / CSS
伦敦哈德森鞋:Hudson Shoes
2018/02/06 全球购物
称象教学反思
2014/02/03 职场文书
周年庆典主持词
2014/04/02 职场文书
端午节演讲稿
2014/05/23 职场文书
答辩状格式范本
2015/05/22 职场文书
详解JS WebSocket断开原因和心跳机制
2021/05/07 Javascript
python调试工具Birdseye的使用教程
2021/05/25 Python
Golang 结构体数据集合
2022/04/22 Golang