Best Practises, Coding

PHP Best Practices for Beginners

To become a professional developer in any language we need to learn best practices. Best Practices are implemented in all disciplines. Best Practices are basically a collection of known best approaches to a problem so we do not repeat the same mistakes over and over. Few of the best-practices outlined in this article are time-tested and industry-standard approaches to common problems.

  1. Indenting

    In terms of readability indenting your code is a big plus and help anyone to read your code, it makes the code easy to read and maintainable in the future and changes are easier to make.

    <?php
    while ($a < $b) {
    	if ($a == 1) {
    		// display some stuff
    	} else {
    		if ($b == 2) {
    			//display some stuff
    		} else {
    			//display some stuff
    		}
    	}
    }
    ?>
    
  2. Avoid Deep Nesting

    Too many levels of nesting can make code harder to read and follow.

    <?php
    if( condition1 ) {
      if( condition2 ) {
        // some stuff
      }
      else {
        // some stuff
      }
    }
    ?>
    

    Using some binary operator will simplyfy this and avoid nested code

    <?php
    if( condition1 && condition2 ) {
      // some stuff 
    }
    else {
      // some stuff
    }
    ?>
    
  3. Explain your code by providing comments

    Explaining your code by comments will help you and others developer that will handle your code understand what your code write does, purpose etc

    <?php
    // Connection function
    function connect() {
    	//some code
    }
    ?>
    

    also don’t over do it or put too many obvious comments

    <?php
        // check visitor ip 
        $visitor_ip = check_visitor_ip($_SERVER['HTTP_CLIENT_IP']);  
          
        // if visitor is true 
        if ($visitor_ip) {  
          
            // display the status  
            echo status();  
        }  
    ?>
    
  4. Always keep PHP separate from HTML

    Some developer find ease to output HTML code from PHP but you will notice on example below it’s readability which is not good, and one thing to consider are page load, Using PHP script to output HTML will be served at least 2-10 times slower than a static HTML page.

    Bad coding example

    <?php
    echo '<form method="post" action="">';
    echo '<p>Full Name<br />';
    echo '<input type="text" name="'.$_POST['fname'].'" size="30" /></p>'; 
    echo '<p>Last Name<br />';
    echo '<input type="text" name="'.$_POST['lname'].'" size="30" /></p>';
    echo '<p>Email<br />';
    echo '<input type="text" name="'.$_POST['email'].'" size="30" /></p>';
    echo '</form>';
    ?>
    

    Good coding example

    <form method="post" action=""> 
    <p>Full Name<br />
    <input type="text" name="<?php echo $_POST['fname'];?>" size="30" /></p>
    <p>Last Name<br />
    <input type="text" name="<?php echo $_POST['lname'];?>" size="30" /></p>
    <p>Email<br />
    <input type="text" name="<?php echo $_POST['email'];?>" size="30" /></p>
    </form>
    

    As much as possible try to maintain fewer script.

  5. Avoid using short open tags <?= or <?

    Both of these short tags form are not good to use specially when you move your code to a server where it’s not supported. Specially when the final hosting server has turned off this short tags supported, If you are not aware on final server you were move your code then its better to avoid short tags and keep using

    <?php echo $s ?>

    than doing a lot of work at the end changing short tags, using this seems small amount of work but assure support in all hosting environment.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles and posts delivered to your feed reader.