Useful Tips For PHP Developer

Oct 15, 2013, by admin

Hi all here are some useful tips for php developers hope you like the post

 

1. Always try to use PHP core functions and classes

Always check out the PHP manual before creating your own function. You can save your time by using php core functions. PHP has very long list of it’s core function. For example there is no need of building an XML parser for RSS feeds; you can take advantage of PHP’s XML parser functions.

2. Always create a configuration file

Never scattered you database connection settings on every php file; you can use a configuration file which will make your code more modular and easier to maintain. Just create one database configuration file and include this in your PHP scripts. if you use single connection file, then in future you can easily change your connection details. It is also useful when you use constant in your script.

3. Always Sanitize Data That Will Go into Your Database

SQL injection is big threat to PHP developers. Most of PHP developers use MYSql databse with PHP. So first you should have full understanding of Injection. There’s a PHP function that can help to avoid sql injection. You can use mysql_real_escape_string. The mysql_real_escape_string function will take a string and sanitize it for you. if you use htmlspecialchars in mysql_real_escape_string that converts reserved HTML characters, it will not only protect your database but also protect your site against cross-site scripting (XSS) attacks.

4. Leave Error Reporting Turned On in Development

During developing your project, always leave error_reporting on which will display errors at run time. These errors will help you to quickly identify from where errors are coming.

make error reproting on

error_reporting(E_ALL);

you can also make error reproting on using below code:

ini_set(‘error_reporting’, E_ALL);

These settings can be changed in your server’s php.ini file or can change in your script by using php ini functions.

5. Don’t Over-Comment Your Code

Proper documentation of your code through comments in your code is a good practice. But it should be in a proper way. There is no need to comment every single line. Just comment the complicated parts of your code so that when your revisits it later you’ll remember what’s going. Never comment simple things like mysql database connection etc.

6. Use Ternary Operators

Always try to use ternary operator instead of an if/else statement. Actually the ternary operator frees up line space and makes your code less cluttered, making it easier to scan. But take care not to use more than one ternary operator in a single statement.

Ternary Operator Example

$yourname= (empty($_POST[‘name’])) ? ‘default’ : $_POST[‘name’];

The above example is similar to this if/else statement

if (empty($_POST[‘name’])) {
$youraction = ‘default’;
} else {
$youraction = $_POST[‘name’];
}

7. Prefer str_replace() over ereg_replace() and preg_replace()

The str_replace() function is much more efficient than regular expressions at replacing strings. In fact, according to Making the Web, str_replace() is 60% more efficient than regular expressions like ereg_replace() and preg_replace(). But if you are using regular expression then ereg_replace() and preg_replace() will be much faster than str_replace().

8. Use isset instead of strlen

If you’re going to be checking the length of a string, use isset instead of strlen. By using isset, your calls will be about 6 to 40 times faster. It should also be noted that by using isset, your call will still be valid if the variable doesn’t exist.

9. Use a PHP Framework

You can use PHP’s frameworks like CakePHP, Zend, Symfony and CodeIgniter that can greatly decrease the time spent developing a website. Actually framewroks are software that bundles with commonly needed functionality that can help speed up development. The fraeworks provides project development platform where you can develop your project much faster.

10. Memcached

If you are going to develop a website that uses a database, Memcached can certainly speed it up. The caching structure for Memcached was first built for the PHP-based blogging website. you can get a detailed tutorial for installing and using memcached with your PHP projects on PHP.net.