2

I want to echo a PHP function with some string literal HTML.

This is how I thought it was done:

echo '<a href="' + $prevpost->url()  + '" class="postnav left nextpost"></a>';

...but that returns nothing. I've tried small variations on where the quotes are etc. but I'm worried I'm barking up the wrong tree and I can't really find what I need from searching.

Note: echo $prevpost->url(); does return the URL I am trying to link to, before anybody asks if that works.

James Mishra
  • 3,720
  • 4
  • 27
  • 31
sanjaypoyzer
  • 3,462
  • 10
  • 33
  • 47
  • 1
    possible duplicate of [PHP string concatenation](http://stackoverflow.com/questions/11441369/php-string-concatenation) – CodeCaster May 30 '13 at 13:15
  • Hi, questions like this can be easily solved by Googling the question title. Always remember to do that first. Thanks. – Pekka Aug 05 '13 at 10:04

6 Answers6

7

The concatenation operator in PHP is . and not +

Maxim Krizhanovsky
  • 25,260
  • 5
  • 51
  • 86
6

Change it to below, in php . (dot) is used as a concatenation operation in php,

echo '<a href="' . $prevpost->url()  . '" class="postnav left nextpost"></a>';
Rikesh
  • 25,621
  • 14
  • 77
  • 86
3

the concatenator in PHP is the . operator

eidsonator
  • 1,289
  • 2
  • 11
  • 25
3

Like other people have mentioned, the PHP concatenation operator is . rather than +.

However, instead of string concatenation, you can use commas when using PHP's echo() function to gain a small speed improvement over concatenation.

Your code would then look like:

echo '<a href="',  $prevpost->url(), '" class="postnav left nextpost"></a>';
James Mishra
  • 3,720
  • 4
  • 27
  • 31
1

You can use dot to concatenate or

echo "<a href='{prevpost->url()}' class='postnav left nextpost'></a>";
Vlad Bereschenko
  • 328
  • 1
  • 3
  • 11
1

In almost any other language we use the + operator, but in php we use the . operator for the concatinating. Try to replace the + with .:

echo '<a href="' . $prevpost->url()  . '" class="postnav left nextpost"></a>';
CodeCaster
  • 139,522
  • 20
  • 204
  • 252
Gautam3164
  • 28,027
  • 10
  • 58
  • 83