在PHP中串联ECHO语法 [英] Concatenating ECHO syntax in PHP

查看:104
本文介绍了在PHP中串联ECHO语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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>';

...产生...

<p class="next"></p>< result of previous_post_link() >

我显然需要<p class="next">< result of previous_post_link() ></p>.我发现一些帖子建议用逗号(',')代替点('.'),所以现在我有了...

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

...有效.这是解决问题的正确"方法,还是仅是可行的"hack"?有更好的方法吗?

...which works. Is this a "correct" way to address the problem, or is this just a "hack" that works? Is there a better approach?

推荐答案

逗号更快.

echo构造允许多个参数".当您使用逗号echo时,输出将直接发送到缓冲区.使用.时,它必须先串联.

The echo construct allows multiple "parameters". When you echo with commas, the output is sent straight to the buffer piece by piece. When you use ., it has to concatenate first.

对于大多数应用程序来说,这不会对速度造成很大的影响,但是无论如何,我通常都会习惯对echo使用逗号.

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.

这是一个基准,如果您好奇的话: http://www.electrictoolbox.com/php-echo-commas-vs-concatenation/

Here's a benchmark, if you're curious: http://www.electrictoolbox.com/php-echo-commas-vs-concatenation/

编辑:现在,这就是事情乱序"的原因. (对所有人表示歉意,正如我刚才一直认为这是整个问题的根本原因.)当您将echo.结合使用时,您首先要在echo完成其工作之前进行连接.为此,需要首先评估每个表达式.考虑一下:

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 before echo gets to do its job. To do that, each expression needs evaluated first. Consider this:

echo (5+5) . (10+10);

PHP首先评估(5+5),然后评估(10+10).这等效于将其转换为:

PHP will first evaluate (5+5) and then (10+10). This is equivalent to turning it into this:

echo 10 . 20;

然后需要将它们串联起来,因此它们被转换为字符串并变为:

And then these need concatenated, so they are converted to strings and become this:

echo "1020";

这有意义吗?现在考虑功能previous_post_link(). @Tim非常正确,此函数没有返回值.对该函数求值时,它不返回任何内容并回显某些内容.因此,如果我们这样做:

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:

echo "test" . previous_post_link();

首先,对这两件事都进行了评估. "test"已经是一个字符串,但是我们需要先运行函数previous_post_link()以获得连接的返回值.运行时,previous_post_link()输出某些内容,但不返回任何内容.然后"test"不进行任何级联,并且该级联通过echo输出.

First, both things are evaluated. "test" is already a string, but we need to run the function previous_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 via echo.

现在,假设我们使用逗号代替:

Now, suppose we use commas instead:

echo "test", previous_post_link();

PHP按顺序评估echo构造的所有参数",并将其输出.首先,输出"test",然后评估previous_post_link(),它具有自己的输出,不返回任何内容,因此没有任何输出.

PHP evaluates all of the "parameters" for the echo construct in order, and outputs them. First, "test" is output, and then previous_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.

这篇关于在PHP中串联ECHO语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