my channels
business · cars · dieting · diy · dogs · etiquette · fitness · frugal living · green living · health · home business · home improvement · jobs · parenting · self help · travel
more

technology, web design, web development, decrementing, incrementing, php, programming,
technology
web design
more...

PHP - pre vs post incrementing / decrementing


RELATED ARTICLES
How computers work - hardware
Philselfology: 5th in Series - How to Make Art of Creative Communication Work Be...
How to Save Money on Your Home Energy Costs: Part one - How to Conduct an Energy...

PHP - Pre vs Post Incrementing/Decrementing

Looking to shave some time off of page loads, but your logic is as slim as it can get?  Well I can help. Here is an easy fix that can speed up an application through a few tweaks to your PHP code

Depending on your application, you may have many incrementing/decrementing variables strewn about your site.  Let’s review the two, and key points as far as performance.

Post Incrementing/Decrementing

Whenever you use the ++ or — operators after a variable name, this is post incrementing or decrementing, respectively.  What the PHP parser does, is makes a copy of the variable to return, and then adds or subtracts 1 from the value.  This takes more time and resources, since it has to do more work. This should only be used when you need to immediately return the value before it is modified.

Pre Incrementing/Decrementing

Using the ++ or — operators before a variable is pre incrementing or decrementing. This is much faster than it’s counterpat, because it doesn’t need to do anything other than directly modify the variable, and then return it.  You should use this whenever you can!  It can be as much as three times faster than the post method.

Now you may be thinking: “How much can that help?”

Well consider the following:

<?php

$x = 0

while ($x < $some_var) {

//Do something

++$x

}

?>

Now if $some_var is two or three, probably not going to save much. But if it were 25? or 50? It’s not entirely irrational to think that a while loops such as this one could go into the hundreds.  with each loop, the performance gain becomes more and more valuable.  Plus, it’s just good form to is the “pre” method whenever possible.


ABOUT THE AUTHOR
Ryan Drane
IT Professional
Phoenix, AZ

MY STATISTICS
Level : Fz Member  [?]
2 Factoids published
5 followers & subscribers
+ 2 positive votes
MY EXPERT RANKINGS
#80 in technology
#1 in incrementing
#2 in web design
ABOUT THIS ARTICLE
0 comments
Published 14 months ago
+ 1 positive votes
SHARE THIS ARTICLE



NEW ARTICLE ALERTS
Sign up for notifications when new knowledge articles are published in topics relating to this article:
 technology
 web design
 web development
 decrementing
 incrementing
 php
 programming
Email address:

Get published. Earn money. Gain Web cred.
Apply for a writer's account on Factoidz.

Related Articles
Philselfology: 4th in Series - How to Make Art of Creative Communication Work Best with Frugality and DIY

The Digital Darkroom: Introducing GIMP - Part 1

"Batman: Arkham Asylum" - a video game review

The Move From Traditional to Mass Media - How Journalism Is Surviving

Philselfology: 3rd in Series - How to Make Art of Creative Communication Work Best with Frugality and DIY

How to Gain Followers, Traffic and Friends - A Listing of Social Networks

Rules are made to be broken - Rules #2 and #3: Using diagonal and converging lines in landscape composition

How to choose the best website hosting company for your needs

HostGator web hosting: my thoughts after hosting with them for 3 years

LogoYes: professional quality online logo design for under $100 (screenshots)

Republish this article [?]
You may republish this article with proper attribution to the author and Factoidz.
Click to highlight the text, then press Control+C to copy to your clipboard
Popular in Technology
Microsoft Plays Catch-Up In Race of Web Platform

Facts About Phishing: An Internet Hacking Technique

Carbonite vs. Mozy: results of side-by-side test

Beware: Not All Polished Concrete Floors Are Created Equally

The game 'FaceBook Mobsters 2 Vendetta': excellent way to catch a cheating spouse

Advantages and disadvantages of Mac versus Windows

View more Technology articles
Popular in Web Design
Simple Key points to Building a Great Website & Basic HTML You will Need

View more Web Design articles
More Related
Philselfology: 2nd in Series - How to Make Art of Creative Communication Work Best with Frugality and DIY

Online Stock Music and Royalty Free Libraries - all you need to know

Why Microwave Ovens Can Have See - Through Windows

Photography 101: Aperture priority vs shutter priority

Online Dating Site Review - BBW Personal Plus

Microsoft Exchange Server: Error Code Event 465 - Solution

How to optimize your "Google resume"

Computer running slow? Simple PC upgrades to boost speed and performance.

Comments & Questions
Leave comment
You can sign in to comment under your Factoidz account.

Your name:

Email address:

Homepage (optional):

Comment:

Notify me of new comments