Bram Vanroy
Bram Vanroy

Reputation: 28447

Different methods of concatenation return different output

I am definitely not a PHP expert, but I would figure that the following snippets output the same HTML. But they don't.

echo '<a href="';
the_permalink();
echo '" title="';
the_title();
echo '"><i class="genericon-standard"></i></a>';

Returns (as it should):

<a href="http://my-site.com/?p=1" title="Hallo wereld!"><i class="genericon-standard"></i></a>

But the much shorter code

echo '<a href="' . the_permalink() . '" title="' . the_title() . '"><i class="genericon-standard"></i></a>';

Returns

http://my-site.com/?p=1Hallo wereld!<a href="" title=""><i class="genericon-standard"></i></a>

Which is not what I want, obviously. Where do I go wrong in the second code (the shorter)?

Upvotes: 0

Views: 116

Answers (3)

rams0610
rams0610

Reputation: 1051

In wordpress get_permalink() and get_the_title() functions shows the values

Upvotes: 0

David Gallardo
David Gallardo

Reputation: 504

I assume you are using Wordpress, so you must use get_permalink() and get_the_title() instead of the_permalink because this function will echo the result and break your string.

Alternatively you can store the permalink in a variable and then concatenate to your string:

$permalink = get_permalink($post->ID); 

Here is the documentation: http://codex.wordpress.org/Function_Reference/the_permalink

Upvotes: 0

xdazz
xdazz

Reputation: 160853

the_permalink() echos the permalink, get_permalink() returns the permalink.

So the second way should be like below:

echo '<a href="' . get_permalink() . '" title="' . get_the_title() . '"><i class="genericon-standard"></i></a>';

Upvotes: 2

Related Questions