在 PHP 中连接 ECHO 语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8109322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:59:05  来源:igfitidea点击:

Concatenating ECHO syntax in PHP

phpsyntaxecho

提问by krembo99

I have made a small function (WordPress), using echo .

我做了一个小功能(WordPress),使用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...

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

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?

...哪个有效。这是解决问题的“正确”方法,还是只是有效的“黑客”?有没有更好的方法?

回答by Brad

Commas are faster.

逗号更快。

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

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

This won't make a huge dent in speed for most applications, but I generally make it a habit to use commas for echoanyway.

对于大多数应用程序来说,这不会对速度产生巨大影响,但我通常习惯于使用逗号echo

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

如果您好奇,这是一个基准: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 echowith ., you concatenate first before echogets to do its job. To do that, each expression needs evaluated first. Consider this:

编辑:现在,这就是事情“失序”的原因。(向所有人道歉,因为我刚刚发现这是一直以来的根本问题。)当您echo使用 时.,您在开始echo工作之前先进行连接。为此,每个表达式都需要先求值。考虑一下:

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

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

PHP 将首先评估(5+5),然后(10+10). 这相当于把它变成这样:

echo 10 . 20;

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

然后这些需要连接,所以它们被转换为字符串并变成这样:

echo "1020";

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:

那有意义吗?现在考虑函数previous_post_link()。@Tim 说得对,这个函数没有返回值。当该函数被评估时,它不返回任何内容并回显一些内容。所以如果我们这样做:

echo "test" . previous_post_link();

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.

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

Now, suppose we use commas instead:

现在,假设我们改用逗号:

echo "test", previous_post_link();

PHP evaluates all of the "parameters" for the echoconstruct 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.

PHPecho按顺序计算构造的所有“参数” ,并输出它们。首先"test"是输出,然后previous_post_link()是求值,它有自己的输出,什么都不返回,所以什么都不输出。

I hope this is clearer. Post if not.

我希望这更清楚。没有的话发帖。

回答by Tim

The issue is that the WordPressprevious_post_link('%link',''.$prevthumbnail.'')function actually has its own print command built-in, and it prints after the echo finishes its printing.

问题是WordPressprevious_post_link('%link',''.$prevthumbnail.'')功能实际上内置了自己的打印命令,它会在 echo 完成打印后进行打印。

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 printingthe value returnsit.

如果要在 echo 中使用此命令(或保存到字符串),则必须使用get_previous_posts_link,而不是打印返回它。

回答by Your Common Sense

everything that needs to be EVALUATED in some way (expression, function) will be inevitably "pushed" to the end when using dots?

使用点时,需要以某种方式评估的所有内容(表达式、函数)都将不可避免地“推”到最后?

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:

看来您正在混合两件事 - 评估和回应。
连接时,所有表达式依次计算:

function aplus($b){
  global $a;
  $a += $b;
}

$a=1;

echo $a."|".aplus(1).$a."||".aplus(1).$a;

while if you are of bad practice of mixing echo with statements having output of their own, this separate echo goes first:

如果您不习惯将 echo 与具有自己输出的语句混合在一起,那么这个单独的 echo 会首先出现:

function e($s){
  echo $s;
}

$a=1;

echo $a."|".e($a +1)."||".e($a+2);

回答by gurghet

As a future memo to me:

作为我未来的备忘录:

$squarer = function ($x) { $out = $x*$x; echo "done!\n"; return $out; };

echo 'The square of 2 is ' . $squarer(2) . "!\n";
echo 'The square of 2 is ', $squarer(2), "!\n";

// **** OUTPUT ****
// done!
// The square of 2 is 4!
// The square of 2 is done!
// 4!

回答by Your Common Sense

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.

他说逗号更快。
这不是真的,而且如果一辆新车的成本低 2 美分,就说一辆新车比另一辆便宜也是不对的。有数以千计的差异——服务、礼物、甚至到商店的距离等等——使得 2 美分的差异完全可以忽略不计。一个理智的买家无论如何都不会考虑2美分的差异。
同样在这里。

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 manipulationthat 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 echoas no sane application would use echo for million times.

这个答案只是骗人的,让你想错了。Wordpress 是世界上最慢的应用程序之一。如果真的想加快速度,他们必须做很多分析和速度优化的工作。将逗号更改为点不会在该数字中。
这就是重点:人们了解到逗号更快,并认为“我正在编写快速代码!!!” 虽然这是完全错误的。首先,代码本身总是很快。我不会进行数据操作使您的代码变慢!比如说,Wordpresss 每次被调用时都会解析并加载几兆字节的本地化数据到内存中!将此数据放入某个缓存中将使您的 wordpress 速度提高 2 倍!这就是我要“养成习惯”的东西。
即使您将代码中的所有点都更改为逗号,您也永远无法衡量任何差异。真正的差异,而不是人为的差异。这尤其适用于echo没有理智的应用程序会使用 echo 数百万次。