PHP 8.3.0 RC 6 available for testing

String 字符串

一个字符串 string 就是由一系列的字符组成,其中每个字符等同于一个字节。这意味着 PHP 只能支持 256 的字符集,因此不支持 Unicode 。详见字符串类型详解

注意: 在 32 位版本中,string 最大可以达到 2GB(最多 2147483647 字节)。

语法

一个字符串可以用 4 种方式表达:

单引号

定义一个字符串的最简单的方法是用单引号把它包围起来(字符 ')。

要表达一个单引号自身,需在它的前面加个反斜线(\)来转义。要表达一个反斜线自身,则用两个反斜线(\\)。其它任何方式的反斜线都会被当成反斜线本身:也就是说如果想使用其它转义序列例如 \r 或者 \n,并不代表任何特殊含义,就单纯是这两个字符本身。

注意: 不像双引号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))
\e Escape(ASCII 字符集中的 ESC 或 0x1B (27))
\f 换页(ASCII 字符集中的 FF 或 0x0C (12))
\\ 反斜线
\$ 美元标记
\" 双引号
\[0-7]{1,3} 八进制:匹配正则表达式序列 [0-7]{1,3} 的是八进制表示法的字符序列(比如 "\101" === "A"),会静默溢出以适应一个字节(例如 "\400" === "\000"
\x[0-9A-Fa-f]{1,2} 十六进制:匹配正则表达式序列 [0-9A-Fa-f]{1,2} 的是十六进制表示法的一个字符(比如 "\x41" === "A"
\u{[0-9A-Fa-f]+} Unicode:匹配正则表达式 [0-9A-Fa-f]+ 的字符序列是 unicode 码位,该码位能作为 UTF-8 的表达方式输出字符串。序列中必须包含大括号。例如 "\u{41}" === "A"

和单引号字符串一样,转义任何其它字符都会导致反斜线被显示出来。

用双引号定义的字符串最重要的特征是变量会被解析,详见变量解析

Heredoc 结构

第三种表达字符串的方法是用 heredoc 句法结构:<<<。在该运算符之后要提供一个标识符,然后换行。接下来是字符串 string 本身,最后要用前面定义的标识符作为结束标志。

结束标识符可以使用空格或制表符(tab)缩进,此时文档字符串会删除所有缩进。 在 PHP 7.3.0 之前的版本中,结束时所引用的标识符必须在该行的第一列。

而且,标识符的命名也要像其它标签一样遵守 PHP 的规则:只能包含字母、数字和下划线,并且必须以字母和下划线作为开头。

示例 #1 PHP 7.3.0 之后的基础 Heredoc 示例

<?php
// 无缩进
echo <<<END
a
b
c
\n
END;
// 4 空格缩进
echo <<<END
a
b
c
END;

以上示例在 PHP 7.3 中的输出:

 
      a
     b
    c
  a
 b
c

如果结束标识符的缩进超过内容的任何一行的缩进,则将抛出 ParseError 异常:

示例 #2 结束标识符的缩进不能超过正文的任何一行

<?php
echo <<<END
a
b
c
END;

以上示例在 PHP 7.3 中的输出:

 
PHP Parse error:  Invalid body indentation level (expecting an indentation level of at least 3) in example.php on line 4

制表符也可以缩进结束标识符,但是,关于缩进结束标识符和内容, 制表符和空格不能混合使用。在以上任何情况下, 将会抛出 ParseError 异常。 之所以包含这些空白限制,是因为混合制表符和空格来缩进不利于易读性。

示例 #3 内容(空白)和结束标识符的不同缩进

<?php
// 以下所有代码都不起作用。
// 正文(空格)和结束标记(制表符),不同的缩进
{
echo <<<END
a
END;
}
// 在正文中混合空格和制表符
{
echo <<<END
a
END;
}
// 在结束标记中混合空格和制表符
{
echo <<<END
a
END;
}

以上示例在 PHP 7.3 中的输出:

 
PHP Parse error:  Invalid indentation - tabs and spaces cannot be mixed in example.php line 8

内容字符串的结束标识符后面不需要跟分号或者换行符。 例如,从 PHP 7.3.0 开始允许以下代码:

示例 #4 在结束标识符后继续表达式

<?php
$values
= [<<<END
a
b
c
END, 'd e f'];
var_dump($values);

以上示例在 PHP 7.3 中的输出:

 
array(2) {
  [0] =>
  string(11) "a
  b
    c"
  [1] =>
  string(5) "d e f"
}
警告

如果在某一行的开头找到了结束标识符,那么不管它是否是另外一个单词的一部分, 它都可能看作结束标识符并引起 ParseError

示例 #5 字符串内容中的结束标识符往往会导致 ParseError

<?php
$values
= [<<<END
a
b
END ING
END
, 'd e f'];

以上示例在 PHP 7.3 中的输出:

 
PHP Parse error:  syntax error, unexpected identifier "ING", expecting "]" in example.php on line 6

为了避免这个问题,遵循以下简单的规则较为安全: 不要选择正文内容中出现的词作为结束标识符

警告

在 PHP 7.3.0 之前,请务必注意,带有结束标识符的行不能包含除 (;)外的任何其他字符。 这意味着标识符不能缩进,分号的前后也不能有任何空白或制表符。更重要的是结束标识符的前面必须是个被本地操作系统认可的换行,比如在 UNIX 和 macOS 系统中是 \n,而结束定界符之后也必须紧跟一个换行。

如果不遵守该规则导致结束标识不“干净”,PHP 将认为它不是结束标识符而继续寻找。如果在文件结束前也没有找到一个正确的结束标识符,PHP 将会在最后一行产生一个解析错误。

示例 #6 PHP 7.3.0 之前的错误示例

<?php
class foo {
public
$bar = <<<EOT
bar
EOT;
}
// 不能缩进标识符
?>

示例 #7 即使在 PHP 7.3.0 之前也合法的示例

<?php
class foo {
public
$bar = <<<EOT
bar
EOT;
}
?>

Heredocs 结构不能用来初始化类的属性。

Heredoc 结构就象是没有双引号的 string,这就是说在 heredoc 结构中单引号不用被转义,但是上文中列出的转义序列还可以使用。变量将被替换,但在 heredoc 结构中含有复杂的变量时要像 string 一样格外小心。

示例 #8 Heredoc 结构的字符串示例

<?php
$str
= <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

/* 含有变量的更复杂示例 */
class foo
{
var
$foo;
var
$bar;

function
__construct()
{
$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 结构用在函数参数中来传递数据:

示例 #9 Heredoc 结构在参数中的示例

<?php
var_dump
(array(<<<EOD
foobar!
EOD
));
?>

可以用 Heredoc 结构来初始化静态变量和类的属性和常量:

示例 #10 使用 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;
}
?>

还可以在 Heredoc 结构中用双引号来声明标识符:

示例 #11 在 heredoc 结构中使用双引号

<?php
echo <<<"FOOBAR"
Hello World!
FOOBAR;
?>

Nowdoc 结构

就象 heredoc 结构类似于双引号字符串,Nowdoc 结构是类似于单引号字符串的。Nowdoc 结构很象 heredoc 结构,但是 nowdoc 中不进行解析操作。这种结构很适合用于嵌入 PHP 代码或其它大段文本而无需对其中的特殊字符进行转义。与 SGML 的 <![CDATA[ ]]> 结构是用来声明大段的不用解析的文本类似,nowdoc 结构也有相同的特征。

一个 nowdoc 结构也用和 heredocs 结构一样的标记 <<<, 但是跟在后面的标识符要用单引号括起来,即 <<<'EOT'。Heredoc 结构的所有规则也同样适用于 nowdoc 结构,尤其是结束标识符的规则。

示例 #12 Nowdoc 结构字符串示例

<?php
echo <<<'EOD'
Example of string spanning multiple lines
using nowdoc syntax. Backslashes are always treated literally,
e.g. \\ and \'.
EOD;

以上示例会输出:

 
Example of string spanning multiple lines
using nowdoc syntax. Backslashes are always treated literally,
e.g. \\ and \'.

示例 #13 含变量引用的 Nowdoc 字符串示例

<?php

/* 含有变量的更复杂的示例 */
class foo
{
public
$foo;
public
$bar;

function
__construct()
{
$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

示例 #14 静态数据的示例

<?php
class foo {
public
$bar = <<<'EOT'
bar
EOT;
}
?>

注意:

Nowdoc 结构是在 PHP 5.3.0 中加入的。

变量解析

字符串用双引号或 heredoc 结构定义时,其中的变量将会被解析。

这里共有两种语法规则:一种简单规则,一种复杂规则。简单的语法规则是最常用和最方便的,它可以用最少的代码在一个 string 中嵌入一个变量,一个 array 的值,或一个 object 的属性。

复杂规则语法的显著标记是用花括号包围的表达式。

简单语法

当 PHP 解析器遇到一个美元符号($)时,它会和其它很多解析器一样,去组合尽量多的标识以形成一个合法的变量名。可以用花括号来明确变量名的界线。

<?php
$juice
= "apple";

echo
"He drank some $juice juice.".PHP_EOL;
// Unintended. "s" is a valid character for a variable name, so this refers to $juices, not $juice.
echo "He drank some juice made of $juices.";
// Explicitly specify the end of the variable name by enclosing the reference in braces.
echo "He drank some juice made of {$juice}s.";
?>

以上示例会输出:

 
He drank some apple juice.
He drank some juice made of .
He drank some juice made of apples.

类似的,一个 array 索引或一个 object 属性也可被解析。数组索引要用方括号(])来表示索引结束的边际,对象属性则是和上述的变量规则相同。

示例 #15 简单语法示例

<?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 $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 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 .

从 PHP 7.1.0 起,还支持数字索引。

示例 #16 负数索引

<?php
$string
= 'string';
echo
"The character at index -2 is $string[-2].", PHP_EOL;
$string[-3] = 'o';
echo
"Changing the character at index -3 to o gives $string.", PHP_EOL;
?>

以上示例会输出:

 
The character at index -2 is n.
Changing the character at index -3 to o gives strong.

如果想要表达更复杂的结构,请用复杂语法。

复杂(花括号)语法

复杂语法不是因为其语法复杂而得名,而是因为它可以使用复杂的表达式。

任何具有 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 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()}";

// 无效, 输出: C:\folder\{fantastic}.txt
echo "C:\folder\{$great}.txt"
// 有效, 输出: C:\folder\fantastic.txt
echo "C:\\folder\\{$great}.txt"
?>

也可以在字符串中用此语法通过变量来调用类的属性。

<?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";
?>

以上示例会输出:


I am bar.
I am bar.

注意:

函数、方法、静态类变量和类常量可使用 {$} ,在该字符串被定义的命名空间中将其值作为变量名来访问。只单一使用花括号 ({}) 无法处理从函数或方法的返回值或者类常量以及类静态变量的值。

<?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() 可用于操作多于一个字符的情况。

注意: 从 PHP 7.1.0 开始,还支持 string 负偏移量。从 string 尾部到指定位置的偏移量。 以前,负偏移量读取时(返回空 string)会发出 E_NOTICE, 写入时(string 保持不变)会发出 E_WARNING

注意: PHP 8.0.0 之前, 出于同样的目的,可以使用大括号访问 string,例如 $str{42}。 从 PHP 7.4.0 起,此大括号语法被弃用,自 PHP 8.0.0 开始不再受支持。

警告

用超出字符串长度的下标写入将会拉长该字符串并以空格填充。非整数类型下标会被转换成整数。非法下标类型会产生一个 E_WARNING 级别错误。 写入时只用到了赋值字符串的第一个字符。 PHP 7.1.0 开始,用空字符串赋值会导致 fatal 错误;在之前赋给的值是 NULL 字符。

警告

PHP 的字符串在内部是字节组成的数组。因此用花括号访问或修改字符串对多字节字符集很不安全。仅应对单字节编码例如 ISO-8859-1 的字符串进行此类操作。

注意: 从 PHP 7.1.0 开始,对空字符串应用空索引运算符会引发致命错误。 以前是空字符串会被静默转为数组。

示例 #17 一些字符串示例

<?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';

?>

字符串下标必须为整数或可转换为整数的字符串,否则会发出警告。之前类似 "foo" 的下标会无声地转换成 0

示例 #18 字符串无效下标的例子

<?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']));
?>

以上示例会输出:

 
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)

注意:

[]{} 访问任何其它类型(不包括数组或具有相应接口的对象实现)的变量只会无声地返回 null

注意:

可以直接在字符串原型中用 []{} 访问字符。

注意:

PHP 7.4 中弃用在字符串字面量中使用 {} 来访问字符。 PHP 8.0 已移除。

有用的函数和运算符

字符串可以用 '.'(点)运算符连接起来,注意 '+'(加号)运算符没有这个功能。更多信息参考字符串运算符

对于 string 的操作有很多有用的函数。

可以参考字符串函数了解大部分函数,高级的查找与替换功能可以参考 Perl 兼容正则表达式函数

另外还有 URL 字符串函数,也有加密/解密字符串的函数(SodiumHash)。

最后,可以参考字符类型函数

转换成字符串

一个值可以通过在其前面加上 (string) 或用 strval() 函数来转变成字符串。在一个需要字符串的表达式中,会自动转换为 string。这发生在当使用 echoprint 函数,或当变量与 string 进行比较的时候。类型类型转换可以更好的解释下面的事情,也可参考函数 settype()

一个布尔值 booltrue 被转换成 string"1"boolfalse 被转换成 ""(空字符串)。这种转换可以在 boolstring 之间相互进行。

一个整数 int 或浮点数 float 被转换为数字的字面样式的 string(包括 float 中的指数部分)。使用指数计数法的浮点数(4.1E+6)也可转换。

注意:

PHP 8.0.0 起,十进制小数点字符都是一个句号(.)。 而在此之前的版本,在脚本的区域(category LC_NUMERIC) 中定义了十进制小数点字符。参见 setlocale()

数组 array 总是转换成字符串 "Array",因此,echoprint 无法显示出该数组的内容。要显示某个单元,可以用 echo $arr['foo'] 这种结构。要显示整个数组内容见下文。

必须使用魔术方法 __toString 才能将 object 转换为 string

资源 Resource 总会被转变成 "Resource id #1" 这种结构的字符串,其中的 1 是 PHP 在运行时分配给该 resource 的资源数字。 While the exact structure of this string should not be relied on and is subject to change, it will always be unique for a given resource within the lifetime of a script being executed (ie a Web request or CLI process) and won't be reused. 要得到一个 resource 的类型,可以用函数 get_resource_type()

null 总是被转变成空字符串。

如上面所说的,直接把 arrayobjectresource 转换成 string 不会得到除了其类型之外的任何有用信息。可以使用函数 print_r()var_dump() 列出这些类型的内容。

大部分的 PHP 值可以转变成 string 来永久保存,这被称作串行化,可以用函数 serialize() 来实现。

字符串类型详解

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 关于此的函数有很多变种:

  • 某些函数假定字符串是以单字节编码的,但并不需要将字节解释为特定的字符。例如 substr()strpos()strlen()strcmp()。理解这些函数的另一种方法是它们作用于内存缓冲区,即按照字节和字节下标操作。
  • 某些函数被传递入了字符串的编码方式,也可能会假定默认无此信息。例如 htmlentities()mbstring 扩展中的大部分函数。
  • 其它函数使用了当前区域(见 setlocale()),但是逐字节操作。
  • 最后一些函数会假定字符串是使用某特定编码的,通常是 UTF-8。intl 扩展和 PCRE(上例中仅在使用了 u 修饰符时)扩展中的大部分函数都是这样。

最后,要书写能够正确使用 Unicode 的程序依赖于很小心地避免那些可能会损坏数据的函数。要使用来自于 intlmbstring 扩展的函数。不过使用能处理 Unicode 编码的函数只是个开始。不管用何种语言提供的函数,最基本的还是了解 Unicode 规格。例如一个程序如果假定只有大写和小写,那可是大错特错。

add a note

User Contributed Notes 23 notes

up
438
John
6 years ago
I've been a PHP programmer for a decade, and I've always been using the "single-quoted literal" and "period-concatenation" method of string creation. But I wanted to answer the performance question once and for all, using sufficient numbers of iterations and a modern PHP version. For my test, I used:

php -v
PHP 7.0.12 (cli) (built: Oct 14 2016 09:56:59) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

------ Results: -------

* 100 million iterations:

$outstr = 'literal' . $n . $data . $int . $data . $float . $n;
63608ms (34.7% slower)

$outstr = "literal$n$data$int$data$float$n";
47218ms (fastest)

$outstr =<<<EOS
literal$n$data$int$data$float$n
EOS;
47992ms (1.64% slower)

$outstr = sprintf('literal%s%s%d%s%f%s', $n, $data, $int, $data, $float, $n);
76629ms (62.3% slower)

$outstr = sprintf('literal%s%5$s%2$d%3$s%4$f%s', $n, $int, $data, $float, $data, $n);
96260ms (103.9% slower)

* 10 million iterations (test adapted to see which of the two fastest methods were faster at adding a newline; either the PHP_EOL literal, or the \n string expansion):

$outstr = 'literal' . $n . $data . $int . $data . $float . $n;
6228ms (reference for single-quoted without newline)

$outstr = "literal$n$data$int$data$float$n";
4653ms (reference for double-quoted without newline)

$outstr = 'literal' . $n . $data . $int . $data . $float . $n . PHP_EOL;
6630ms (35.3% slower than double-quoted with \n newline)

$outstr = "literal$n$data$int$data$float$n\n";
4899ms (fastest at newlines)

* 100 million iterations (a test intended to see which one of the two ${var} and {$var} double-quote styles is faster):

$outstr = 'literal' . $n . $data . $int . $data . $float . $n;
67048ms (38.2% slower)

$outstr = "literal$n$data$int$data$float$n";
49058ms (1.15% slower)

$outstr = "literal{$n}{$data}{$int}{$data}{$float}{$n}"
49221ms (1.49% slower)

$outstr = "literal${n}${data}${int}${data}${float}${n}"
48500ms (fastest; the differences are small but this held true across multiple runs of the test, and this was always the fastest variable encapsulation style)

* 1 BILLION iterations (testing a completely literal string with nothing to parse in it):

$outstr = 'literal string testing';
23852ms (fastest)

$outstr = "literal string testing";
24222ms (1.55% slower)

It blows my mind. The double-quoted strings "which look so $slow since they have to parse everything for \n backslashes and $dollar signs to do variable expansion", turned out to be the FASTEST string concatenation method in PHP - PERIOD!

Single-quotes are only faster if your string is completely literal (with nothing to parse in it and nothing to concatenate), but the margin is very tiny and doesn't matter.

So the "highest code performance" style rules are:

1. Always use double-quoted strings for concatenation.

2. Put your variables in "This is a {$variable} notation", because it's the fastest method which still allows complex expansions like "This {$var['foo']} is {$obj->awesome()}!". You cannot do that with the "${var}" style.

3. Feel free to use single-quoted strings for TOTALLY literal strings such as array keys/values, variable values, etc, since they are a TINY bit faster when you want literal non-parsed strings. But I had to do 1 billion iterations to find a 1.55% measurable difference. So the only real reason I'd consider using single-quoted strings for my literals is for code cleanliness, to make it super clear that the string is literal.

4. If you think another method such as sprintf() or 'this'.$var.'style' is more readable, and you don't care about maximizing performance, then feel free to use whatever concatenation method you prefer!
up
12
BahmanMD
10 months ago
In PHP 8.2 using ${var} in strings is deprecated, use {$var} instead:

<?php
$juice
= "apple";

// Valid. Explicitly specify the end of the variable name by enclosing it in braces:
echo "He drank some juice made of {$juice}s.";
?>
up
106
gtisza at gmail dot com
11 years ago
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
);
?>
up
25
garbage at iglou dot eu
7 years ago
You can use string like array of char (like C)

$a = "String array test";

var_dump($a);
// Return string(17) "String array test"

var_dump($a[0]);
// Return string(1) "S"

// -- With array cast --
var_dump((array) $a);
// Return array(1) { [0]=> string(17) "String array test"}

var_dump((array) $a[0]);
// Return string(17) "S"

- Norihiori
up
9
vseokdog at gmail dot com
4 years ago
Don't forget about new E_WARNING and E_NOTICE errors from PHP 7.1.x: they have been introduced when invalid strings are coerced using operators expecting numbers (+ - * / ** % << >> | & ^) or their assignment equivalents. An E_NOTICE is emitted when the string begins with a numeric value but contains trailing non-numeric characters, and an E_WARNING is emitted when the string does not contain a numeric value.

Example:
$foo = 1 + "bob-1.3e3";
$foo = "10.2 pigs " + 1.0;
Will produce: Warning: A non-numeric value encountered

Read more: /manual/en/migration71.other-changes.php
up
14
sideshowAnthony at googlemail dot com
7 years ago
Something I experienced which no doubt will help someone . . .
In my editor, this will syntax highlight HTML and the $comment:

$html = <<<"EOD"
<b>$comment</b>
EOD;

Using this shows all the same colour:

$html = <<<EOD
<b>$comment</b>
EOD;

making it a lot easier to work with
up
22
lelon at lelon dot net
19 years ago
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.
up
19
og at gams dot at
16 years ago
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
up
8
Ray.Paseur sometimes uses Gmail
4 years ago
md5('240610708') == md5('QNKCDZO')

This comparison is true because both md5() hashes start '0e' so PHP type juggling understands these strings to be scientific notation. By definition, zero raised to any power is zero.
up
13
steve at mrclay dot org
15 years ago
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 . '"';
}
?>
up
4
php at richardneill dot org
10 years ago
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().
up
0
greenbluemoonlight at gmail dot com
3 years ago
<?php
\\Example # 10 Simple Syntax - Solution for the last "echo" line.

class people {
public
$john = "John Smith";
public
$jane = "Jane Smith";
public
$robert = "Robert Paulsen";

public
$smith = "Smith";
}

$people = new people();

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
\\Outputs: Robert Paulsen greeted the two

/**Solution:**\

echo "$people->robert greeted the two $people->smith\x08s";

\\Will work
\\Outputs: Robert Paulsen greeted the two Smiths

?>
up
2
necrodust44 at gmail dot com
9 years ago
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.
up
5
atnak at chejz dot com
19 years ago
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.
up
3
chAlx at findme dot if dot u dot need
15 years ago
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)
?>
up
1
Richard Neill
16 years ago
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"
up
0
headden at karelia dot ru
14 years ago
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(...)}
?>
up
-4
jonijnm at example dot com
6 years ago
Both should work :(

<?php

class Testing {
public static
$VAR = 'static';
public const VAR =
'const';

public function
sayHelloStatic() {
echo
"hello: {$this::$VAR}";
}

public function
sayHelloConst() {
echo
"hello: {$this::VAR}"; //Parse error: syntax error, unexpected '}', expecting '['
}
}

$obj = new Testing();
$obj->sayHelloStatic();
$obj->sayHelloConst();
up
-4
nospam at nospam dot com
7 years ago
Beware that consistent with "String conversion to numbers":

<?php

if ('123abc' == 123) echo '(intstr == int) incorrectly tests as true.';

// Because one side is a number, the string is incorrectly converted from intstr to int, which then matches the test number.

// True for all conditionals such as if and switch statements (probably also while loops)!

// This could be a huge security risk when testing/using/saving user input, while expecting and testing for only an integer.

// It seems the only fix is for 123 to be a string as '123' so no conversion happens.

?>
up
-6
shd at earthling dot net
14 years ago
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.
up
-7
bishop
17 years ago
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');
/* c-style comment will be masked, as will other heredocs (not using the same marker) */
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.
up
-5
Hayley Watson
5 years ago
Any single expression, however complex, that starts with $ (i.e., a variable) can be {}-embedded in a double-quoted string:

<?php

echo "The expression {$h->q()["x}"]->p(9 == 0 ? 17 : 42)} gets parsed just as well as " . $h->q()["x}"]->p(9 == 0 ? 17 : 42) . " does.";

?>
up
-8
mark at manngo dot net
6 years ago
I though that it would be helpful to add this comment so that the information at least appears on the right page on the PHP site.

Note that if you intend to use a double-quoted string with an associative key, you may run into the T_ENCAPSED_AND_WHITESPACE error. Some regard this as one of the less obvious error messages.

An expression such as:

<?php
$fruit
=array(
'a'=>'apple',
'b'=>'banana',
// etc
);

print
"This is a $fruit['a']"; // T_ENCAPSED_AND_WHITESPACE
?>

will definitely fall to pieces.

You can resolve it as follows:

<?php
print "This is a $fruit[a]"; // unquote the key
print "This is a ${fruit['a']}"; // Complex Syntax
print "This is a {$fruit['a']}"; // Complex Syntax variation
?>

I have a personal preference for the last variation as it is more natural and closer to what the expression would be like outside the string.

It’s not clear (to me, at least) why PHP misinterprets the single quote inside the expression but I imagine that it has something to do with the fact quotes are not part of the value string — once the string is already being parsed the quotes just get in the way … ?
To Top