phpechoincrement

Add 1 to variable inside of an echo expression


Getting very confused with echoing an HTML anchor with a variable inside.

<?php
echo '&nbsp;&nbsp;<a href="?p=" . $current_page +1" . ">Next</a>';
?>

I've tried so many variations of lost which ones I've tried. One of the attempts was with curly brackets { but still nothing. I know I'm getting my single and double quotes muddled up!

Could somebody please put me straight on this one. Also, what is the rules for apostrophes and quotes in PHP. If I want to echo something, what shall I start it with, an apostrophe or a quote.


Solution

  • <?php
    echo '&nbsp;&nbsp;<a href="?p='.($current_page + 1).'">Next</a>';
    ?>
    

    If you want to do some math of other trickery inside an echo, you will need to surround it in brackets.

    Edit: @DaveRandom points out that the exception to the trickery clause is $var++ and ++$var.

    Edit 2: (This is rare to see something answered ten years ago have an edit request) The brackets around the math operation causes it to be executed prior to the echo statement, not as part of the output in a linear manner.