类型
在线手册:中文 英文
PHP手册

字符串

一个字符串 就是由一系列的字符组成,因此,一个字符就是一个字节。这就是说,一个字节只能有256种不同的变化,这也暗示了PHP无 法原生支持Unicode 。 更多信息可参考函数 utf8_encode()utf8_decode()

Note: 一个很长的字符串是没有问题的,PHP对于字符串; 而对字符串长度的限制只和运行PHP程序的该台计 算机的内存大小有关。

语法

一个字符串 通过下面的4种方法来定义:

单引号

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

如果想要输出一个单引号,需在它的前面加个反斜线 (\)。在单引号前或在字符串的结尾处 想要输出反斜线,输入两条 (\\)。注意,如果在任何其它的字符前加了反斜线,反斜线将会被直接输出。

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将对一些特殊的字符进行解析:

Escaped characters
Sequence Meaning
\n 换行 (LF or 0x0A (10) in ASCII)
\r 回车 (CR or 0x0D (13) in ASCII)
\t 水平方向的 tab(HT or 0x09 (9) in ASCII)
\v 竖直方向的 tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
\f 换页 (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\\ 反斜线
\$ 美金dollar标记
\" 双引号
\[0-7]{1,3} 符合该表达式顺序的字符串是一个八进制的字符
\x[0-9A-Fa-f]{1,2} 符合该表达式顺序的字符串是一个十六进制的字符

和单引号 字符串一样, 如果输出上述之外的字符,反斜线会被打印出来。 PHP5.1.1以 前,\{$var} 中的反斜线还不会被显示出来。

用双引号定义的 字符串最重要的特征是变量会被进行,更多信息见字符串解析

Heredoc结构

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

结束时所引用的标识符必须在一行的开始位置, 而且,标识符的命名也要像其它标签一样遵守PHP的规则:只能包含 字母、数字和下划线,并且不能用数字和下划线作为开头。

Warning

要注意的是结束标识符这行除了 可能有一个分号(;)外,绝对不能包括 其它字符。这意味着标识符不能缩进,分号的前后也不能有任何空白或tabs。更重要的是结束标识符的前面必须是个被本地 操作系统认可的新行标签,比如在UNIX和Mac OS X系统中是\n ,而结束标识符(可能有个分号)的后面也必须跟个 新行标签。

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

Heredocs结构不能用来初始化class,而从PHP 5.3以后,则该限制只能用在包含变量的情况下。

Example #1 非法的示例

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

Heredoc结构就象是没有使用双引号的双引号字符串, 这就是说在heredoc结构中引号不用被替换,但是上文中列出的字符 (\n等)也可使用。 变量将被替换,但在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;
?>

Note:

PHP4才引入了Heredoc结构。

Nowdoc结构

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

一个nowdoc结构也用和heredocs结构一样的标记 <<<, 但是跟在后面的标志符要用 单引号括起来,就像<<<'EOT'这样。heredocs结构的所有规则也同样适用于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:

不象 heredocs结构,nowdocs结构可以用在任意的静态数据环境中,最典型的示例是用来初始化类的属性或常量:

Example #7 表态数据的示例

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

Note:

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

变量解析

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

这里共有两种语法规则:一种简单 规则,一种复杂规 则。简单的句法规则是最常用和最方便的, 它可以用最少的代码在一个字符串中加入变量, 数组 值,或 对象属性。

复杂的句法规则是在PHP4以后加入的,被花括号包围的表达式是其明显标记。

简单句法规则

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

<?php
$beer 
'Heineken';
echo 
"$beer's taste is great"//有效;单引号"'"是非法的变量名组成元素
echo "He drank some $beers"//无效;字母s是有效的变量名组成元素,但是这里的变量是$beer
echo "He drank some ${beer}s"// 有效
echo "He drank some {$beer}s"// 有效
?>

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

<?php
// 下面的例子是在字符串中引用数组
// 当数组处于字符串外部时,要把数组的值用括号括起来且不要用花括号{ }

// 显示所有错误
error_reporting(E_ALL);

$fruits = array('strawberry' => 'red''banana' => 'yellow');

// 有效;但是注意在字符串外面不能这样引用数组
echo "A banana is $fruits[banana].";

// 有效
echo "A banana is {$fruits['banana']}.";

// 有效,但是PHP会先寻找常量banana
echo "A banana is {$fruits[banana]}.";

// 无效,要用花括号,这里将会产生一个解析错误
echo "A banana is $fruits['banana'].";

// 有效
echo "A banana is " $fruits['banana'] . ".";

// 有效
echo "This square is $square->width meters broad.";

// 无效,解决方法见复杂结构
echo "This square is $square->width00 centimeters broad.";
?>

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

复杂句法规则

复杂句法规则不是结构复杂而命名,而是因为它可以使用复杂的表达式。

任何想用在字符串中标量变量,数组变量或对象属性都可使用这种方法。 只需简单地像在字符串以外的地方那样写出表达式, 然后用花括号{}把它括起来。 由于 { 无法被转义,只有 $ 要紧挨着 {才会被认出来,可以用 {\$ 来表达 {$。下面的示例可以更好的解释:

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

以上例程会输出:


I am bar.
I am bar.

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

存取和修改字符串中的字符

字符串中的字符可以通过一个以0为开始的,用类似数组结构中的方括号包含对应的数字来查找和修改,比如 $str[42], 可以把 字符串想像数组 。 函数 substr()substr_replace()可以用来实现多于一个字符 的情况。

Note: 字符串为了同样的目的也可以用花括号,比如 $str{42},但是, 在 PHP 5.3.0中不推荐使用这种格式,应该用方括号,就像 $str[42]

Warning

方括号中的数字超出范围将会产生空白。非整数类型被转换成整数,非整数类型转变成整数,非法类型会产生一个 E_NOTICE级别错误,负数在写入时会产生一个E_NOTICE,但读 取的是空字符串。被指定的字符串只有第一个字符可用,空字符串用指定为空字节。下面为英文原文: 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.

Example #8 Some string examples

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

?>

Note:

[]{} 存取其它类型的变量只会返回 NULL.

有用的函数和操作符

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

对于字符串 的操作有很多有用的函数。

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

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

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

转换成字符串

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

一个boolean TRUE 值被转换成 字符串"1"Boolean FALSE 被转换成"" (空的字符串)。 这种转变可以在 boolean字符串 之间往返进行。

一个 整数浮点数 被转变为数字的字面样式的字符串 (包括 浮点数中的指数部分),使用指数计数法的浮点数 (4.1E+6)也可转变。

Note:

在脚本场所(category LC_NUMERIC)定义了小数点,更多可以参考函数 setlocale()

数组转换成 字符串 "Array",因此, echo()print() c无法显示出该数组的值。如果显示一个数组值,可以用 echo $arr['foo']这种结构,更多内容见下文。

在PHP 4中对象被转换成 字符串 "Object", 果为了调试原因需要打印出对象的值,方法见正文。为了得到对象的类的名称,可以用 get_class() 函数。 在PHP5中, 可以用 __toString

资源总会被转变成"Resource id #1"这种结构的 字符串 , 其中的 1 是PHP分配给该资源的独特数字。不用过多关注这种结构,它马上要转变了。为了得到一个 resource类型,可以用函数 get_resource_type()

NULL 总是被转变成空的字符串。

如上面所说的,直接把数组对象资源 转换成 字符串 不会得到超出其自身的更多信息。可以使用函数 print_r()var_dump() 列出这些类型的内容。

大部分的PHP值可以转变成 字符串s 来长期储存,这被称作串行化,可以用函数 serialize() 来实现。 如果PHP引擎设定支持 WDDX , PHP值也可储存成XML格式。

字符串转变成数字

当一个字符串 被用在了一个数字的环境中,结果和类型如下:

如果字符串 没有包含 '.','e'或'E' 并且数字值符合整数类型的限定 ( PHP_INT_MAX定义的), 这个 字符串 可被认定是一个 integer, 在其它情况下被认定为一个float

字符串的开始部分给定了它的值,如果 字符串 以合法的数字开始,这个数字可直接使用。 否则,值就 是 0 (零)。 合法数值由符号,后面跟着一个或多个数字(可能有个小数点),再跟着可选的指数符号如'e' 或 'E',后面跟着一个或多个数字。

<?php
$foo 
"10.5";                // $foo is float (11.5)
$foo "-1.3e3";              // $foo is float (-1299)
$foo "bob-1.3e3";           // $foo is integer (1)
$foo "bob3";                // $foo is integer (1)
$foo "10 Small Pigs";       // $foo is integer (11)
$foo "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手册
PHP手册 - N: 字符串

用户评论:

m021 at springtimesoftware dot com (01-Apr-2012 10:00)

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.

Savchenko Pavel (23-Mar-2012 05:10)

re:
$r = '12345';
print_r($r['1']); // return 2 --  correct, this is element of string
print_r($r['value']); // return 1 -- incorrect, WTF???

$t='value';
echo (int)$t; //return 0
print_r($r['0']); // return 1--  correct, this is element of string

Sergey Kuznetsov (14-Mar-2012 04:58)

$r = '12345';

print_r($r['1']); // return 2 --  correct, this is element of string
print_r($r['value']); // return 1 -- incorrect, WTF???

gtisza at gmail dot com (10-Jan-2012 02:32)

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

MarkSG (19-Sep-2011 09:38)

Actually, HEREDOC works precisely as documented, with nothing hidden at all. It's just that it's one of those things which isn't entirely intuitive to grasp.

A HEREDOC block has to start with the delimiter followed by a newline, and end with a newline followed by the delimiter. That means that the leading and trailing newlines are part of the syntax, not a part of the block. So this

<?php
$foo
= <<<EOF
this
is
a
block
EOF;
?>

is equivalent to this

<?php
$foo
=
"this
is
a
block"
;
?>

or this

<?php
$foo
= "this\nis\na\nblock";
?>

but not this:

<?php
$foo
= "
this
is
a
block
"
;
?>

or this

<?php
$foo
= "\nthis\nis\na\nblock\n";
?>

Viewing it with the syntax highlighting switched on helps to make the difference clearer.

sgbeal at googlemail dot com (12-Aug-2011 12:44)

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.

KOmaSHOOTER at gmx dot de (26-Jul-2011 09:37)

curly brackets for strings
<?php
$test_array
= array("hey","people");
$key = 0;
$key1 = 1;
$letter = 0;
echo(
$test_array[$key]{$letter});
echo(
$test_array[$key1]{$letter});
?>

result: "hp"

Michael (09-May-2011 01:56)

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

Ultimater at gmail dot com (27-Apr-2011 11:18)

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 &lt; 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
?>

dee jay simple 0 0 7 at ge mahl dot com (01-Mar-2011 08:15)

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

saamde at gmail dot com (27-May-2010 11:40)

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.

&#34;Sascha Ziemann&#34; (17-Dec-2009 09:58)

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

shd at earthling dot net (20-Oct-2009 10:54)

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.

deminy at deminy dot net (17-Sep-2009 05:08)

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 .

Liesbeth (03-Sep-2009 09:54)

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

?>

headden at karelia dot ru (20-Jun-2009 08:43)

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(...)}
?>

Jarobman (21-Mar-2009 09:20)

One thing I have noticed (using PHP 5.2.4) is that alphanumeric text starting with letters or underscore without spaces and operators in PHP code without single or double quotes is interpreted as a string.  I noticed this when trying to access a member variable from an object and getting an integer returned on what I thought should have been a syntax error.  The text cannot correspond to most PHP keywords since that would generate a fatal error.

<?php
class MyObject
{
    private
$__internal_array = array();
   
//insert code for overloading __get(), __set(), etc. to
    //access items from $__internal_array
}

$object = new MyObject();

//thought this would give a syntax error, put param returned an int somehow
some_function($object-membervar);

//correct
echo hiImAString;

//correct
echo hello.world._6789;

//correct
thisisastandalonestring;

//correct, outputs 0
echo string+anotherstring-yetanotherstring;

//correct, outputs 1concatenated
echo true.false.concatenated;

//incorrect, syntax error
echo cantdothis.1234567890;

//incorrect, syntax error, use of PHP keywords
echo try.and.do.this.if.you.can.else.break;
?>

Any other combination of the above with operators and other special characters will generate fatal errors.  I think it would be the opinion of many people that this form of a string is not at all practical and should never be used at all.  Personally, I think this looks like a bug, but I could be wrong.  However, this is something else one could watch out for when debugging since this is hard to pick up in code especially when expecting integer or boolean values.

Cheers,
Jarobman

cvolny at gmail dot com (03-Dec-2008 07:43)

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.

Obeliks (16-Nov-2008 04:21)

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.

Salil Kothadia (15-Oct-2008 09:33)

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

steve at mrclay dot org (30-Sep-2008 09:33)

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 . '"';
}
?>

chAlx at findme dot if dot u dot need (11-Sep-2008 04:42)

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)
?>

harmor (01-Sep-2008 11:05)

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);

nullhility at gmail dot com (06-Jun-2008 08:40)

It's also valuable to note the following:

<?php
${date("M")} = "Worked";
echo ${
date("M")};
?>

This is perfectly legal, anything inside the braces is executed first, the return value then becomes the variable name. Echoing the same variable variable using the function that created it results in the same return and therefore the same variable name is used in the echo statement. Have fun ;).

Evan K (28-Feb-2008 09:03)

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);

/* Outputs:
string(34) "Quantity:\t500\nPrice:\t$5.25 each"
string(31) "Quantity:    500
Price:    $5.25 each"
*/

?>

dot dot dot dot dot alexander at gmail dot com (07-Feb-2008 06:31)

I think there's not that much to string comparison as claiming date recognition:

It's simply comparing ordinal values of the characters from the {0} to the {strlen-1} one.
In this case
<?php
$a
= '2007-11-06 15:17:48';
$b = '2007-11-05 15:17:48';

var_dump($a > $b);
?>
mArIo@luigi ~ $: php test.php
bool(true)
here all characters match till it reaches position 9 (the "day")
there, 6 has a bigger ord()inal value than 5

<?php
$a
= 'January 25th, 2008 00:23:38';
$b = 'Janury 24th, 2008 00:23:37'; // ($a > $b) === false
?>
Here when we reach 'r' in "Janury" we see that "a" is "less" than "r" so the example would evaluate as ($a < $b) === true

Here:
<?php
$a
= 'February 1st, 2008 00:23:38';
$b = 'January 25th, 2008 00:23:38';
?>
as expected the letter "F" comes before "J" as an ordinal character, so $a is less than $b
 Even here:
<?php
var_dump
('Z' > 'M'); //bool(true)
?>
it gets confirmed that the string comparison operators >, <, =>, =<, == just do a ordinal character comparison starting from position {0} to the first difference or the end of the string.

rkfranklin+php at gmail dot com (26-Sep-2007 08:35)

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

Richard Neill (01-Jun-2007 04:31)

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"

og at gams dot at (26-Apr-2007 01:06)

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

penda ekoka (24-Apr-2007 06:14)

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;

?>

php at moechofe dot com (01-Apr-2007 04:44)

A simple benchmark to check differents about :
- simple and double quote concatenation and
- double quote and heredoc replacement

<?php

function test_simple_quote_concat()
{
 
$b = 'string';
 
$a  = ' string'.$b.' string'.$b.' srting'.$b;
 
$a .= ' string'.$b.' string'.$b.' string'.$b;
 
$a .= ' string'.$b.' string'.$b.' string'.$b;
 
$a .= ' string'.$b.' string'.$b.' string'.$b;
 
$a .= ' string'.$b.' string'.$b.' string'.$b;
 
$a .= ' string'.$b.' string'.$b.' string'.$b;
 
$a .= ' string'.$b.' string'.$b.' string'.$b;
 
$a .= ' string'.$b.' string'.$b.' string'.$b;
}

function
test_double_quote_concat()
{
 
$b = "string";
 
$a  = " string".$b." string".$b." string".$b;
 
$a .= " string".$b." string".$b." string".$b;
 
$a .= " string".$b." string".$b." string".$b;
 
$a .= " string".$b." string".$b." string".$b;
 
$a .= " string".$b." string".$b." string".$b;
 
$a .= " string".$b." string".$b." string".$b;
 
$a .= " string".$b." string".$b." string".$b;
 
$a .= " string".$b." string".$b." string".$b;
}

