string就是一串连续的字符。
注意:PHP没有对string的长度做限制。唯一限制的就是PHP在计算机中的可用内存(php.ini文件中的memory_limit变量的值)
限定字符串范围的方法有4中:
1、单引号;
2、双引号;
3、原型文档语法;
4、nowdoc syntax(PHP5.3.0开始)
1、如果字符串使用单引号“‘”包裹,字符串中如果出现单引号“,”和反斜杠“\”符号,需要进行转义。
1 // Outputs: Arnold once said: "I'll be back" 2 echo 'Arnold once said: "I\'ll be back"'; 3 4 // Outputs: You deleted C:\*.*? 5 echo 'You deleted C:\\*.*?'; 6 7 // Outputs: You deleted C:\*.*? 8 echo 'You deleted C:\*.*?'; 9
(有待验证 单引号包裹的字符串反斜杠是否需要转义)
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、简单语法:
因为解析器会贪婪匹配$后面的字符,所以,为了不出什么以外,应该使用"{"和"}"来表名变量的边界。
1 <?php 2 $beer = 'Heineken'; 3 echo "$beer's taste is great"; // works; "'" is an invalid character for variable names 4 echo "He drank some $beers"; // won't work; 's' is a valid character for variable names but the variable is "$beer" 5 echo "He drank some ${beer}s"; // works 6 echo "He drank some {$beer}s"; // works 7 ?>
同样,数组的下标和对象的属性也会不解析。
1 <?php 2 // These examples are specific to using arrays inside of strings. 3 // When outside of a string, always quote array string keys and do not use 4 // {braces}. 5 6 // Show all errors 7 error_reporting(E_ALL); 8 9 $fruits = array('strawberry' => 'red', 'banana' => 'yellow'); 10 11 // Works, but note that this works differently outside a string 12 echo "A banana is $fruits[banana]."; 13 14 // Works 15 echo "A banana is {$fruits['banana']}."; 16 17 // Works, but PHP looks for a constant named banana first, as described below. 18 echo "A banana is {$fruits[banana]}."; 19 20 // Won't work, use braces. This results in a parse error. 21 echo "A banana is $fruits['banana']."; 22 23 // Works 24 echo "A banana is " . $fruits['banana'] . "."; 25 26 // Works 27 echo "This square is $square->width meters broad."; 28 29 // Won't work. For a solution, see the complex syntax. 30 echo "This square is $square->width00 centimeters broad."; 31 ?>
2、复合语法:
1 <?php 2 // Show all errors 3 error_reporting(E_ALL); 4 5 $great = 'fantastic'; 6 7 // Won't work, outputs: This is { fantastic} 8 echo "This is { $great}"; 9 10 // Works, outputs: This is fantastic 11 echo "This is {$great}"; 12 echo "This is ${great}"; 13 14 // Works 15 echo "This square is {$square->width}00 centimeters broad."; 16 17 // Works 18 echo "This works: {$arr[4][3]}"; 19 20 // This is wrong for the same reason as $foo[bar] is wrong outside a string. 21 // In other words, it will still work, but only because PHP first looks for a 22 // constant named foo; an error of level E_NOTICE (undefined constant) will be 23 // thrown. 24 echo "This is wrong: {$arr[foo][3]}"; 25 26 // Works. When using multi-dimensional arrays, always use braces around arrays 27 // when inside of strings 28 echo "This works: {$arr['foo'][3]}"; 29 30 // Works. 31 echo "This works: " . $arr['foo'][3]; 32 33 echo "This works too: {$obj->values[3]->name}"; 34 35 echo "This is the value of the var named $name: {${$name}}"; 36 37 echo "This is the value of the var named by the return value of getName(): {${getName()}}"; 38 39 echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}"; 40
访问,修改字符串中的指定字符:
字符串可以使用"[]"和"{}"进行访问。(注意: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。
|
请发表评论