high-quality coding standards in PHP

php high-quality applications code:

  • is easy to read, maintain, and extend.
  • is secure - that is, protected from attacks.
  • handles error conditions and problems in a robust and graceful way.
  • is well tested.

how write the php high-quality code

always write modular code:

php contains a number of features that help you write modular code look below

  • classes : take the concept of code encapsulation, polymorphism, inheritance, abstraction.
  • functions : easily handle the concept of reuse code by calling the function from elsewhere in your application code.
  • namespaces : usefull for large applications and libraries. they help to avoid clashes where the same class name, function name, or constant name is accidentally used in different libraries, or a different parts of an application.

for good quality code writing documentation to improve readability for other programmers who work on you code.

note : you must remove all comment before host your web apps.

validating input, as well as encoding output, to mitigate the risk of security holes in your code.

cleanly separating your application's business logic from its user interface.

using automated testing frameworks to improve the quality of your code.

note : you must follow the pear coding standards for consistency

  <?php  
     
        function   foo()
        {     
        
            if ($doo) {
              
                do_it();
                
                again();
            
            }else {
            
                    erro();
                    
            }
            
            finalize();
        }
        
  ?>

avoid deep nesting : too many levels of nesting can make code harder to read.

<?php 
   
function do_stuff() {  

    if (is_writ($folder)) {  
  
        if ($fp = fopen($file_path,'r+')) {  
  
            if ($stuff = get_stuff()) {  
  
                if (fwrite($fp,$stuff)) {  
  
                    // ...                      // avoid deep nesting 
  
                } else {  
                           return false;  
                    }  
            } else {  
                        return false;  
               }  
        } else {  
                   return false;  
           }  
    } else {  
               return false;  
         }  
}  

?>

PHP advantages of high-quality code

code reuse : you only have to write the code once, and then you can use it many times throughout your various php applications.

readability : by splitting your code into chunks you make your application code easier to read and understand.

efficiency : rather then having your entire application in one huge file - which the php engine then has to load and interpret for each page view - you can load only the code you need to carry out each request.

ease of maintenance.