Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating ECHO syntax in PHP

Tags:

syntax

php

echo

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?

like image 662
krembo99 Avatar asked Nov 13 '11 03:11

krembo99


People also ask

How do you concatenate in PHP?

The first is the concatenation operator ('. '), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('. ='), which appends the argument on the right side to the argument on the left side.

How do you concatenate 2 strings?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs.

What is the concatenation operator in PHP?

1. Concatenation Operator ("."): In PHP, this operator is used to combine the two string values and returns it as a new string.


2 Answers

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.

like image 85
Brad Avatar answered Oct 02 '22 17:10

Brad


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.

like image 32
Tim Avatar answered Oct 02 '22 15:10

Tim