logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
Painting
Heart Disease
Horror Literature
Dating
Hiking & Backpacking
SF/Fantasy Books
Healthy Foods


dailyclick
All times in EST

Full Schedule
g
g PHP Site

BellaOnline's PHP Editor

g

How To Increment and Decrement In PHP

Guest Author - Diane Cipollo

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.

Pre-increment
Post-increment
Pre-decrement
Post-decrement

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.

$page_counter++;

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.
C:\sokkit\site\sample3.php3

<?php

$page_counter = 500;
$page_counter++;

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 http://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!

Add How+To+Increment+and+Decrement+In+PHP to Twitter Add How+To+Increment+and+Decrement+In+PHP to Facebook Add How+To+Increment+and+Decrement+In+PHP to MySpace Add How+To+Increment+and+Decrement+In+PHP to Del.icio.us Digg How+To+Increment+and+Decrement+In+PHP Add How+To+Increment+and+Decrement+In+PHP to Yahoo My Web Add How+To+Increment+and+Decrement+In+PHP to Google Bookmarks Add How+To+Increment+and+Decrement+In+PHP to Stumbleupon Add How+To+Increment+and+Decrement+In+PHP to Reddit




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


For FREE email updates, subscribe to the PHP Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2013 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.

g


g features
Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2013 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor