欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP中的string类型使用说明

程序员文章站 2023-11-18 15:57:22
注意:php没有对string的长度做限制。唯一限制的就是php在计算机中的可用内存(php.ini文件中的memory_limit变量的值) 限定字符串范围的方法有4中:...
注意: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。