DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Reactive Programming
  • Auditing Tools for Kubernetes
  • Using Render Log Streams to Log to Papertrail

Trending

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Reactive Programming
  • Auditing Tools for Kubernetes
  • Using Render Log Streams to Log to Papertrail
  1. DZone
  2. Coding
  3. Languages
  4. 6 PHP Optimization Tips

6 PHP Optimization Tips

David Amador user avatar by
David Amador
·
May. 20, 11 · News
Like (0)
Save
Tweet
Share
18.69K Views

Join the DZone community and get the full member experience.

Join For Free

As most languages PHP allows many ways to solve a problem. Some are faster then others. Here are a few ways to improve your PHP code.

Loop

At some point your program will require a loop. And loop is considered as efficiency killer if you have many nested loop (means loop in a loop) as one loop will required to run ‘n’ times and if you have 1 nested loop, this means your program will have to run n2 times. There are many ways we can define a loop in PHP. But how do you know which one is the best way to loop your data? Apparently, using a for loop is better than foreach and while loop if the maximum loop is pre-calculated outside the for loop!

// For each loop the count function is being called.
for($i =0; $i < count($array);$i++){
echo 'This is bad';
}
 
#Better than foreach and while loop
$total = (int)count($array);
for($i =0; $i < $total;$i++){
echo 'This better, max loop was pre-calculated';
}

Single Vs Double Quotes

single(’) quote is faster than double(”) quote. Why? Because PHP will scan through double quote strings for any PHP variables (additional operation). So unless you have a $var inside the string use single quotes.

$var = 'I have some text';
$var2 = "I have some text"; // We dont have vars so single would be good;
$var3 = "I have some $var"; // In this case the double quotes is necessary

Pre increment vs Post increment

In PHP, it seems like pre increment is better than the other ways of performing an increment. Its around 10% better than post increment? The reason? Some said that post increment made certain copy unlike pre increment.

$i++;
++$i;
$i+=1;
$i = $i + 1;

Echo Vs Print

echo is better. But how much better? Its around 12%-20% faster using echo compare to print when there is no $ symbol in the printing string. And around 40-80% faster if there is an $ symbol used in a printing string!

Dot Vs Commas Concatenation

I use dot to concatenate my stuff. Such as the one shown below:

$a = 'PHP  ';
$b = 'Tips';
 
echo $a.$b;

The other way is:

$a = 'PHP ';
$b = 'Tips';
echo $a,$b;

Tests show that dot is more preferable if there are no variables or $ symbol involved which is around 200% faster. On the other hand, commas will help to increase around 20%-35% efficiency when dealing with $ symbols.

explode Vs preg_split

To split a string the usual way is to use explode because it support even on PHP4.0 and that’s also what i was taught on school. The answer in term of efficiency is explode. Split supports regular express and this makes it quite the same comparison between str_replace and preg_replace, anything that have regular expression support will usually be a bit more slower than those that doesn’t support it. It took around 20.563% faster using explode in PHP.

These benchmarks and others can be found here: http://net-beta.net/ubench/

PHP optimization

Published at DZone with permission of David Amador, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Reactive Programming
  • Auditing Tools for Kubernetes
  • Using Render Log Streams to Log to Papertrail

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: