10 helpful tips for Novice PHP developers

Learning from my experience, on how I got into PHP development, I wanted to share some ideas of what has helped me master PHP. Hope these tips help some Novice PHP developers which can be also termed as best practices during the time of programming.

1) Comment, Comment, Comment

Using Comment in code shows the sign of good developer. You must have seen at the beginning of the file there are comments and descriptions. That shows useful information about the file or class. There are some information available, like Date, when the code is developed and modified.

Mostly comments define the description about code which means the purpose of the code is written. Any special note, information also added to the comment.

2) Turn on Error Reporting

Sometimes during development, errors, bugs and warnings are not displayed and we do not get perfect output or some time it displays directly errors to the visitors. So in this case of development turn on error reporting. There are different levels of strictness in the reporting that you can use, but if you use E_ALL it will show you the most critical errors and warnings. Once you’ve completed development and ready for production, you’ll want to turn off error reporting otherwise your visitors will see strange errors which they mayn’t understand.

3) Don’t Put phpinfo() in your Webroot

While development on server many developers use code to get server php details and forget to remove from server. This is a really insecure practice, as hackers can get server detail. Make sure phpinfo() is in a secure spot, and as an extra measure, delete it once you’re done.

4) Use Meaningful, Consistent Naming Conventions

In development use meaningful and consistent naming conventions for class, function, variables, objects etc. It’s easy for team members or future developers to understand.

5) Know the Difference between Single and Double Quotes

To display string, developer use single and double quotes. Best practice to use single quotes. Single quotes in strings as the parser doesn’t have to sift through the code to look for escaped characters and other things that double quotes allow. Always try to use single quotes whenever possible.

6) Don’t Copy Extra Variables

Some developer makes code more appealing by using extra or unnecessary variable. That will consume double memory and also cache. Avoid to use that.

For Example
Code with Extra Variables:
$description = strip_tags($_POST[‘description’]);
echo $description;
Code without Extra Variables:
echo strip_tags($_POST[‘description’]);

7) Store Passwords with Encryption

Many beginner developers store sensitive data like passwords into the database without encrypting. Use MD5 to encrypt passwords before you put them into the database.
For Example:
echo md5(‘myPassword’);

8) Always Use

Many developer use shortcuts like <? ?>, , <% %> insted of . to save few characters, other methods are depreciated and unofficial. Stick with the standard as it will be guaranteed to be supported in all future versions.

<? echo “Hello world”; ?>

<% echo “Hello world”; %>
Instead that use following:

<?php echo “Hello world”; ?>

9) Use Output Buffering

Output buffering is used to improve the performance and speed of your PHP script. Without output buffering, your script will show the HTML on the page in pieces. Adding output buffering allows the PHP to store the HTML as a variable and send it to the browser in one chunk. To use output buffering in script add ob_start() at the top of the file.

If ob_start() used for buffering also append the “ob_end_flush(); at bottom of PHP script. Developers can also used ob_start(‘ob_gzhandler’) to compress the HTML.

10) Use a ROOT path instead relative paths

Relative paths first searches for directories specified in the include paths of php, then looks from the current directory.

So many directories are checked. When a script is being run from cron, it may not have its parent directory as the working directory.

Relative path:

Absolute path:
define(‘ROOT’ , ‘/var/www/project/’);
require_once(ROOT . ‘../../lib/some_class.php’);

Absolute path and will always stay constant. But we can improve that using portable magic constants like __FILE__.

define(‘ROOT’ , pathinfo(__FILE__, PATHINFO_DIRNAME));
require_once(ROOT . ‘../../lib/some_class.php’);

Hope the experience and thoughts I have shared helps you in your journey towards mastering PHP. These are some basic 101 for PHP and it’s not meant for expert/advanced PHP developers. cmsMinds is a PHP development company and we can assist you with any PHP related issue. We’ve team of PHP developers who can assist you on bug fix, upgrade or architecting a new application.

Posted in PHP

Quick Inquiry