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!

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

@dani

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;

@dani

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.