Yesterday’s article actually got me a little amped up about coding conventions – Comparing a while loop against a foreach loop of an array – because I never thought I would actually have to do a comparison between a while loop and a foreach loop on an array! If we go back and revisit the post, I was reviewing a recent CakePHP commit for an optimization on the Hash class. The code in question is three separate blocks of code that leverage the array_shift function to get the next value in the array with a foreach loop instead.
Category: PHP
-
A Short Rant About Coding Conventions
-
Comparing a while loop against a foreach loop of an array
Are you confused by the title? I was when I first got the idea to even write this blog as well. I was recently perusing the CakePHP change logs and came across an interesting commit – Optimization for Hash Method.The optimization is quite simple, this code:
[code]
while (($key = array_shift($parts)) !== null)
[/code]Is replaced with the following code:
[code]
foreach ($parts as $key)
[/code]This is actually done several times in the commit. It seems that the original developer is really keen on using the array_shift function. Just in seeing this code, I thought the original code was odd to utilize that function so I immediately had to do a comparison and validate the optimization!
-
What I Learned This Summer
Well – it’s Friday and all of the kids are back in school. While this post is being published, I’m probably stuck in traffic! I can’t believe it’s a new school year already, luckily my kids aren’t old enough so it’s just traffic that I need to get used to.This has been a great summer so far and I thought it would be a good idea to summarize the variety of things I’ve learned about – but have not necessarily blogged about…
-
It’s LEMP not LAMP!
I’m sure many of you have heard the term LAMP before – standing for Linux Apache Mysql and PHP. This is a very typical setup for many open source websites. It’s been around for ages. But make way for LEMP. In a recent report by w3techs, a new HTTP server is climbing its way up the ranks called nginx, but it is pronounced Engine-X; hence, the term LEMP – standing for Linux Engine-X (nginx) Mysql and PHP.According to this w3techs report nginx is now used by almost 12.5% of the websites we know what web server they are running on. More significantly a staggering 28.2% (of the 12.5%) websites rank in the top 1,000 worldwide (according to their Alexa ranking).
Along with my recent switch to Amazon EC2, I also decided to switch to a LEMP stack. I thought I should throw out another shout out to HowToForge.com for this AMAZING step-by-step tutorial on installing a LEMP stack with extremely simple to follow instructions on my brand new Ubuntu 12 server.
Check out the step-by-step instructions to setting up your LEMP server.
I think I only had to make one modification to this instruction set before being able to perform one of the apt-get install commands I had to perform an update on the box. Luckily enough, the OS told me exactly what to do!
A friend and former colleague introduced me to nginx a while back with his blog about Setting up WordPress with nginx and FastCGI. This is quite useful for understanding the nginx configuration for a virtual host that requires rewrite rules as nginx currently has no support for .htaccess files!
-
PHP: Require/Include vs Autoloader
Google has long since ingrained into my brain how important every millisecond is when dealing with large amounts of traffic.In this post, I’m going to demonstrate a really simplistic way to improve your PHP website performance. It seems to go against the grain of “old school” vanilla PHP writing, but the results are incredible! By removing the use of require and include and replacing it with a spl_autoload_register function instead, the time savings are more than 10 times!
Not only that, in theory it’s less lines of code!