Good Coding Style in PHP + Other languages #15

Style 1.1: Use proper indenting

while ($x < $z) {    if ($a == 1)    {       echo 'A was equal to 1';  }   else    {       if ($b == 2)        {           //do something      }       else        {           //do something else     }   }}

1.2
while ($x < $z) { if ($a == 1) { echo 'A was equal to 1'; } else { if ($b == 2) { //do something } else { //do something else } }}

Style 2.1: Properly indent conditional statements. Always use braces, it will make later additions of more statements easier.

while ($x < $z) {   if ($a == 1)    {       echo 'A was equal to 1';  }   else    {       if ($b == 2)        {           //do something      }       else        {           //do something else     }   }}

Style 2.2

while ($x < $z) {   if ($a == 1) {      echo 'A was equal to 1';  } else {        if ($b == 2) {          //do something      } else {            //do something else     }   }}

3.1 Function Calls
No space between function names and parenthesis.

   $var = myFunction($x, $y);

3.2 Function declarations

Use braces properly, give meaningful names to the parameters, always return values from functions. Avoid printing/echoing inside functions.

function myFunction($province, $city = ''){   //indent all code inside here   return $result;}   

4. Use comments before a function. Also, use comments before a block [especially if it uses some difficult to understand logic]use PHPDoc style comments that may work like Javadoc to create documentation from your source files

/** *  short description of function * *   Optional more detailed description. * * @param $paramName - type - brief purpose *  @param ... *    ... *   @return type and description */

5. Use include_once or require_once instead of include or require to include a file that contains common variables, functions, classes.
6. Php tags: always use

  

instead of

  

7. to enclose strings use single quote ‘ ‘ rather than double quotes ” “. Try to use . to concate string variables. You can use double quote and put variables inside.

 $associative_array['name']; $var='My String';   $var2='Very... long... string... ' . $var . ' ...more string... ';  $sql="INSERT INTO mytable (field) VALUES ('$var')";

8. Follow some conventions for variable and function names

  • Class name start with uppercase letter. Each word should start with uppercase letter
  • Variable and function name may start with lower case letters. Then each word will start with a capital letter
  • give meaningful names to variables and functions
  • Do not make them too lengthy. I prefer less than 12-15 character names
  • Do not abbreviate words in variable or function names. Use $url or $articleUrl as variable names, not $URL or $articleURL as

From: http://sitestree.com/?p=4741
Categories:15
Tags:
Post Data:2006-08-07 13:22:40

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>