Tips for PHP Developers Pt. 1

by Mike Willbanks on April 12th, 2006

Every seasoned developer has their tricks of the trade. My good friend and I normally will sit down and talk about web development. We focus normally on security, performance and the best way to do things. I will be posting probably 2 or 3 more tips sections, however since I have limited time here are a few to start!

When checking for an empty string use $str==” or strlen($str)
You should do this because using empty() would return false for a string with the number 0. This is probably not an expected thing that happens.

Do not store escaped output in the database (htmlentities or htmlspecialchars)
This can be a very touchy one for some developers. You should not do this because if you escape some html characters you are now using more characters in the database where it should have allowed to store the full string and lead to user confusion why it didn’t store the whole string after entering it in to the maxlength of a text input. Another reason being, what if you chose to not only use this databases data for the web but for a desktop application or PDF? Now you need to decode the escaped data.

Use Casting to speed up and check input
When you are checking numbers you can cast them to check them quicker and not have to utilize a function call. Take the following:

if ($id = (int) $_GET['id']) {
//will evaluate to true if the number is greater than 0.

You can do the same things with floats, arrays and objects. However you will need to do more validation if it isn’t a number.

Using while(list($k,$v) = each($array)) instead of foreach($array as $k=>$v)
You should do this while iterating through an array because foreach creates a copy instead of actually iterating through the array. Still use foreach if you are using objects. If you are going to need to reuse this array make sure to call reset($array) otherwise you can’t iterate the array!

Use a Singleton Patterns
If you are using a database this is highly beneficial so that you aren’t using the global keyword to pass the connection into a function and also for keeping memory down. See the example for a PHP 4 implementation for a singleton:

class singleton {
	function &singleton($class) {
		static $_instances;
		if (!is_array($_instances)) {
			$_instances = array();
		if (!isset($_instances[$class])) {
			$instances[$class] =& new $class;
		return $instances[$class];
$myclass =& singleton('stdObject');

Use static class methods to group functions
If you have a bunch of functions related to one thing you can group them all in a class and call them statically so they are essentially in a namespace. This helps so you do not clutter the global scope. This also helps with possble naming conflicts. Example:

class myTypes {
	function get_type($var) {
	function check_type($var, $check) {
	function set_type($var, $type) {
$var_get_type = myTypes::get_type($var);

I hope these are helpful for you, feel free to leave comments with additional tips or questions that you may have that I can cover in the next tips blog!

From PHP

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS