Good Coding Style in PHP + Other languages #Root

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

  <?php

  ?>

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=3535
Categories:Root
Tags:
Post Data:2016-07-08 16:04:58

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada