Hi,

$page = 10;
echo 'page ' .$page; //echoes: 10

echo 'backward ' .$backward = $page--; //echoes: 10
echo "<br>"; echo "<br>";
echo 'forward ' .$forward = $page++; //echoes: 9
echo "<br>"; echo "<br>";
Strange!

Why is the decrement failing ?
Since $page = 10, then decrementing that ($backward) should = 9. But echoes 10!

Again, since $page = 10, then incrementing that ($forward) should = 11. But echoes 9!

Dani AI

Generated

Good debugging thread — correctly identified that evaluation order was the source of the surprise, and 's later fix (removing the curly braces) explains the parse error you saw. A few practical notes that add useful context and help avoid the same confusion later.

Curly braces like {...} are part of PHP's string/variable parsing syntax and are not a general grouping operator for arithmetic; using them outside their intended context produces a parse error. Also avoid packing assignment, concatenation and post/pre increment or decrement into a single echo expression — it makes the code hard to read and easy to misinterpret. Compute the previous/next page on separate lines, cast or validate incoming page input to an integer, and clamp it to valid bounds (for example, minimum 1). That makes pagination logic predictable and safe in production.

One more caution: ++/-- on non-numeric strings follows PHP's string-increment rules (which can be surprising), so prefer explicit +1/-1 arithmetic when clarity matters. For official details, see the PHP manual on increment/decrement behavior and on string/variable parsing and operator precedence: Incrementing/decrementing operators and Strings / Variable parsing.

Recommended Answers

All 5 Replies

You are setting the value of $page to $backward, and then decrementing $page.

That's why you will notice that after executing:

echo 'backward ' . $backward = $page--;

The value of $backward is 10, and the value of $page is 9.

If what you are trying to do is decrement $page, and then set the newly decremented value to the $backward variable, you will want to do ++$page and --$page.

// Set the value of $page to $backward, and then increment $page
$backward = $page++;

// Increment $page, and then set the new value to $backward
$backward = ++$page;

Another example:

$page = 10; // We set $page to 10
echo 'backward ' .$backward = $page--; // We set $backward to the value of $page, which is 10, and *then* set $page to 9
echo 'forward ' .$forward = $page++; // We set $forward to the value of $page, which is now 9, and *then* set $page back to 10

Thank you as things are clear now.
What I wanted to do is create a one step backward & forward button on my serp pagination pages.
The + was not working when trying to increment a variable. Do not know why. I think I added a bracket somewhere.
Hence, I resorted to the usual inc & dec later-on by using the two ++ and -- signs and started facing the issue that made me open this thread.
Last night I was half awake. Tonight fully awake and now I see I easily in 5 mins sorted the original issue out. Here it is so you understand what I was trying to achieve:

echo $page_no = 10; echo '<br>';

echo $forward = $page_no+1; echo '<br>';
echo $back = $page_no-1;

I updated my code in my previous post. That is the code I was looking to generate last night.
instead of writing it like it, I wrote like following instead and did not work:

$page_no=10;

    echo $forward = {$page_no}+1;

    echo '<br>';

    echo $page_no; echo '<br>';


    echo $backward = {$page_no}-1; echo '<br>';
    echo '<br>';
    echo '<br>';
    echo $page_no;

Parse error: syntax error, unexpected token "{" in C:... on line 8

When this code failed. I then resorted to the code you see on my original post in this thread. And came across more issues.
Now everything is sorted. The code in my previous post is what I need.

In short, my +1 and -1 failed due to adding brackets. I then later-on resorted to ++ and -- and came across further issues prompting me to open this thread (my original post code).
Today issue sorted as removed the brackets.

Glad you got it sorted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.