tags:

views:

22

answers:

2

cant you?

Instead of

$next_page = $pageNumber + 1;
print '<a href="'.$url.'/page/'.$next_page.'">Next &raquo;</a>';

i would like to write it like this

print '<a href="'.$url.'/page/'.$pageNumber+1.'">Next &raquo;</a>';

the last one gives my parse error. why?

+2  A: 

You need to put the operation between brackets:

print '<a href="'.$url.'/page/'.($pageNumber+1).'">Next &raquo;</a>';

That should do the trick.

To clarify: Right now you are trying to concat a number (1) and a string, PHP can't do that. If you put the expression between brackets, the result becomes a regular variable for the parser, which can be auto typed to a string and thus used in a concatenation.

Blizz
Actually, you're wrong. PHP can concat a number and a string. It can not, however, add two strings, and since concatenation happens before addition, that's what happens.
Mewp
Of course, you're right.
Blizz
+1  A: 

Because of operator precedence. This will work:

print '<a href="'.$url.'/page/'.($pageNumber+1).'">Next &raquo;</a>';
Mewp