简体   繁体   中英

Concatenating ECHO syntax in PHP

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?

Commas are faster.

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.

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

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

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

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

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:

$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!

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:

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

$a=1;

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM