Here is a tutorial on how to write a nice and clean PHP code.
Let us start with variables.
I use prefix for variables with camel notation. This makes it easy to see and check the right variable for its type.
-
$strName = ‘ali’ ;
-
$intCounter = 1;
-
$arrData =
array(‘a’=>
‘ali’ ,
‘i’=>
‘is’ ,
‘p’=>
‘perfect’);
-
$blnTrigger = true;
-
$objMailSender = new mailSenderClass();
I use single quote for my data.
-
$strMyNameIs = ‘Alireza’;
And I use double quotes for SQL or strings that I need to have a single quote inside.
-
$strSQL = "SELECT * FROM person WHERE name LIKE ‘ali’ ";
I never use variable inside a double quote string. I break up the quote.
-
$strCode = "some $bad code ";// WRONG AND UGLY
-
$strNiceCode = ‘Very ‘ . $strData . ‘ is pasted here’ ;// NICE CODE
Never ever use short variables like $a or $temp or other lazy short names.
Use variable names that are understandable and describe what it contains.
And using a camelNotation makes it easy to read.
Never start with underscore or other kind of separator for names.
-
$strReturn = ‘Nice code’;
Only use underscore for constants where you can not use tha camel notation.
-
define(‘FOO_BAR’,
‘It works!’);
And do not start with underscore. There are some other system variables and functions that starts with underscore like _get() or $_SESSION