Home > Backend Development > PHP Tutorial > php像数组一样存取和修改字符串字符_php实例

php像数组一样存取和修改字符串字符_php实例

WBOY
Release: 2016-06-07 17:21:15
Original
805 people have browsed it

如获取第二个$str[1]或$str{1} ,不建议使用{},最好使用[]
测试如下

复制代码 代码如下:

//获取字符最后一个字符
$str = 'phpddt.com';
echo $str[strlen($str)-1]; //m
//修改第一个字符
$str = 'phpddt.com';
$str[0] = 'a';  //ahpddt.com
//方括号中的数字超出范围将会产生空白。 
$str = 'phpddt.com';
$str[100] = 'y';  //phpddt.com y
//如果是非整数类型被转换成整数
$str = 'phpddt.com';
$str['a'] = 'y'; //phpddt.com y
$str = 'phpddt.com';
$str[-1] = 'y'; //负数会出错:Warning: Illegal string offset: -1
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template