©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
一个字符串 string 就是由一系列的字符组成,其中每个字符等同于一个字节。这意味着 PHP 只能支持 256 的字符集,因此不支持 Unicode 。详见字符串类型详解。
Note: string 最大可以达到 2GB。
一个字符串可以用 4 种方式表达:
定义一个字符串的最简单的方法是用单引号把它包围起来(字符 ')。
要表达一个单引号自身,需在它的前面加个反斜线(\)来转义。要表达一个反斜线自身,则用两个反斜线(\\)。其它任何方式的反斜线都会被当成反斜线本身:也就是说如果想使用其它转义序列例如 \r 或者 \n,并不代表任何特殊含义,就单纯是这两个字符本身。
Note: 不像双引号和 heredoc 语法结构,在单引号字符串中的变量和特殊字符的转义序列将不会被替换。
<?php
echo 'this is a simple string' ;
// 可以录入多行
echo 'You can also have embedded newlines in
strings this way as it is
okay to do' ;
// 输出: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"' ;
// 输出: You deleted C:\*.*?
echo 'You deleted C:\\*.*?' ;
// 输出: You deleted C:\*.*?
echo 'You deleted C:\*.*?' ;
// 输出: This will not expand: \n a newline
echo 'This will not expand: \n a newline' ;
// 输出: Variables do not $expand $either
echo 'Variables do not $expand $either' ;
?>
如果字符串是包围在双引号(")中, PHP 将对一些特殊的字符进行解析:
序列 | 含义 |
---|---|
\n | 换行(ASCII 字符集中的 LF 或 0x0A (10)) |
\r | 回车(ASCII 字符集中的 CR 或 0x0D (13)) |
\t | 水平制表符(ASCII 字符集中的 HT 或 0x09 (9)) |
\v | 垂直制表符(ASCII 字符集中的 VT 或 0x0B (11))(自 PHP 5.2.5 起) |
\e | Escape(ASCII 字符集中的 ESC 或 0x1B (27))(自 PHP 5.4.0 起) |
\f | 换页(ASCII 字符集中的 FF 或 0x0C (12))(自 PHP 5.2.5 起) |
\\ | 反斜线 |
\$ | 美元标记 |
\" | 双引号 |
\[0-7]{1,3} | 符合该正则表达式序列的是一个以八进制方式来表达的字符 |
\x[0-9A-Fa-f]{1,2} | 符合该正则表达式序列的是一个以十六进制方式来表达的字符 |
和单引号字符串一样,转义任何其它字符都会导致反斜线被显示出来。PHP 5.1.1 以前,\{$var} 中的反斜线还不会被显示出来。
用双引号定义的字符串最重要的特征是变量会被解析,详见变量解析。
第三种表达字符串的方法是用 heredoc 句法结构:<<<。在该运算符之后要提供一个标识符,然后换行。接下来是字符串 string 本身,最后要用前面定义的标识符作为结束标志。
结束时所引用的标识符必须在该行的第一列,而且,标识符的命名也要像其它标签一样遵守 PHP 的规则:只能包含字母、数字和下划线,并且必须以字母和下划线作为开头。
要注意的是结束标识符这行除了可能有一个分号(;)外,绝对不能包含其它字符。这意味着标识符不能缩进,分号的前后也不能有任何空白或制表符。更重要的是结束标识符的前面必须是个被本地操作系统认可的换行,比如在 UNIX 和 Mac OS X 系统中是 \n,而结束定界符(可能其后有个分号)之后也必须紧跟一个换行。
如果不遵守该规则导致结束标识不“干净”,PHP 将认为它不是结束标识符而继续寻找。如果在文件结束前也没有找到一个正确的结束标识符,PHP 将会在最后一行产生一个解析错误。
Heredocs 结构不能用来初始化类的属性。自 PHP 5.3 起,此限制仅对 heredoc 包含变量时有效。
Example #1 非法的示例
<?php
class foo {
public $bar = <<<EOT
bar
EOT;
}
?>
Heredoc 结构就象是没有使用双引号的双引号字符串,这就是说在 heredoc 结构中单引号不用被转义,但是上文中列出的转义序列还可以使用。变量将被替换,但在 heredoc 结构中含有复杂的变量时要格外小心。
Example #2 Heredoc 结构的字符串示例
<?php
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
class foo
{
var $foo ;
var $bar ;
function foo ()
{
$this -> foo = 'Foo' ;
$this -> bar = array( 'Bar1' , 'Bar2' , 'Bar3' );
}
}
$foo = new foo ();
$name = 'MyName' ;
echo <<<EOT
My name is " $name ". I am printing some $foo -> foo .
Now, I am printing some { $foo -> bar [ 1 ]} .
This should print a capital 'A': \x41
EOT;
?>
以上例程会输出:
My name is "MyName". I am printing some Foo. Now, I am printing some Bar2. This should print a capital 'A': A
也可以把 Heredoc 结构用在函数参数中来传递数据:
Example #3 Heredoc 结构在参数中的示例
<?php
var_dump (array(<<<EOD
foobar!
EOD
));
?>
在 PHP 5.3.0 以后,也可以用 Heredoc 结构来初始化静态变量和类的属性和常量:
Example #4 使用 Heredoc 结构来初始化静态值
<?php
// 静态变量
function foo ()
{
static $bar = <<<LABEL
Nothing in here...
LABEL;
}
// 类的常量、属性
class foo
{
const BAR = <<<FOOBAR
Constant example
FOOBAR;
public $baz = <<<FOOBAR
Property example
FOOBAR;
}
?>
自 PHP 5.3.0 起还可以在 Heredoc 结构中用双引号来声明标识符:
Example #5 在 heredoc 结构中使用双引号
<?php
echo <<<"FOOBAR"
Hello World!
FOOBAR;
?>
就象 heredoc 结构类似于双引号字符串,Nowdoc 结构是类似于单引号字符串的。Nowdoc 结构很象 heredoc 结构,但是 nowdoc 中不进行解析操作。这种结构很适合用于嵌入 PHP 代码或其它大段文本而无需对其中的特殊字符进行转义。与 SGML 的 <![CDATA[ ]]> 结构是用来声明大段的不用解析的文本类似,nowdoc 结构也有相同的特征。
一个 nowdoc 结构也用和 heredocs 结构一样的标记 <<<, 但是跟在后面的标识符要用单引号括起来,即 <<<'EOT'。Heredoc 结构的所有规则也同样适用于 nowdoc 结构,尤其是结束标识符的规则。
Example #6 Nowdoc 结构字符串示例
<?php
$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
class foo
{
public $foo ;
public $bar ;
function foo ()
{
$this -> foo = 'Foo' ;
$this -> bar = array( 'Bar1' , 'Bar2' , 'Bar3' );
}
}
$foo = new foo ();
$name = 'MyName' ;
echo <<<'EOT'
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should not print a capital 'A': \x41
EOT;
?>
以上例程会输出:
My name is "$name". I am printing some $foo->foo. Now, I am printing some {$foo->bar[1]}. This should not print a capital 'A': \x41
Note:
不象 heredoc 结构,nowdoc 结构可以用在任意的静态数据环境中,最典型的示例是用来初始化类的属性或常量:
Example #7 静态数据的示例
<?php
class foo {
public $bar = <<<'EOT'
bar
EOT;
}
?>
Note:
Nowdoc 结构是在 PHP 5.3.0 中加入的。
当 字符串 用双引号或 heredoc 结构定义时,其中的变量将会被解析。
这里共有两种语法规则:一种简单规则,一种复杂规则。简单的语法规则是最常用和最方便的,它可以用最少的代码在一个 string 中嵌入一个变量,一个 array 的值,或一个 object 的属性。
复杂规则语法的显著标记是用花括号包围的表达式。
当 PHP 解析器遇到一个美元符号($)时,它会和其它很多解析器一样,去组合尽量多的标识以形成一个合法的变量名。可以用花括号来明确变量名的界线。
<?php
$juice = "apple" ;
echo "He drank some $juice juice." . PHP_EOL ;
// Invalid. "s" is a valid character for a variable name, but the variable is $juice.
echo "He drank some juice made of $juices ." ;
?>
以上例程会输出:
He drank some apple juice. He drank some juice made of .
类似的,一个 array 索引或一个 object 属性也可被解析。数组索引要用方括号(])来表示索引结束的边际,对象属性则是和上述的变量规则相同。
Example #8 简单语法示例
<?php
$juices = array( "apple" , "orange" , "koolaid1" => "purple" );
echo "He drank some $juices [ 0 ] juice." . PHP_EOL ;
echo "He drank some $juices [ 1 ] juice." . PHP_EOL ;
echo "He drank some juice made of $juice [ 0 ] s." . PHP_EOL ; // Won't work
echo "He drank some $juices [ koolaid1 ] juice." . PHP_EOL ;
class people {
public $john = "John Smith" ;
public $jane = "Jane Smith" ;
public $robert = "Robert Paulsen" ;
public $smith = "Smith" ;
}
$people = new people ();
echo " $people -> john drank some $juices [ 0 ] juice." . PHP_EOL ;
echo " $people -> john then said hello to $people -> jane ." . PHP_EOL ;
echo " $people -> john 's wife greeted $people -> robert ." . PHP_EOL ;
echo " $people -> robert greeted the two $people -> smiths ." ; // Won't work
?>
以上例程会输出:
He drank some apple juice. He drank some orange juice. He drank some juice made of s. He drank some purple juice. John Smith drank some apple juice. John Smith then said hello to Jane Smith. John Smith's wife greeted Robert Paulsen. Robert Paulsen greeted the two .
如果想要表达更复杂的结构,请用复杂语法。
复杂语法不是因为其语法复杂而得名,而是因为它可以使用复杂的表达式。
任何具有 string 表达的标量变量,数组单元或对象属性都可使用此语法。只需简单地像在 string 以外的地方那样写出表达式,然后用花括号 { 和 } 把它括起来即可。由于 { 无法被转义,只有 $ 紧挨着 { 时才会被识别。可以用 {\$ 来表达 {$。下面的示例可以更好的解释:
<?php
// 显示所有错误
error_reporting ( E_ALL );
$great = 'fantastic' ;
// 无效,输出:?This?is?{?fantastic}
echo "This is { $great }" ;
// 有效,输出: This is fantastic
echo "This is { $great } " ;
echo "This is ${ great } " ;
// 有效
echo "This square is { $square -> width } 00 centimeters broad." ;
// 有效,只有通过花括号语法才能正确解析带引号的键名
echo "This works: { $arr [ 'key' ]} " ;
// 有效
echo "This works: { $arr [ 4 ][ 3 ]} " ;
// 这是错误的表达式,因为就象 $foo[bar] 的格式在字符串以外也是错的一样。
// 换句话说,只有在 PHP 能找到常量 foo 的前提下才会正常工作;这里会产生一个
// E_NOTICE?(undefined?constant) 级别的错误。
echo "This is wrong: { $arr [ foo ][ 3 ]} " ;
// 有效,当在字符串中使用多重数组时,一定要用括号将它括起来
echo "This works: { $arr [ 'foo' ][ 3 ]} " ;
// 有效
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 ()}} " ;
// 无效,输出: This is the return value of getName(): {getName()}
echo "This is the return value of getName(): {getName()}" ;
?>
也可以在字符串中用此语法通过变量来调用类的属性。
<?php
class foo {
var $bar = 'I am bar.' ;
}
$foo = new foo ();
$bar = 'bar' ;
$baz = array( 'foo' , 'bar' , 'baz' , 'quux' );
echo " { $foo -> $bar } \n" ;
echo " { $foo -> $baz [ 1 ]} \n" ;
?>
以上例程会输出:
Note:
函数、方法、静态类变量和类常量只有在 PHP 5 以后才可在 {$} 中使用。然而,只有在该字符串被定义的命名空间中才可以将其值作为变量名来访问。只单一使用花括号 ({}) 无法处理从函数或方法的返回值或者类常量以及类静态变量的值。
<?php
// 显示所有错误
error_reporting ( E_ALL );
class beers {
const softdrink = 'rootbeer' ;
public static $ale = 'ipa' ;
}
$rootbeer = 'A & W' ;
$ipa = 'Alexander Keith\'s' ;
// 有效,输出: I'd like an A & W
echo "I'd like an {${ beers :: softdrink }} \n" ;
// 也有效,输出: I'd like an Alexander Keith's
echo "I'd like an {${ beers :: $ale }} \n" ;
?>
string 中的字符可以通过一个从 0 开始的下标,用类似 array 结构中的方括号包含对应的数字来访问和修改,比如 $str[42] 。可以把 string 当成字符组成的 array 。函数 substr() 和 substr_replace() 可用于操作多于一个字符的情况。
Note: string 也可用花括号访问,比如 $str{42} 。
用超出字符串长度的下标写入将会拉长该字符串并以空格填充。非整数类型下标会被转换成整数。非法下标类型会产生一个
E_NOTICE
级别错误。用负数下标写入字符串时会产生一个 E_NOTICE
级别错误,用负数下标读取字符串时返回空字符串。写入时只用到了赋值字符串的第一个字符。用空字符串赋值则赋给的值是
NULL 字符。
PHP 的字符串在内部是字节组成的数组。因此用花括号访问或修改字符串对多字节字符集很不安全。仅应对单字节编码例如 ISO-8859-1 的字符串进行此类操作。
Example #9 一些字符串示例
<?php
// 取得字符串的第一个字符
$str = 'This is a test.' ;
$first = $str [ 0 ];
// 取得字符串的第三个字符
$third = $str [ 2 ];
// 取得字符串的最后一个字符
$str = 'This is still a test.' ;
$last = $str [ strlen ( $str )- 1 ];
// 修改字符串的最后一个字符
$str = 'Look at the sea' ;
$str [ strlen ( $str )- 1 ] = 'e' ;
?>
自 PHP 5.4 起字符串下标必须为整数或可转换为整数的字符串,否则会发出警告。之前例如 "foo" 的下标会无声地转换成 0。
Example #10 PHP 5.3 和 PHP 5.4 的区别
<?php
$str = 'abc' ;
var_dump ( $str [ '1' ]);
var_dump (isset( $str [ '1' ]));
var_dump ( $str [ '1.0' ]);
var_dump (isset( $str [ '1.0' ]));
var_dump ( $str [ 'x' ]);
var_dump (isset( $str [ 'x' ]));
var_dump ( $str [ '1x' ]);
var_dump (isset( $str [ '1x' ]));
?>
以上例程在PHP 5.3中的输出:
string(1) "b" bool(true) string(1) "b" bool(true) string(1) "a" bool(true) string(1) "b" bool(true)
以上例程在PHP 5.4中的输出:
string(1) "b" bool(true)Warning: Illegal string offset '1.0' in /tmp/t.php on line 7 string(1) "b" bool(false)Warning: Illegal string offset 'x' in /tmp/t.php on line 9 string(1) "a" bool(false) string(1) "b" bool(false)
Note:
用 [] 或 {} 访问任何其它类型(不包括数组或具有相应接口的对象实现)的变量只会无声地返回
NULL
。
Note:
PHP 5.5 增加了直接在字符串原型中用 [] 或 {} 访问字符的支持。
字符串可以用 '.'(点)运算符连接起来,注意 '+'(加号)运算符没有这个功能。更多信息参考字符串运算符。
对于 string 的操作有很多有用的函数。
可以参考字符串函数了解大部分函数,高级的查找与替换功能可以参考正则表达式函数或 Perl 兼容正则表达式函数。
另外还有 URL 字符串函数,也有加密/解密字符串的函数(mcrypt 和 mhash)。
最后,可以参考字符类型函数。
一个值可以通过在其前面加上 (string) 或用 strval() 函数来转变成字符串。在一个需要字符串的表达式中,会自动转换为 string 。比如在使用函数 echo 或 print 时,或在一个变量和一个 string 进行比较时,就会发生这种转换。类型和类型转换可以更好的解释下面的事情,也可参考函数 settype() 。
一个布尔值 boolean 的 TRUE
被转换成 string 的
"1"。 Boolean 的 FALSE
被转换成
""(空字符串)。这种转换可以在 boolean
和 string 之间相互进行。
一个整数 integer 或浮点数 float 被转换为数字的字面样式的 string (包括 float 中的指数部分)。使用指数计数法的浮点数(4.1E+6)也可转换。
Note:
在脚本的区域(category LC_NUMERIC)中定义了十进制小数点字符。参见 setlocale() 。
数组 array 总是转换成字符串 "Array",因此, echo 和 print 无法显示出该 数组 的内容。要显示某个单元,可以用 echo $arr['foo'] 这种结构。要显示整个数组内容见下文。
在 PHP 4 中对象 object 总是被转换成字符串 "Object",如果为了调试原因需要打印出对象的值,请继续阅读下文。为了得到对象的类的名称,可以用 get_class() 函数。自 PHP 5 起,适当时可以用 __toString 方法。
资源 resource 总会被转变成 "Resource id #1" 这种结构的字符串,其中的 1 是 PHP 在运行时分配给该 resource 的唯一值。不要依赖此结构,可能会有变更。要得到一个 resource 的类型,可以用函数 get_resource_type() 。
NULL
总是被转变成空字符串。
如上面所说的,直接把 array , object 或 resource 转换成 string 不会得到除了其类型之外的任何有用信息。可以使用函数 print_r() 和 var_dump() 列出这些类型的内容。
大部分的 PHP 值可以转变成 string 来永久保存,这被称作串行化,可以用函数 serialize() 来实现。如果 PHP 引擎设定支持 WDDX,PHP 值也可被串行化为格式良好的 XML 文本。
当一个字符串被当作一个数值来取值,其结果和类型如下:
如果该字符串没有包含 '.','e' 或 'E'
并且其数字值在整型的范围之内(由 PHP_INT_MAX
所定义),该字符串将被当成 integer
来取值。其它所有情况下都被作为 float 来取值。
该字符串的开始部分决定了它的值。如果该字符串以合法的数值开始,则使用该数值。否则其值为 0(零)。合法数值由可选的正负号,后面跟着一个或多个数字(可能有小数点),再跟着可选的指数部分。指数部分由 'e' 或 'E' 后面跟着一个或多个数字构成。
<?php
$foo = 1 + "10.5" ; // $foo is float (11.5)
$foo = 1 + "-1.3e3" ; // $foo is float (-1299)
$foo = 1 + "bob-1.3e3" ; // $foo is integer (1)
$foo = 1 + "bob3" ; // $foo is integer (1)
$foo = 1 + "10 Small Pigs" ; // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies" ; // $foo is float (14.2)
$foo = "10.0 pigs " + 1 ; // $foo is float (11)
$foo = "10.0 pigs " + 1.0 ; // $foo is float (11)
?>
更多信息可以参考 Unix 手册中的 strtod(3)。
本节中的示例可以通过复制/粘贴到下面的代码中来显示:
<?php
echo "\$foo== $foo ; type is " . gettype ( $foo ) . "<br />\n" ;
?>
不要想像在 C 语言中的那样,通过将一个字符转换成整数以得到其代码。使用函数 ord() 和 chr() 实现 ASCII 码和字符间的转换。
PHP 中的 string 的实现方式是一个由字节组成的数组再加上一个整数指明缓冲区长度。并无如何将字节转换成字符的信息,由程序员来决定。字符串由什么值来组成并无限制;特别的,其值为 0(“NUL bytes”)的字节可以处于字符串任何位置(不过有几个函数,在本手册中被称为非“二进制安全”的,也许会把 NUL 字节之后的数据全都忽略)。
字符串类型的此特性解释了为什么 PHP 中没有单独的“byte”类型 - 已经用字符串来代替了。返回非文本值的函数 - 例如从网络套接字读取的任意数据 - 仍会返回字符串。
由于 PHP 并不特别指明字符串的编码,那字符串到底是怎样编码的呢?例如字符串 "á" 到底是等于 "\xE1"(ISO-8859-1),"\xC3\xA1"(UTF-8,C form),"\x61\xCC\x81"(UTF-8,D form)还是任何其它可能的表达呢?答案是字符串会被按照该脚本文件相同的编码方式来编码。因此如果一个脚本的编码是 ISO-8859-1,则其中的字符串也会被编码为 ISO-8859-1,以此类推。不过这并不适用于激活了 Zend Multibyte 时;此时脚本可以是以任何方式编码的(明确指定或被自动检测)然后被转换为某种内部编码,然后字符串将被用此方式编码。注意脚本的编码有一些约束(如果激活了 Zend Multibyte 则是其内部编码)- 这意味着此编码应该是 ASCII 的兼容超集,例如 UTF-8 或 ISO-8859-1。不过要注意,依赖状态的编码其中相同的字节值可以用于首字母和非首字母而转换状态,这可能会造成问题。
当然了,要做到有用,操作文本的函数必须假定字符串是如何编码的。不幸的是,PHP 关于此的函数有很多变种:
最后,要书写能够正确使用 Unicode 的程序依赖于很小心地避免那些可能会损坏数据的函数。要使用来自于 intl 和 mbstring 扩展的函数。不过使用能处理 Unicode 编码的函数只是个开始。不管用何种语言提供的函数,最基本的还是了解 Unicode 规格。例如一个程序如果假定只有大写和小写,那可是大错特错。
[#1] cnbk201 at gmail dot com [2015-01-12 22:07:20]
Small note to consider in heredoc multiple dimension array will not work and neither will any native language functions
<?php
$a[1] = "man";
$b['man'] = "player";
echo <<<ED
$b[$a[1]] // will result in error
substr($a[1], 1) // will result in substr(man, 1)
ED;
?>
[#2] Anonymous [2014-03-31 15:33:45]
$my_int = "12,140";
echo 1 + $my_int ;
Returns 13 not the expected 12141
[#3] Ray.Paseur often uses Gmail [2014-03-08 13:14:16]
In Example #8, above, consider the risk to the script if a programmer were to define('koolaid1', 'XYZ'); For this reason it's wise to use quotes around literal-string associative array keys. As written without quotes, PHP should raise a Notice.
[#4] benl39 at free dot fr [2014-03-04 09:14:24]
Note that :
<?php
echo 'error' == 0, '<br>'; // TRUE
echo 'error' == '0', '<br>'; // FALSE
echo '0' == 0, '<br>'; // TRUE
// So, 'error' != 'error' ?
?>
[#5] necrodust44 at gmail dot com [2014-03-04 01:52:28]
String conversion to numbers.
Unfortunately, the documentation is not correct.
?The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).?
It is not said and is not shown in examples throughout the documentation that, while converting strings to numbers, leading space characters are ignored, like with the strtod function.
<?php
echo " \v\f \r 1234" + 1; // 1235
var_export ("\v\f \r 1234" == "1234"); // true
?>
However, PHP's behaviour differs even from the strtod's. The documentation says that if the string contains a "e" or "E" character, it will be parsed as a float, and suggests to see the manual for strtod for more information. The manual says
?A hexadecimal number consists of a "0x" or "0X" followed by a nonempty sequence of hexadecimal digits possibly containing a radix character, optionally followed by a binary exponent. A binary exponent consists of a 'P' or 'p', followed by an optional plus or minus sign, followed by a nonempty sequence of decimal digits, and indicates multiplication by a power of 2.?
But it seems that PHP does not recognise the exponent or the radix character.
<?php
echo "0xEp4" + 1; // 15
?>
strtod also uses the current locale to choose the radix character, but PHP ignores the locale, and the radix character is always 2E. However, PHP uses the locale while converting numbers to strings.
With strtod, the current locale is also used to choose the space characters, I don't know about PHP.
[#6] espertalhao04 at hotmail dot com [2013-12-10 10:34:09]
gtisza at gmail dot com
You incorrectly stated that thee documentation doesn't refer anything about the semicolon at the end of the heredocs and nowdocs being interpreted as a "real" semicolon.
If you read carefully, you will notice this, in the 1st sentence of the warning about heredocs:
"It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;)."
Interesting...
It is refering about semicolons...
But wait, there is more:
http://php.net/manual/en/language.basic-syntax.instruction-separation.php
1st sentence says:
"As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement."
So, here says that semicolons are statement separators, basicly...
So, if you put a "real" semicolon at the end of these examples:
<?php
$a=5;
$foo="String";
$bar=array();
$yep=null;
$other=func();
?>
Why shouldn't you put at the end of heredocs and nowdocs?
After all, a heredoc or a nowdoc is simply a string.
You should read more carefully the documentation first before saying any comment.
About serious questions:
I didn't read all comments here, but you can run functions inside strings and heredocs.
And you can even nest them inside {}
Example:
<?php
$f=function($x){$a=func_get_args();unset($a[0]);return call_user_func_array($x,$a);};
$d=0;
echo $b=<<<NUMBERS
4.0909 rounded is: {$f('round',4.0909,$d)}
Time now is: {$f('time')}
Nested heredocs/nowdocs: {$f('sprintf',<<<OTHER
Here is an %s of nested %s
OTHER
,"Example",<<<'NOW'
heredocs and nowdocs
NOW
)}
NUMBERS;
?>
It's not pretty, and is hard to read, but sometimes it is useful to confuse curious people (like minifying the code).
Warning: if any function that runs inside a string or heredoc gives a fatal error, the script MAY continue!
[#7] php at richardneill dot org [2013-02-28 18:20:54]
Leading zeroes in strings are (least-surprise) not treated as octal.
Consider:
$x = "0123" + 0;
$y = 0123 + 0;
echo "x is $x, y is $y"; //prints "x is 123, y is 83"
in other words:
* leading zeros in numeric literals in the source-code are interpreted as "octal", c.f. strtol().
* leading zeros in strings (eg user-submitted data), when cast (implicitly or explicitly) to integer are ignored, and considered as decimal, c.f. strtod().
[#8] mcamiano at ncsu dot edu [2012-10-05 14:43:54]
Regarding the lack of complex expression interpolation, just assign an identity function to a variable and call it:
function id($arg) { return $arg; }
$expr = id;
echo "Field is: {$expr( "1 ". ucfirst('whatzit')) }";
It is slower due to an additional function call, but it does avoid the assignment of a one-shot temporary variable. When there are a lot of very simple value transformations made just for display purposes, it can de-clutter code.
[#9] Denis R. [2012-06-10 11:01:54]
Hi.
I noticed that the documentation does not mention that when you have an XML element which contains a dash (-) in its name can only be accessed using the bracelets notation.
For example:
<xml version="1">
<root>
<element-one>value4element-one</element-one>
</root>
to access the above 'element-one' using SimpleXML you need to use the following:
$simpleXMLObj->root->{'element-one'}
to retrieve the value.
Hope this helps,
Denis R.
[#10] m021 at springtimesoftware dot com [2012-04-01 14:00:09]
Heredoc literals delete any trailing space (tabs and blanks) on each line. This is unexpected, since quoted strings do not do this. This is probably done for historical reasons, so would not be considered a bug.
[#11] gtisza at gmail dot com [2012-01-10 06:32:10]
The documentation does not mention, but a closing semicolon at the end of the heredoc is actually interpreted as a real semicolon, and as such, sometimes leads to syntax errors.
This works:
<?php
$foo = <<<END
abcd
END;
?>
This does not:
<?php
foo(<<<END
abcd
END;
);
// syntax error, unexpected ';'
?>
Without semicolon, it works fine:
<?php
foo(<<<END
abcd
END
);
?>
[#12] sgbeal at googlemail dot com [2011-08-12 04:44:13]
The docs say: "Heredoc text behaves just like a double-quoted string, without the double quotes" but there is a notable hidden exception to that rule: the final newline in the string (the one before closing heredoc token) is elided. i.e. if you have:
$foo = <<<EOF
a
b
c
EOF;
the result is equivalent to "a\nb\nc", NOT "a\nb\nc\n" like the docs imply.
[#13] Michael [2011-05-09 05:56:29]
Just want to mention that if you want a literal { around a variable within a string, for example if you want your output to be something like the following:
{hello, world}
and all that you put inside the {} is a variable, you can do a double {{}}, like this:
$test = 'hello, world';
echo "{{$test}}";
[#14] Ultimater at gmail dot com [2011-04-27 15:18:51]
If you require a NowDoc but don't have support for them on your server -- since your PHP version is less than PHP 5.3.0 -- and you are in need of a workaround, I'd suggest using PHP's __halt_compiler() which is basically a knock-off of Perl's __DATA__ token if you are familiar with it.
Give this a run to see my suggestion in action:
<?php
//set $nowDoc to a string containing a code snippet for the user to read
$nowDoc = file_get_contents(__FILE__,null,null,__COMPILER_HALT_OFFSET__);
$nowDoc=highlight_string($nowDoc,true);
echo <<<EOF
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>NowDoc support for PHP < 5.3.0</title>
<meta name="author" content="Ultimater at gmail dot com" />
<meta name="about-this-page"
content="Note that I built this code explicitly for the
php.net documenation for demonstrative purposes." />
<style type="text/css">
body{text-align:center;}
table.border{background:#e0eaee;margin:1px auto;padding:1px;}
table.border td{padding:5px;border:1px solid #8880ff;text-align:left;
background-color:#eee;}
code ::selection{background:#5f5color:white;}
code ::-moz-selection{background:#5f5;color:white;}
a{color:#33a;text-decoration:none;}
a:hover{color:rgb(3,128,252);}
</style>
</head>
<body>
<h1 style="margin:1px auto;">
<a
href="http://php.net/manual/en/language.types.string.php#example-77">
Example #8 Simple syntax example
</a></h1>
<table class="border"><tr><td>
$nowDoc
</td></tr></table></body></html>
EOF;
__halt_compiler()
//Example code snippet we want displayed on the webpage
//note that the compiler isn't actually stopped until the semicolon
;<?php
$juices = array("apple", "orange", "koolaid1" => "purple");
echo "He drank some $juices[0] juice.".PHP_EOL;
echo "He drank some $juices[1] juice.".PHP_EOL;
echo "He drank some juice made of $juice[0]s.".PHP_EOL; // Won't work
echo "He drank some $juices[koolaid1] juice.".PHP_EOL;
class people {
public $john = "John Smith";
public $jane = "Jane Smith";
public $robert = "Robert Paulsen";
public $smith = "Smith";
}
$people = new people();
echo "$people->john drank some $juices[0] juice.".PHP_EOL;
echo "$people->john then said hello to $people->jane.".PHP_EOL;
echo "$people->john's wife greeted $people->robert.".PHP_EOL;
echo "$people->robert greeted the two $people->smiths."; // Won't work
?>
[#15] dee jay simple 0 0 7 at ge mahl dot com [2011-03-01 12:15:57]
I recently discovered the joys of using heredoc with sprintf and positions. Useful if you want some code to iterate, you can repeat placeholders.
<?php
function getNumber($num = 0) {
$foo = rand(1,20);
return ($foo + $num);
}
function getString() {
$foo = array("California","Oregon","Washington");
shuffle($foo);
return $foo[0];
}
function getDiv() {
$num = getNumber();
$div = sprintf( "<div>%s</div>", getNumber(rand(-5,5)) );
return $div;
}
$string = <<<THESTRING
I like the state of %1\$s <br />
I picked: %2\$d as a number, <br />
I also picked %2\$d as a number again <br />
%3\$s<br />
%3\$s<br />
%3\$s<br />
%3\$s<br />
%3\$s<br />
THESTRING;
$returnText = sprintf( $string, getString(),getNumber(),getDiv() );
echo $returnText;
?>
Expected output of the above code:
I like the state of Oregon
I picked: 15 as a number,
I also picked 15 as a number again
5
5
5
5
5
[#16] saamde at gmail dot com [2010-05-27 15:40:58]
Watch out for the "unexpected T_SL" error. This appears to occur when there is white space just after "<<<EOT" and since it's white space it's real hard to spot the error in your code.
[#17] "Sascha Ziemann" [2009-12-17 01:58:54]
Empty strings seem to be no real strings, because they behave different to strings containing data. Here is an example.
It is possible to change a character at a specific position using the square bracket notation:
<?php
$str = '0';
$str[0] = 'a';
echo $str."\n"; // => 'a'
?>
It is also possible to change a character with does not exist, if the index is "behind" the end of the string:
<?php
$str = '0';
$str[1] = 'a';
echo $str."\n"; // => 0a
?>
But if you do that on an empty string, the string gets silently converted into an array:
<?php
$str = '';
$str[0] = 'a';
echo $str."\n"; // => Array
?>
[#18] shd at earthling dot net [2009-10-20 02:54:24]
If you want a parsed variable surrounded by curly braces, just double the curly braces:
<?php
$foo = "bar";
echo "{{$foo}}";
?>
will just show {bar}. The { is special only if followed by the $ sign and matches one }. In this case, that applies only to the inner braces. The outer ones are not escaped and pass through directly.
[#19] deminy at deminy dot net [2009-09-16 21:08:16]
Although current documentation says 'A string literal can be specified in four different ways: ...', actually there is a fifth way to specify a (binary) string:
<?php $binary = b'This is a binary string'; ?>
The above statement declares a binary string using the 'b' prefix, which is available since PHP 5.2.1. However, it will only have effect as of PHP 6.0.0, as noted on http://www.php.net/manual/en/function.is-binary.php .
[#20] Liesbeth [2009-09-03 01:54:58]
If you need to emulate a nowdoc in PHP < 5.3, try using HTML mode and output capturing. This way '$' or '\n' in your string won't be a problem anymore (but unfortunately, '
<?' will be).
<?php
// Start of script
ob_start(); ?>
A text with 'quotes'
and $$$dollars$$$.
<?php $input = ob_get_contents(); ob_end_clean();
// Do what you want with $input
echo "<pre>" . $input . "</pre>";
?>
[#21] headden at karelia dot ru [2009-06-20 00:43:20]
Here is an easy hack to allow double-quoted strings and heredocs to contain arbitrary expressions in curly braces syntax, including constants and other function calls:
<?php
// Hack declaration
function _expr($v) { return $v; }
$_expr = '_expr';
// Our playground
define('qwe', 'asd');
define('zxc', 5);
$a=3;
$b=4;
function c($a, $b) { return $a+$b; }
// Usage
echo "pre {$_expr(1+2)} post\n"; // outputs 'pre 3 post'
echo "pre {$_expr(qwe)} post\n"; // outputs 'pre asd post'
echo "pre {$_expr(c($a, $b)+zxc*2)} post\n"; // outputs 'pre 17 post'
// General syntax is {$_expr(...)}
?>
[#22] cvolny at gmail dot com [2008-12-02 23:43:36]
I commented on a php bug feature request for a string expansion function and figured I should post somewhere it might be useful:
using regex, pretty straightforward:
<?php
function stringExpand($subject, array $vars) {
// loop over $vars map
foreach ($vars as $name => $value) {
// use preg_replace to match ${`$name`} or $`$name`
$subject = preg_replace(sprintf('/\$\{?%s\}?/', $name), $value,
$subject);
}
// return variable expanded string
return $subject;
}
?>
using eval() and not limiting access to only certain variables (entire current symbol table including [super]globals):
<?php
function stringExpandDangerous($subject, array $vars = array(), $random = true) {
// extract $vars into current symbol table
extract($vars);
$delim;
// if requested to be random (default), generate delim, otherwise use predefined (trivially faster)
if ($random)
$delim = '___' . chr(mt_rand(65,90)) . chr(mt_rand(65,90)) . chr(mt_rand(65,90)) . chr(mt_rand(65,90)) . chr(mt_rand(65,90)) . '___';
else
$delim = '__ASDFZXCV1324ZXCV__'; // button mashing...
// built the eval code
$statement = "return <<<$delim\n\n" . $subject . "\n$delim;\n";
// execute statement, saving output to $result variable
$result = eval($statement);
// if eval() returned FALSE, throw a custom exception
if ($result === false)
throw new EvalException($statement);
// return variable expanded string
return $result;
}
?>
I hope that helps someone, but I do caution against using the eval() route even if it is tempting. I don't know if there's ever a truely safe way to use eval() on the web, I'd rather not use it.
[#23] Obeliks [2008-11-15 20:21:36]
Expectedly
<?php $string[$x] ?>
and
<?php substr($string, $x, 1) ?>
will yield the same result... normally!
However, when you turn on the Function Overloading Feature (http://php.net/manual/en/mbstring.overload.php), this might not be true!
If you use this Overloading Feature with 3rd party software, you should check for usage of the String access operator, otherwise you might be in for some nasty surprises.
[#24] Salil Kothadia [2008-10-15 01:33:06]
An interesting finding about Heredoc "syntax error, unexpected $end".
I got this error because I did not use the php close tag "?>" and I had no code after the heredoc code.
foo1.php code gives "syntax error, unexpected $end".
But in foo2.php and foo3.php, when you add a php close tag or when you have some more code after heredoc it works fine.
Example Code:
foo1.php
1.
<?php
2. $str = <<<EOD
3. Example of string
4. spanning multiple lines
5. using heredoc syntax.
6. EOD;
7.
foo2.php
1. <?php
2. $str = <<<EOD
3. Example of string
4. spanning multiple lines
5. using heredoc syntax.
6. EOD;
7.
8. echo $str;
9.
foo3.php
1. <?php
2. $str = <<<EOD
3. Example of string
4. spanning multiple lines
5. using heredoc syntax.
6. EOD;
7. ?>
[#25] steve at mrclay dot org [2008-09-30 13:33:30]
Simple function to create human-readably escaped double-quoted strings for use in source code or when debugging strings with newlines/tabs/etc.
<?php
function doubleQuote($str) {
$ret = '"';
for ($i = 0, $l = strlen($str); $i < $l; ++$i) {
$o = ord($str[$i]);
if ($o < 31 || $o > 126) {
switch ($o) {
case 9: $ret .= '\t'; break;
case 10: $ret .= '\n'; break;
case 11: $ret .= '\v'; break;
case 12: $ret .= '\f'; break;
case 13: $ret .= '\r'; break;
default: $ret .= '\x' . str_pad(dechex($o), 2, '0', STR_PAD_LEFT);
}
} else {
switch ($o) {
case 36: $ret .= '\$'; break;
case 34: $ret .= '\"'; break;
case 92: $ret .= '\\\\'; break;
default: $ret .= $str[$i];
}
}
}
return $ret . '"';
}
?>
[#26] chAlx at findme dot if dot u dot need [2008-09-11 08:42:47]
To save Your mind don't read previous comments about dates ;)
When both strings can be converted to the numerics (in ("$a" > "$b") test) then resulted numerics are used, else FULL strings are compared char-by-char:
<?php
var_dump('1.22' > '01.23'); // bool(false)
var_dump('1.22.00' > '01.23.00'); // bool(true)
var_dump('1-22-00' > '01-23-00'); // bool(true)
var_dump((float)'1.22.00' > (float)'01.23.00'); // bool(false)
?>
[#27] harmor [2008-09-01 15:05:20]
So you want to get the last character of a string using "String access and modification by character"? Well negative indexes are not allowed so $str[-1] will return an empty string.
<?php
//Tested using: PHP 5.2.5
$str = 'This is a test.';
$last = $str[-1]; //string(0) ""
$realLast = $str[strlen($str)-1]; //string(1) "."
$substr = substr($str,-1); //string(1) "."
echo '<pre>';
var_dump($last);
var_dump($realLast);
var_dump($substr);
[#28] Evan K [2008-02-28 13:03:15]
I encountered the odd situation of having a string containing unexpanded escape sequences that I wanted to expand, but also contained dollar signs that would be interpolated as variables. "$5.25\n", for example, where I want to convert \n to a newline, but don't want attempted interpolation of $5.
Some muddling through docs and many obscenties later, I produced the following, which expands escape sequences in an existing string with NO interpolation.
<?php
// where we do all our magic
function expand_escape($string) {
return preg_replace_callback(
'/\\\([nrtvf]|[0-7]{1,3}|[0-9A-Fa-f]{1,2})?/',
create_function(
'$matches',
'return ($matches[0] == "\\\\") ? "" : eval( sprintf(\'return "%s";\', $matches[0]) );'
),
$string
);
}
// a string to test, and show the before and after
$before = 'Quantity:\t500\nPrice:\t$5.25 each';
$after = expand_escape($before);
var_dump($before, $after);
?>
[#29] rkfranklin+php at gmail dot com [2007-09-26 12:35:45]
If you want to use a variable in an array index within a double quoted string you have to realize that when you put the curly braces around the array, everything inside the curly braces gets evaluated as if it were outside a string. Here are some examples:
<?php
$i = 0;
$myArray[Person0] = Bob;
$myArray[Person1] = George;
// prints Bob (the ++ is used to emphasize that the expression inside the {} is really being evaluated.)
echo "{$myArray['Person'.$i++]}<br>";
// these print George
echo "{$myArray['Person'.$i]}<br>";
echo "{$myArray["Person{$i}"]}<br>";
// These don't work
echo "{$myArray['Person$i']}<br>";
echo "{$myArray['Person'$i]}<br>";
// These both throw fatal errors
// echo "$myArray[Person$i]<br>";
//echo "$myArray[Person{$i}]<br>";
?>
[#30] Richard Neill [2007-05-31 20:31:30]
Unlike bash, we can't do
echo "\a" #beep!
Of course, that would be rather meaningless for PHP/web, but it's useful for PHP-CLI. The solution is simple: echo "\x07"
[#31] og at gams dot at [2007-04-25 17:06:09]
easy transparent solution for using constants in the heredoc format:
DEFINE('TEST','TEST STRING');
$const = get_defined_constants();
echo <<<END
{$const['TEST']}
END;
Result:
TEST STRING
[#32] penda ekoka [2007-04-24 10:14:28]
error control operator (@) with heredoc syntax:
the error control operator is pretty handy for supressing minimal errors or omissions. For example an email form that request some basic non mandatory information to your users. Some may complete the form, other may not. Lets say you don't want to tweak PHP for error levels and you just wish to create some basic template that will be emailed to the admin with the user information submitted. You manage to collect the user input in an array called $form:
<?php
// creating your mailer
$mailer = new SomeMailerLib();
$mailer->from = ' System <mail@yourwebsite.com>';
$mailer->to = 'admin@yourwebsite.com';
$mailer->subject = 'New user request';
// you put the error control operator before the heredoc operator to suppress notices and warnings about unset indices like this
$mailer->body = @<<<FORM
Firstname = {$form['firstname']}
Lastname = {$form['lastname']}
Email = {$form['email']}
Telephone = {$form['telephone']}
Address = {$form['address']}
FORM;
?>
[#33] bryant at zionprogramming dot com [2007-02-27 12:16:27]
As of (at least) PHP 5.2, you can no longer convert an object to a string unless it has a __toString method. Converting an object without this method now gives the error:
PHP Catchable fatal error: Object of class <classname> could not be converted to string in <file> on line <line>
Try this code to get the same results as before:
<?php
if (!is_object($value) || method_exists($value, '__toString')) {
$string = (string)$value;
} else {
$string = 'Object';
}
?>
[#34] fmouse at fmp dot com [2007-02-21 10:20:46]
It may be obvious to some, but it's convenient to note that variables _will_ be expanded inside of single quotes if these occur inside of a double-quoted string. This can be handy in constructing exec calls with complex data to be passed to other programs. e.g.:
$foo = "green";
echo "the grass is $foo";
the grass is green
echo 'the grass is $foo';
the grass is $foo
echo "the grass is '$foo'";
the grass is 'green'
[#35] bishop [2006-03-28 12:58:56]
You may use heredoc syntax to comment out large blocks of code, as follows:
<?php
<<<_EOC
// end-of-line comment will be masked... so will regular PHP:
echo ($test == 'foo' ? 'bar' : 'baz');
echo <<<EOHTML
This is text you'll never see!
EOHTML;
function defintion($params) {
echo 'foo';
}
class definition extends nothing {
function definition($param) {
echo 'do nothing';
}
}
how about syntax errors?; = gone, I bet.
_EOC;
?>
Useful for debugging when C-style just won't do. Also useful if you wish to embed Perl-like Plain Old Documentation; extraction between POD markers is left as an exercise for the reader.
Note there is a performance penalty for this method, as PHP must still parse and variable substitute the string.
[#36] webmaster at rephunter dot net [2005-11-30 08:57:07]
Use caution when you need white space at the end of a heredoc. Not only is the mandatory final newline before the terminating symbol stripped, but an immediately preceding newline or space character is also stripped.
For example, in the following, the final space character (indicated by \s -- that is, the "\s" is not literally in the text, but is only used to indicate the space character) is stripped:
$string = <<<EOT
this is a string with a terminating space\s
EOT;
In the following, there will only be a single newline at the end of the string, even though two are shown in the text:
$string = <<<EOT
this is a string that must be
followed by a single newline
EOT;
[#37] DELETETHIS dot php at dfackrell dot mailshell dot com [2005-11-01 08:05:59]
Just some quick observations on variable interpolation:
Because PHP looks for {? to start a complex variable expression in a double-quoted string, you can call object methods, but not class methods or unbound functions.
This works:
<?php
class a {
function b() {
return "World";
}
}
$c = new a;
echo "Hello {$c->b()}.\n"
?>
While this does not:
<?php
function b() {
return "World";
}
echo "Hello {b()}\n";
?>
Also, it appears that you can almost without limitation perform other processing within the argument list, but not outside it. For example:
<?php
$true = true;
define("HW", "Hello World");
echo "{$true && HW}";
?>
gives: Parse error: parse error, unexpected T_BOOLEAN_AND, expecting '}' in - on line 3
There may still be some way to kludge the syntax to allow constants and unbound function calls inside a double-quoted string, but it isn't readily apparent to me at the moment, and I'm not sure I'd prefer the workaround over breaking out of the string at this point.
[#38] lelon at lelon dot net [2004-10-27 12:01:45]
You can use the complex syntax to put the value of both object properties AND object methods inside a string. For example...
<?php
class Test {
public $one = 1;
public function two() {
return 2;
}
}
$test = new Test();
echo "foo {$test->one} bar {$test->two()}";
?>
Will output "foo 1 bar 2".
However, you cannot do this for all values in your namespace. Class constants and static properties/methods will not work because the complex syntax looks for the '$'.
<?php
class Test {
const ONE = 1;
}
echo "foo {Test::ONE} bar";
?>
This will output "foo {Test::one} bar". Constants and static properties require you to break up the string.
[#39] Jonathan Lozinski [2004-08-06 12:03:58]
A note on the heredoc stuff.
If you're editing with VI/VIM and possible other syntax highlighting editors, then using certain words is the way forward. if you use <<<HTML for example, then the text will be hightlighted for HTML!!
I just found this out and used sed to alter all EOF to HTML.
JAVASCRIPT also works, and possibly others. The only thing about <<<JAVASCRIPT is that you can't add the <script> tags.., so use HTML instead, which will correctly highlight all JavaScript too..
You can also use EOHTML, EOSQL, and EOJAVASCRIPT.
[#40] www.feisar.de [2004-04-28 07:49:00]
watch out when comparing strings that are numbers. this example:
<?php
$x1 = '111111111111111111';
$x2 = '111111111111111112';
echo ($x1 == $x2) ? "true\n" : "false\n";
?>
will output "true", although the strings are different. With large integer-strings, it seems that PHP compares only the integer values, not the strings. Even strval() will not work here.
To be on the safe side, use:
$x1 === $x2
[#41] atnak at chejz dot com [2004-04-11 15:53:50]
Here is a possible gotcha related to oddness involved with accessing strings by character past the end of the string:
$string = 'a';
var_dump($string[2]); // string(0) ""
var_dump($string[7]); // string(0) ""
$string[7] === ''; // TRUE
It appears that anything past the end of the string gives an empty string.. However, when E_NOTICE is on, the above examples will throw the message:
Notice: Uninitialized string offset: N in FILE on line LINE
This message cannot be specifically masked with @$string[7], as is possible when $string itself is unset.
isset($string[7]); // FALSE
$string[7] === NULL; // FALSE
Even though it seems like a not-NULL value of type string, it is still considered unset.