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
  • 4,249
  • 4
  • 30
  • 35
suryanaga
  • 3,728
  • 11
  • 36
  • 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
  • 26,265
  • 5
  • 59
  • 89
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
  • 26,156
  • 14
  • 79
  • 87
3

the concatenator in PHP is the . operator

eidsonator
  • 1,319
  • 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
  • 4,249
  • 4
  • 30
  • 35
1

You can use dot to concatenate or

echo "<a href='{prevpost->url()}' class='postnav left nextpost'></a>";
Vlad Bereschenko
  • 338
  • 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
  • 147,647
  • 23
  • 218
  • 272
GautamD31
  • 28,552
  • 10
  • 64
  • 85