Standard Code Rule(PHP)

Here following standard rule use while code .

1. Do not put duplicate html code when conditions
example
if ( $page == ‘index.php’ ):
<li><a href=”index.php” class=”menu selected”>Home</a></li>
else:
<li><a href=”index.php” class=”menu”>Home</a></li>
endif;

2. Ever try to use ternary operator when single line or assignment conditionally
example
$variable = $user == ‘administrator’ ? ‘Welcome Admin’ : ‘Welcome ‘.$user;
echo ( $act == ‘add’ ) ? ‘New Record’ : ‘Modify Record’;

3. Write code and review your code 2 times before apply
Put yourself being computer to execute the code will help to improve your code

4. Option to Review code is ask your colleague to check your code same way as above

5. Put one line description that what is the code is doing / performing

6. Never use $_POST, $_GET, $_REQUEST directly.
example
$myvar = ”;
if ( !isset($_REQUEST[‘myvar’]) ) $myvar = $_REQUEST[‘myvar’];

7. Unset all objects you used in PHP file at the end of the script. unset($obj)

8. No need to put, !empty($object) where you are using foreach($object as $k => $v)

9. Indentation require to understand the logic you wrote by other developer or yourself

10. What is the difference between require and require_once?

11. try to use single quote with echo if not used any varible within string
example
echo ‘hello ‘.$ajay;
echo “hello $ajay”;

12. form inputs
always do prictice to selfclose for input type like hidden, text, button, submit, image etc..

13. try to use css based animation / menu instead of javascript because css3 is good to do the same

14. use: instead of { for if, foreach, while and switch when it’s within view template

15. Non Credential redirection logic should always at the top not anywhere else

16. Do not use any standard keyword for variable

Leave a comment