Let’s create something better together.

If you prefer phones, we have one of those too: +1 919 694 8000










    • Project Info
      icon
    • Technology
      icon
    • Contact Details
      icon







      BackNext








      Back

      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.

      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.

      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.

      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.

      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.

      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.

      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’]);

      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’);

      Many developer use shortcuts like <? ?>, , <% %> instead 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.

      Shortcuts:
      <? echo “Hello world”; ?>

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

      <?php echo “Hello world”; ?>

      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.

      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:

      require(‘/var/www/project/file.php’);

      Absolute path:
      define(‘ROOT’ , ‘/var/www/project/’);
      require(‘/var/www/project/file.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(‘/var/www/project/file.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.

      Author's Bio

      Vishal Sharma
      Vishal Sharma

      With a decade of WordPress development and 5 years in project and team management, Vishal Sharma stands as a pillar of expertise and innovation. His active participation and speaking engagements within the WordPress community underscore a deep commitment to pushing boundaries and fostering growth. Trust Vishal to deliver unparalleled digital experiences rooted in a rich blend of creativity, technical skill, and leadership.

      Share This Article:

      Recent Blogs
      VIEW ALL BLOGS