| |||||||||||||||||||||||||
|
![]() Beautiful code on PHP I shall result fundamentals which need to be observed by development of a code, that in a week it would be possible to look a code and to define{determine}, what function what to do{make}. One more preimuhhstvom a spelling of a readable code, it is simplicity of search and elimination of mistakes. At once I shall say, that which I mark examples as "incorrect", mean not that they do not work, and that they nekkorektny from the point of view to development of a readable code. 1. Put variables outside the brackets Always at a conclusion to the screen of the text, variables in line it is necessary to put outside the brackets. It not only is convenient for viewing, but also is effective, as at it the conclusion to the screen is carried out more quickly. Example incorrect: echo " Value is $val " Example correct: echo " Value is ". $val 2. Necessarily use comments Each function should be whenever possible described by the small comment. Each fragment if you consider that he complex{difficult} enough for understanding, should be described by the small comment. And in general, the comment everything should be described, that you consider necessary. You should define{determine} a choice of a kind of the comment for yourselves. It can be one text line, or the block of lines in which purpose{appointment} of function is described, the information on its{her} author, etc. 3. Use the reduced kind of function echo. For example, recording of a kind <? php echo $val ?> It is possible to replace easy on <? = $val?> 4. Whenever possible bear{take out} big blocks HTML for limits of designs php. do not abuse function php. Example incorrect: <? php for ($i = 1; $i <10; $i ++) { echo " Number is ". $i; echo "<br>"; echo " Number before is ". ($i - 1); echo "<br>"; } ?> Example correct: <? php for ($i = 1; $i <10; $i ++) { ?> Number is <? = $i?> <br> Number before is <? = ($i-1)?> <br> <? php } ?> Pay attention, that here opens two designs php, and between them it is inserted HTML the text. Probably, by the given example also it is not visible a clear advantage of carrying out of the text for limits php to a design, but actually when you should deal with the tables, the similar rule can be useful very much. 5. The code should be leveled concerning blocks. Example incorrect: <? php for ($i = 1; $i <10; $i ++) { echo $i; $j ++; } ?> Remember, php it to you not paskal` with his{its} blocks begin... end. Here the block should open in the same line where he began, and the beginning of the block is closed leveled, concerning: <? php for ($i = 1; $i <10; $i ++) { echo $i; $j ++; } ?> 6. Simplify complex{difficult} designs. Break them on simple. Example incorrect: $res = mysql_result (mysql_query (" SELECT Num FROM db "), 0, 0) Example correct: $query = mysql_query (" SELECT Num FROM db "); $res = mysql_result ($query, 0, 0); 7. Use more blanks and empty lines. Actually it is important enough element a spelling of a readable code. I saw, how the some people do not leave empty lines and use as small as possible blanks, considering that they take a superfluous place. It radically is not correct, as superfluous bajty will allow other person to save time for analysis of an another's code. Example incorrect: <? php for ($i=1; $j <10; $i ++) { $sum = $ sum + $ i; $j ++; } ?> Example correct: <? php for ($i = 1; $j <10; $i ++) { $sum = $sum + $i; $j ++; } ?> 8. Use the reduced kinds of mathematical and line operations. Remember, that +1 always it is possible to replace on ++, and +n on + =n. Examples of replacements: $i = $i + 1 it is equivalent $i ++ $i = $i - 1 it is equivalent $i-- $i = $i + $n it is equivalent $i + = $ n $i = $i. "hello" it is equivalent $i. = "hello" |
||||||||||||||||||||||||