How To Increment and Decrement In PHP

How To Increment and Decrement In PHP
Some of the most frequently used code in PHP or any programming language is the code that will increment or decrement the value of a variable. This code or expression is used in programs as simple as a page counter or as complicated as credit card processing. In a previous tutorial, we discussed PHP operators. In this tutorial you will use one of these four operators.


Let's take a look at the process involved in a simple page counter script. Your first step is to set the beginning number for the counter. You will do this by setting the initial value for the counter variable. You can use any number here. Let's say that in our example we already have had 500 people view this webpage. To create this counter variable we will name the variable $page_counter and give it the initial value of 500. We will use the assignment operator (=) to do this. This is one way to initialize a variable.

$page_counter = 500;

Next we will write the code that will increment the value of this variable by one when the webpage is viewed. This is where we will use the PHP post-increment operator. If you take a look at the chart in the previous tutorial, you will see that this operator is made from the variable name and two plus signs.


Now that we have increased the value of the page counter variable, we will print the new value to the web browser. We can use the echo statement to do this.

echo $page_counter;

Let's test the code. Open your text editor and type in the following code. Now save your webpage. Call it "sample3.php3" (or sample3.php) and put the file in the following location.


$page_counter = 500;

echo $page_counter;


Finally we will test the script. Open the Sokkit Control Panel and RIGHT click on the View Site button. Choose "Open in New Window" from the pop-up list. This will cause the sokkitdefault.html webpage to be displayed in the new window. You should see a message saying "This site is powered by Sokkit 3.4". Now go to the url address line in the web browser. It should say https://localhost/. To test your script, add sample3.php3 at the end of this and click Enter. You will see a white page with the current value of the $page_counter variable which should be 501.

Open the program in the text editor again and change the initial value to 10. Test the script again. You should see the number 11. You can experiment more by replacing the post-increment operator with the pre-increment, pre-decrement and post-decrement operators in your script. What results to you get?

++$page_counter; //pre-increment -- 11

--$page_counter; //pre-decrement -- 9

$page_counter--; //post-decrement -- 9

This site needs an editor - click to learn more!

Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map

Content copyright © 2023 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.