PHP 中的连接 ECHO 语法
我使用 echo 制作了一个小功能(WordPress)。
/* .. Some code */
switch ($linktype) {
case "next":
echo '<p class="next">' . previous_post_link('%link',''.$prevthumbnail.'') . '</p>';
break;
case "prev":
echo '<p class="prev">' . next_post_link('%link',''.$nextthumbnail.'') . '</p>';
break;
}
/* .. Some other code*/
使用我知道的“常规”串联语法...
echo '<p class="next">'. previous_post_link('%link',''.$prevthumbnail.'') . '</p>';
...产生...
<p class="next"></p>< result of previous_post_link() >
我显然需要 < previous_post_link() >
的结果。我发现一些帖子建议将点 ('.') 替换为逗号 (','),所以现在我......
echo '<p class="next">' , previous_post_link('%link',''.$prevthumbnail.'') , '</p>';
...这有效。这是解决问题的“正确”方法,还是只是有效的“黑客”方法?有更好的方法吗?
I have made a small function (WordPress), using echo .
/* .. Some code */
switch ($linktype) {
case "next":
echo '<p class="next">' . previous_post_link('%link',''.$prevthumbnail.'') . '</p>';
break;
case "prev":
echo '<p class="prev">' . next_post_link('%link',''.$nextthumbnail.'') . '</p>';
break;
}
/* .. Some other code*/
Using the "regular" concatenation syntax that I know...
echo '<p class="next">'. previous_post_link('%link',''.$prevthumbnail.'') . '</p>';
...produces...
<p class="next"></p>< result of previous_post_link() >
I obviously need <p class="next">< result of previous_post_link() ></p>
. I have found some post suggesting to replace the dots ('.') with commas (','), so now I have...
echo '<p class="next">' , previous_post_link('%link',''.$prevthumbnail.'') , '</p>';
...which works. Is this a "correct" way to address the problem, or is this just a "hack" that works? Is there a better approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
逗号更快。
echo
构造允许多个“参数”。当您使用逗号echo
时,输出将直接逐段发送到缓冲区。当您使用.
时,必须先连接它。对于大多数应用程序来说,这不会对速度造成巨大影响,但无论如何,我通常都会养成使用逗号来表示 echo 的习惯。
如果您好奇的话,这是一个基准:
http://www.electrictoolbox.com/php-echo-commas-vs-concatenation/
编辑: 现在,这就是事情“失序”的原因。 (向所有人道歉,因为我刚刚发现这一直是根本问题。)当您使用
.
来echo
时,您首先在echo 之前连接
开始完成它的工作。为此,需要首先评估每个表达式。考虑一下:PHP 将首先计算
(5+5)
,然后计算(10+10)
。这相当于把它变成这样:然后这些需要连接起来,所以它们转换成字符串,变成这样:
这样有意义吗?现在考虑函数
previous_post_link()
。 @Tim 说得很对,这个函数没有返回值。当该函数被评估时,它不返回任何内容并回显一些内容。因此,如果我们这样做:首先,对这两件事进行评估。
"test"
已经是一个字符串,但我们需要先运行函数previous_post_link()
来获取其连接的返回值。运行时,previous_post_link()
输出一些内容,但不返回任何内容。然后,"test"
不连接任何内容,并且该连接通过echo
输出。现在,假设我们使用逗号代替:
PHP 按顺序计算
echo
构造的所有“参数”,并输出它们。首先,输出"test"
,然后评估previous_post_link()
,它有自己的输出,并且不返回任何内容,因此没有为其输出任何内容。我希望这更清楚。如果没有的话发帖。
Commas are faster.
The
echo
construct allows multiple "parameters". When youecho
with commas, the output is sent straight to the buffer piece by piece. When you use.
, it has to concatenate first.This won't make a huge dent in speed for most applications, but I generally make it a habit to use commas for
echo
anyway.Here's a benchmark, if you're curious:
http://www.electrictoolbox.com/php-echo-commas-vs-concatenation/
EDIT: Now, here's why things are "out of order". (Apologies to all, as I just now figured out that this was the root question the whole time.) When you
echo
with.
, you concatenate first beforeecho
gets to do its job. To do that, each expression needs evaluated first. Consider this:PHP will first evaluate
(5+5)
and then(10+10)
. This is equivalent to turning it into this:And then these need concatenated, so they are converted to strings and become this:
Does that make sense? Now consider the function
previous_post_link()
. @Tim is quite right that there is no return value from this function. When that function is evaluated, it returns nothing and echos something. So if we do this:First, both things are evaluated.
"test"
is already a string, but we need to run the functionprevious_post_link()
first to get its return value for concatenation. When ran,previous_post_link()
outputs something, and returns nothing."test"
is then concatenated with nothing, and that concatenation is output viaecho
.Now, suppose we use commas instead:
PHP evaluates all of the "parameters" for the
echo
construct in order, and outputs them. First,"test"
is output, and thenprevious_post_link()
is evaluated, which has its own output, and returns nothing, so nothing is output for it.I hope this is clearer. Post if not.
问题是 WordPress
previous_post_link('%link',''.$prevthumbnail .'')
函数实际上内置了自己的打印命令,它在 echo 完成打印后进行打印。如果您想在 echo 中使用此命令(或保存到字符串),则必须使用
get_previous_posts_link
,它不是打印而是返回它的值。The issue is that the WordPress
previous_post_link('%link',''.$prevthumbnail.'')
function actually has its own print command built-in, and it prints after the echo finishes its printing.If you want to use this command within an echo (or to save to a string) you must use
get_previous_posts_link
, which instead of printing the value returns it.作为我未来的备忘录:
As a future memo to me:
我无法重现这种行为。而且,根据我的知识,它应该是相反的:回显(未评估)值首先进行,然后是回显的结果。
看来你正在混合两件事 - 评估和回声。
连接时,所有表达式都会依次求值:
如果您不习惯将 echo 与具有自己输出的语句混合在一起,那么这个单独的 echo 首先执行:
I can't reproduce this behavior. And, according to my knowledge, it should be contrary: echoed (not evaluated) values goes first, and then goes the result of the echo.
it seems you are mixing 2 matters - evaluation and echoing.
when concatenated, all expressions gets evaluated in turn:
while if you are of bad practice of mixing echo with statements having output of their own, this separate echo goes first:
好吧,用一个题外话来反驳布拉德的题外话。
他说逗号更快。
但这是不正确的,并且说一辆新车比另一辆便宜 2 美分也是不正确的。有成千上万的差异——服务、礼物、甚至到商店的距离等等——使得 2 美分的差异完全可以忽略不计。一个理智的买家无论如何都不会考虑 2 美分的差异。
同样在这里。
这个答案只是欺骗性的,会让你以错误的方式思考。 Wordpress 是世界上最慢的应用程序之一。 如果真的想加快速度,他们必须做大量的分析和速度优化工作。将逗号更改为点不会在该数字中。
这就是要点:人们知道逗号更快,并认为“我正在编写快速代码!!!”虽然这是完全错误的。首先,代码本身总是很快。我的数据操作不会让你的代码变慢!比如说,Wordpresss 每次调用时都会解析几兆字节的本地化数据并将其加载到内存中!将此数据放入缓存中将使您的 WordPress 速度提高 2 倍!这就是我“养成习惯”的。
即使您将代码中的所有点更改为逗号,您也永远无法测量任何差异。这是一种真正的差异,而不是人为的差异。这尤其适用于
echo
,因为没有任何正常的应用程序会使用 echo 数百万次。Well, an offtopic to counter Brad's offtopic.
He says that commas are faster.
That is just not true, as well as it's not true to say that one new car is cheaper than another if it costs 2 cents less. There are thousands differencies - service, gifts, even distance to the shop, etc. - making 2 cents difference totally negligible. A sane buyer wouldn't take 2 cents difference into account by any means.
Same here.
This answer is just deceiving, and makes you think wrong way. Wordpress is one of slowest applications in the world. And if one really want to speed it up, they have to do A LOT of job of profiling and speed optimization. An changing commas to dots wouldn't be in that number.
That's the point: one learns that commas are faster and thinks "I am writing fast code!!!" while it's utterly wrong. First, code itself is always fast. I't s data manipulation that makes your code slow! Say, Wordpresss is parsing and loading several-magabyte of localization data into memory every time it's called! Placing this data into some cache will make your wordpress 2 times faster! That's what I'd "make a habit" of.
While even if you change ALL dots in your code to commas, you will never ever be able to measure any difference. A real difference, not an artificial one. That's especially applicable to
echo
as no sane application would use echo for million times.