Tag Archives: PHP

PHP Benchmark: count() vs. empty()

In my experience, PHP is not a very “fast” language. Opcode and memory caching are important to many large projects, but they will not make up for poor decisions made when writing code. In recent years, we’ve seen the PHP team integrate opcode cache into PHP 5.5, and Facebook investing in HipHop. These are welcome improvements, but we need to be smart about what functions and language constructs we use.

Given an empty array, empty() will return false, and count() will return zero. Given an array with one or more values, empty() will return true, and count() will return the number of values. Either method will produce an effective result in a conditional checking for a boolean value (==), but how do they perform? First, let’s look at the code used for the benchmark.

The Code

The code is simple. In both files, we executed each function/construct one million times and measured the results with Linux’s time command. In addition to testing with an empty array, I also benchmarked count() and empty() on an array with 1,000 values. There was no measurable variance between the performance of an empty array and an array with 1,000 values.


$array = array();
//$array = range(0, 1000);
for ($i = 0; $i < 1000000; $i++) {


$array = array();
//$array = range(0, 1000);
for ($i = 0; $i < 1000000; $i++) {

The Results

I ran the benchmark about 10 times, and each result confirmed that empty() was roughly 20-30 times faster than count().


% time ./empty.php

real    0m0.122s
user    0m0.094s
sys     0m0.009s


% time ./count.php

real    0m2.630s
user    0m0.118s
sys     0m2.494s

Using memcached with WordPress


Today, I decided to implement a popular distributed caching system called memcached.  Memcached allows PHP objects and variables to be stored in memory, which when used properly, can decrease page load time and server load.  Memcached can also work as a central cache for a cluster of servers.  File-based caching systems work by generating static files, and storing them in a local directory to be served in place of the dynamic content.  This can reduce the number of database queries, and help avoid having to process the script with each new request; however, there is a downside. Continue reading Using memcached with WordPress

PHP 5.3.1 is finally here!

PHP LogoAfter four release candidates, we finally have a stable PHP 5.3.1 release.  For those already running PHP 5.3.0, there’s not much that is different, but there are some improvements.  For others, it means there will probably be PHP 5.3 support in cPanel/WHM via EasyApache very soon.

In PHP 5.3.1, the max_file_uploads directive was introduced into php.ini to help prevent DOS (denial of service) attacks via file exhaustion.  max_file_uploads is set at 20 by default.  There were also several other security issues patches, and many bugs fixed in this release.  Links containing additional details are below:

Adding default database table prefixes in Zend Framework 1.9.x

Recently, I began working with Zend Framework 1.9.2 to restructure one of my web sites.  I learned how to create an extension of the Zend_Db_Table_Abstract from a great “Getting Started” tutorial, but I found myself rewriting the table prefix with each and every new table.  Below I explain one solution to this problem, but I am new to Zend Framework, so please feel free to recommend a better, more integrated way of doing this, if you know of anything.

Step 1:  application.ini

Place the following code in your /application/configs/application.ini file:

table_prefix = "webjawns_"

Step 2:  TPC_DB_Table_Abstract class

Create a new class in the library directory. For this example, we’ll use TPC_DB_Table_Abstract as the name, which will be stored in /library/TPC/DB/Table/Abstract.php.

class TPC_DB_Table_Abstract extends Zend_Db_Table_Abstract {
	protected function _setupTableName() {
		global $application;

		$prefix = $application->getOption('table_prefix');

		$this->_name = $prefix . $this->_name;

Step 3:  /application/Bootstrap.php

After that, you’ll want to make sure that the new Abstract.php file is referenced in your /application/Bootstrap.php file.

protected function _initAutoload() {
	$moduleLoader = new Zend_Application_Module_Autoloader( array(
		'namespace' => '',
		'basePath' => APPLICATION_PATH ) );
	$moduleLoader->addResourceType('', '../library/TPC', 'TPC');
	return $moduleLoader;

Step 4:  /application/models/DbTable/NewTable.php

To use the table prefix, we will basically be substituting Zend_Db_Table_Abstract with TPC_DB_Table_Abstract.  See the example below:

class Model_DbTable_NewTable extends TPC_DB_Table_Abstract {
	// Database name will be $prefix + $_name
	protected $_name = 'newtable';

This is just one way to create default table prefixes for each of your tables. I’m sure this isn’t the only way, and there may even be better, easier ways to do this. As an added bonus, we use the TPC_DB_Table_Abstract class to extend the Zend database functionality even further. If anyone else has any ideas about how to use default table prefixes (other than using a constant), please feel free to share them so we can all benefit.

MySQL database class using Singleton design pattern

(Note: The following is a revised tutorial I wrote earlier this year, transferred from one of my old web sites.)

Using the Singleton design pattern is a simple way to limit the number of class instances to one, helps keep database connections organized, and can also save memory.  The PHP code below is a basic example on how to use the Singleton pattern to create a database class.  This class can be used to manage a database connection throughout PHP script execution.  While this is a basic example, many improvements have been made over the original version.  If this is well received, we will be happy to add additional functionality. Click read more to view the class, and learn about the functionality of each part. Continue reading MySQL database class using Singleton design pattern

Top 3 PHP IDEs

What can I say about our beloved IDEs?  To those just learning, this may just seem like another meaningless acronym in a world already filled with pointless acronyms; however, this will save your life … literally!  So, what is an IDE?

IDE stands for Integrated Development Environment.  IDEs allow you to perform a multitude of tasks from one place.  Example uses of an IDE include editing code, debugging an application, and updating SVN.  IDEs don’t have to include all of these features, and they aren’t limited to just these features.

Below I’ve compiled my top three PHP IDEs:

1. Eclipse PDT (Free)

Eclipse PDT - PHP Perspective
Eclipse PDT - PHP Perspective

Eclipse is extremely powerful and supports a multitude of programming languages, most notably Java.  Eclipse PDT includes built-in support for JavaScript, and comes complete with code assist/completion, syntax highlighting, SVN and CVS support via Subclipse, project management features, and more.  The all-in-one package also comes with a PHP debugger. The downside is that it can be kind of slow at times if you are on an older PC.

2. Crimson Editor (Free)

Crimson Editor
Crimson Editor

Alright, so Crimson Editor is more of a “text editor” than anything else; however, it has proven to be quite useful regardless.  Though Crimson Editor hasn’t been actively worked on since 2008, when it was renamed Emerald Editor by another team, it is great for working on dual screens.  I often find myself using Crimson Editor to copy code from one place to another (i.e. Eclipse PDT on screen A, Crimson Editor on screen B).  Crimson Editor also comes equipped with syntax highlighting for many languages, word wrap, project settings, and built-in FTP.  CE is great because it takes up next to nothing as far as RAM and CPU, but it is not as robust as NetBeans or Eclipse PDT.

3. NetBeans (Free)

NetBeans IDE 6.7.1
NetBeans IDE 6.7.1

NetBeans is similar to Eclipse PDT, but is a little less robust in my opinion.  It does, however, offer version control functionality and database support right out of the box, as well as the other key features of any other IDE.  It seems to be a little faster than Eclipse, and has gained traction in the community.  I don’t really use NetBeans too often, but if Eclipse went away, I would start eating NetBeans.

There are many more great IDEs out there, but these have made quite the impression on me and some of my friends.  It really comes down to personal preference, and what you are trying to do.  They all revolve around the same basic principles, and have similar features.  Try each one out, and find out what works best for you.

I encourage everyone to comment and let us know which IDE you use and why you use it.