Your Ad Here

Common PHP Best Practices

Date: 2 Mar 2010 Comments: 2 so far
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in D:\Hosting\3681091\html\blog\wp-content\plugins\digg-digg\dd.class.php on line 759

Warning: file_get_contents(http://feeds.delicious.com/v2/json/urlinfo/data?url=http%3A%2F%2Fwww.devtheweb.net%2Fblog%2F2010%2F03%2F02%2Fcommon-php-best-practices%2F) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in D:\Hosting\3681091\html\blog\wp-content\plugins\digg-digg\dd.class.php on line 759

After posts about php best practices in security and performance, here is a list of common php best practices:

1. Always use the standard php tags, ex.

<?php echo “devtheweb.net blog”; ?> and never use shortcuts when declaring php code, ex.

<?=

echo “devtheweb.net blog”;

?>

or

<?
echo “devtheweb.net blog”;
?>

or even asp.net style

<%

echo “devtheweb.net blog”;

%>

All these declarations are deprecated. So, When you stick to the standard php declaration, it’s guaranteed that it will be supported in future php versions.

2. Document your code

It’s a simple to be done, but it could save you much troubles when you later come back on your code.

3. Upgrade to the latest PHP version regularly

When you upgrade to the latest version, there ara many fixed bugs, enhancements, etc.

4. Use Namespaces

The times when namespaces doesn’t exist in php are in the past. If you use PHP 5.3.0 or later you can use them, here’s an example how you can define a namespace:

<?php

// define this code in the ‘YourNamespace’ namespace
namespace YourNamespace;

// … code …

?>

or

<?php

namespace YourNamespace1;
// php code for the YourNamespace1 namespace

namespace YourNamespace2;
// php code for the YourNamespace2 namespace

// Alternative syntax
namespace YourNamespace3 {

// php code for the Your3 namespace
}

?>

5. Always Validate Cookie Data

The cookie data is passed on the web, so it can be harmful. You can validate it using the mysql_real_escape_string() or htmlspecialchars().

6. Tier your Code

To tier your code it means to separate the different components of your code into different parts. This will allow you to make future chages in your code easily.

7. Define all configuration parameters in a single config file

This will allow easily to exchange the config file to reflect settings for your local development site.

8. Code to a standard

The main reason to code to a standard is that PHP is loosely-typed languages and without a proper coding standard, code will look like huge piles of garbage.

That’s all. I hope you’ve found something useful in the tips above :)

P.S. If you like my php post you can check out my blog regularly, I post at least one php related article a week :)

  1. 2 Comments to “Common PHP Best Practices”

    1. Tom says:

      #correction
      > 4. Use Namespaces
      >
      > The times when namespaces doesn’t exist in php are in the past. If you use PHP 3.1 or later you can use them

      Namespaces have been around since PHP 5.3, not 3.1

    Leave a Reply


    Spam protection by WP Captcha-Free