function
test_double_quote_replace()
{
 
$b = "string";
 
$a = " string$b string$b string$b
string
$b string$b string$b
string
$b string$b string$b
string
$b string$b string$b
string
$b string$b string$b
string
$b string$b string$b
string
$b string$b string$b
string
$b string$b string$b";
}

function
test_eot_replace()
{
 
$b = <<<EOT
string
EOT;
 
$a = <<<EOT
string{$b} string{$b} string{$b}
string
{$b} string{$b} string{$b}
string
{$b} string{$b} string{$b}
string
{$b} string{$b} string{$b}
string
{$b} string{$b} string{$b}
string
{$b} string{$b} string{$b}
string
{$b} string{$b} string{$b}
string
{$b} string{$b} string{$b}
EOT;
}

$iter = 2000;

for(
$i=0; $i<$iter; $i++ )
 
test_simple_quote_concat();

for(
$i=0; $i<$iter; $i++ )
 
test_double_quote_concat();

for(
$i=0; $i<$iter; $i++ )
 
test_double_quote_replace();

for(
$i=0; $i<$iter; $i++ )
 
test_eot_replace();

?>

I've use xdebug profiler to obtain the followed results:

test_simple_quote_concat : 173ms
test_double_quote_concat : 161ms
test_double_quote_replace : 147ms
test_eot_replace : 130ms

bryant at zionprogramming dot com (27-Feb-2007 08:16)

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

?>

fmouse at fmp dot com (21-Feb-2007 06:20)

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'

bishop (28-Mar-2006 08:58)

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.

webmaster at rephunter dot net (30-Nov-2005 04:57)

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;

DELETETHIS dot php at dfackrell dot mailshell dot com (01-Nov-2005 04:05)

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:

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

lelon at lelon dot net (27-Oct-2004 08:01)

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.

Jonathan Lozinski (06-Aug-2004 08:03)

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.

www.feisar.de (28-Apr-2004 03:49)

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

atnak at chejz dot com (11-Apr-2004 11:53)

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.

vallo at cs dot helsinki dot fi (04-Nov-2002 01:41)

Even if the correct way to handle variables is determined from the context, some things just doesn't work without doing some preparation.

I spent several hours figuring out why I couldn't index a character out of a string after doing some math with it just before. The reason was that PHP thought the string was an integer!

$reference = $base + $userid;
.. looping commands ..
$chartohandle = $reference{$last_char - $i};

Above doesn't work. Reason: last operation with $reference is to store a product of an addition -> integer variable. $reference .=""; (string catenation) had to be added before I got it to work:

$reference = $base + $userid;
$reference .= "";
.. looping commands ..
$chartohandle = $reference{$last_char - $i};

Et voil! Nice stream of single characters.