Showing posts with label PHP Best rules to create website. Show all posts
Showing posts with label PHP Best rules to create website. Show all posts

Monday, 13 July 2015

PHP: PHP BASIC PROGRAMMING STANDARDS

Everyone should follow some coding standards. It is required because there may be many developers working on different modules of one project; if they follow their own standards the code may become very un-manageable. It is very difficult to maintain the code in future.
Reasons to follow standards:
  1. Your co-programmers should understand your code. A coding standard acts like a blueprint for the team.
  2. Simple and clear code will reduce your common mistakes.
  3. If you revise your code after some days, it is easy to understand that code.
  4. For best quality project you should follow the coding standards.
Here are some coding tips to follow for better result:
  1. Text indenting and line length: It is better to maintain text indenting and particular line length while coding. For text indenting use four spaces instead of tab, because different computers may take different settings for tab. It is better to maintain the line length approximately 75-85 characters long for better reading.
  2. Control structures: Control statements should have one space between keyword and opening parenthesis, to make a difference between function calls and control statements. It is recommended to use curly braces even though it is optional.
  3. Function calls: Functions should be called with no space between name and opening parenthesis, and the first parameter, spaces between commas and each parameter, and no space between last parameter and closing parenthesis.
  4. Function definitions: Function names should follow the allman style.
  5. Comments: Use C style comments (/* */) and standard C++ (//) style comments.
  6. Variable names:
      • Use lower case letters
      • Use ‘_’ as word separator.
      • Global variables should be prepended with ‘g’
      • Static variables should be prepended with ‘s’
      • Global constants should be all caps with ‘_’ separator
7. One statement per one line
Here is the set of tips to follow to become a good PHP developer.
  1. Don’t use PHP short tags:
Short_open_tags is an ini setting. By default it is enabled in PHP 5.3, but every server not run PHP 5.3. It is more important to use PHP full tags in compatibility and portability.
       2. Stop using MySQL Library:
It is better to stop using MySQL library functions as they have not updated in some time.
Use MySQLi or PDO
       3. Use array functions and string functions:
While working with arrays and strings it is more important to use functions instead of writing your own code.

PHP: MOST IMPORTANT CODING TIPS

PHP Code Tags :
1. Start PHP file with a <?php tag and end it with the closing ?>.
2. Always use <?php  ?> to delimit PHP code, not the <? ?> shorthand.
3. This is required for PHP compliance and is also the most portable way to include PHP code on different operating systems and setups.
Line Length:
1. Use an indent of 4 spaces and don’t use any tab because different computers use different settings for tab. It is recommended to keep lines at approximately 75-85 characters long for better code readability.
Control Structures:
1. These include if, for, while, switch, etc. Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls. You are strongly encouraged to always use curly braces even in situations where they are technically optional.
Example:
if ((condition1) || (condition2)) { action1; } elseif ((condition3) && (condition4)) { action2; } else { default action; } Example: switch (condition) { case 1: action1; break; case 2: action2; break; default: defaultaction; break; }
Comments:
1. Php contains both single line and multiple line comments, So if you start any class or function definitely mention comments. Comments are most important in every programming language.
Example :
/** * Class  : <class name> * Site name :<site name> * Developing for <page name> * Company :  <company name> * @author  <author mail/uname> * @copyright 2012-2013 The PHP Group * @license http://www.php.net/license/3_01.txt PHP License 3.01 * @link  <url> * PHP version 5 */ Class <classname> {  //after class name give one space before left curly bracket }
Example :
/** * <function functionality> * * @param  <mention params> * @return  <mention return type> * Function  <function name> { }
2. Any where you can mention “=” give one space before equal and after equal
Example:
<?php $marks = 100; ?>
1. Function names and variable names must follow unique style
i.  camelCase
ii. under_scores

PHP: TIPS FOR OPTIMIZING PHP CODE

1. If a method can be static, declare it static, speed can be improved.

2. Echo is more faster than print.

3. Use Echo’s multiple parameters instead of string concatenation.

4. Unset your variables to free memory, especially large arrays.

5. Avoid magic like __get, __set, __autoload.

6. Require_once() is expensive.

7. Use full paths in includes and requires, less time spent on resolving the OS paths.

8. str_replace is faster than preg_replace, but strtr is faster than str_replace

9. Error suppression with @ is very slow.

10. Close your database connections when you’re done with them

11. Error messages are expensive

12. Incrementing a global variable is 2 times slower than a local var.

13. Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable.

14. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one.

15. Use any frame works (For example cake php,yii frame work)

16. Use Ternary Operators in your coding

17. Methods in derived classes run faster than ones defined in the base class.

18. Use if else conditions are more faster than the select statements.

19.Avoid looping in your coding.When you use array go for the foreach loop rather than for loop.

20. Don’t use short tags in your coding.

21. ++$i is more faster than $ i++ ,use pre-increment when it is possible.

22. Avoid the PHP mail() function header injection issue.

23. $row[’id’] is 7 times faster than $row[id], because if you don’t supply quotes it has to guess which index you meant, assuming you didn’t mean a constant.

24. Separate code, content and presentation: keep your PHP code separate from your HTML.

25. Avoid using plain text when storing and evaluating passwords to avoid exposure, instead use a hash, such as an md5, hash.

26. Not everything has to be OOP, often it is just overhead, each method and object call consumes a lot of memory.

Wednesday, 27 May 2015

PHP: Things to Remember While Developing a Website

These days website is an essential marketing platform for your business and key part of your marketing mix. If you are planning to have one for your own business then you can hire any web developer or you can do it on your own.

Developing a website is not the hard part, hard part is, it must serve the purpose for which you have created it. If you are able to answer the following questions then you get the winning website. Otherwise follow the blog, it will take you through the steps you may have missed during your website development.

Is your website serving the purpose you have created it for?

Is sufficient traffic coming to your website?

Does your website has any errors?

How is it performing on web?

So here are few things which you always keep in mind when developing a website:

1. Code website keeping in mind SEO: It will help your site to boost in search ranking when you make it live. For more details about SEO practices.

2. Register your Website with Google Analytics and webmaster tools: Google analytics and webmaster tools will help you to keep track of your website. Using these tools you can see daily hits of your website, categories hits based on many other filters such as city, browser etc. These tools will also tell you the words for which your website is coming in search, search rank of website and how you can improve your website search rank. The best part of these tools is that they are free:) (upto limit).

3. Don’t forget to include customized 404 error page: This is often overlooked and underestimated. However, designed carefully, these pages can make a random visitor stay on your website, take a look around and eventually find the information he or she was looking for in the first place. Effective 404 error pages communicate why a particular page couldn’t be displayed and what users can do next. A search box and list of useful resources (possibly related to the missing page) could be helpful in this case. So go and designed one for your website.

4. Create robot.txt for your website : robot.txt is a text file, which must be present in your website folder. It prevents crawlers accessing all parts(URLS) of your website and you can tell crawler about your sitemap.xml(explained later).

5. Create and submit sitemap.xml to webmaster tool: Site maps can improve search engine optimization of a site by making sure that all the pages can be found. It also tells crawler about your sitemap.xml using robot.txt file.

6. Check Your website performance: Before making a website live, you must check its performance(load speed and other errors). For this you can use google pagespeed tool.

So these are some points which can help you achieve the purpose for which you are creating your website.

It may possible that I have missed some other important points, which you are welcome to mention in comments.