PHP IF Evaluation & Server Overhead

  • undoIT
  • Novice
  • Novice
  • User avatar
  • Posts: 22

Post 3+ Months Ago

Does anyone know how much server overhead there is when PHP if statements are evaluated, i.e. memory consumption and CPU usage and if this is a major issue? I've searched around and didn't find anything that definitively answered this question.

For example, if I have a WordPress blog and use a lot of if statements to render each post summary on the home page, is this going to require a great deal more server resources if the home page starts getting high traffic? And, on pages where many posts are listed (i.e. category pages) , it seems like this could become an issue.

Or, is this something I shouldn't even be worrying about?
  • Graduate
  • Graduate
  • User avatar
  • Posts: 125
  • Loc: UK

Post 3+ Months Ago

I don't believe the actual 'if' would require much CPU. I think it's what happens as a result of the if that matters.

You could always log in via SSH and run the 'top' command while running your PHP script to check the server usage.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6256
  • Loc: Seattle, WA

Post 3+ Months Ago

undoIT wrote:
Or, is this something I shouldn't even be worrying about?

  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

spork wrote:
undoIT wrote:
Or, is this something I shouldn't even be worrying about?


No :) Sorry Spork I had to (I owed you one :P)

It is kinda like asking "how long is a piece of string?"

An if statement is a vital part of logic flow and if you don't use it the language is pretty useless. As pointed out you should be more concerned about the logic inside the if statement because

Code: [ Select ]
if($var == 0){
echo "Hello";
  1. if($var == 0){
  2. echo "Hello";
  3. }

is going to execute far quicker than
Code: [ Select ]
if($var == 0){
$teaLady = new TeaLady();
$coffeeMachine = new CoffeeMachine();
$customer = new Customer();
$coffeeShop = new CoffeeShop();

if(!($teaLady->getTipsTotal > 0)){
$teaLady->findNewJob(new JobApplication());
  1. if($var == 0){
  2. $teaLady = new TeaLady();
  3. $teaLady->makeTea();
  4. $teaLady->serveTea();
  5. $coffeeMachine = new CoffeeMachine();
  6. $coffeMachine->percolateCoffee($teaLady->grindCoffee);
  7. $teaLady->prepareCoffee($coffeMachine->getCoffee());
  8. $customer = new Customer();
  9. $customer->consumeCoffee($teaLady->serveCoffee());
  10. $coffeeShop = new CoffeeShop();
  11. $coffeeShop->createCustomerReview($customer->getCoffeeReview);
  12. if($coffeeShop->getCustomerReview($cutomer)->getIsHappy()){
  13. $teaLady->addTip($customer->tipTeaLady());
  14. }else{
  15. $teaLady->smileAndWave($customer);
  16. }
  17. if(!($teaLady->getTipsTotal > 0)){
  18. $teaLady->findNewJob(new JobApplication());
  19. }
  20. }

Bit contrived but I think it gets the point across (and if any one complains about this object model I will hunt you down and tattoo nerd on your forehead :))
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13506
  • Loc: Florida

Post 3+ Months Ago

When you have IF statements which depend on visitor details and prevent you from creating static pages in a cache, then IF can become a very costly piece of syntax. Static pages are several orders of magnitude faster to retrieve and serve than dynamic pages are.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 64 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum

© 1998-2015. Ozzu® is a registered trademark of Unmelted, LLC.