【发布时间】:2017-12-08 15:36:17
【问题描述】:
考虑下面的代码:
<?php
function a($txt) {
b("Cleveland");
}
function b($txt) {
var_dump(debug_backtrace());
}
echo '<pre>';
a("Peter");
'</pre>';
?>
输出:
array(2) {
[0]=>
array(4) {
["file"]=>
string(40) "C:\xampp\htdocs\php_playground\hello.php"
["line"]=>
int(6)
["function"]=>
string(1) "b"
["args"]=>
array(1) {
[0]=>
string(9) "Cleveland"
}
}
[1]=>
array(4) {
["file"]=>
string(40) "C:\xampp\htdocs\php_playground\hello.php"
["line"]=>
int(12)
["function"]=>
string(1) "a"
["args"]=>
array(1) {
[0]=>
string(5) "Peter"
}
}
}
现在,考虑下面的代码:
<?php
function a($txt) {
b("Cleveland");
}
function b($txt) {
var_dump(debug_backtrace());
}
echo '<pre>'.a("Peter").'</pre>';
?>
输出:
array(2) { [0]=> array(4) { ["file"]=> string(40) "C:\xampp\htdocs\php_playground\hello.php" ["line"]=> int(3) ["function"]=> string(1) "b" ["args"]=> array(1) { [0]=> string(9) "Cleveland" } } [1]=> array(4) { ["file"]=> string(40) "C:\xampp\htdocs\php_playground\hello.php" ["line"]=> int(8) ["function"]=> string(1) "a" ["args"]=> array(1) { [0]=> string(5) "Peter" } } }
为什么<pre> 标记的两种不同用法会得到两种不同的输出?
echo '<pre>';
a("Peter");
'</pre>';
与
echo '<pre>'.a("Peter").'</pre>';
我认为我在这两个陈述中没有做任何不同的事情。我在做几乎相同的事情,那为什么我得到不同的输出?
【问题讨论】:
-
'</pre>';完全什么都不做。你可以换成'</bla>';或者只是'bla';,都是一样的。它是一个在PHP代码中丢失的字符串,一个没有任何作用的表达式。