Coding conventions are a crucial aspect of maintaining excellent code quality. In this Answer, you will learn the conventions of naming a variable, class, function, and constant and importing a file/library/package in PHP.
Most PHP developers write variables, functions, iterators, and exception instance names in lowerCamelCase. In addition, their name should be self-explanatory and concise.
Note: A concise name is neither too lengthy nor too short; nonetheless, if a longer name better represents the variable, we choose the longer one.
In lowerCamelCase, the first letter of the first word in a variable name is written in lower case. If the variable name consists of multiple words, all words other than the first word begin with a capital letter.
<?PHP/*Good variable, function, iterator and exceptioninstance names are written inlowerCamelCase, are concise and descriptive*/function sumElementsOfIterable(iterable $myIterable){$sum = 0;try{foreach($myIterable as $element){$sum += $element;}}catch(Exception $objException){print($objException);}return $sum;}echo sumElementsOfIterable([2, 4, 6, 8]);?>
Class and interface names are always written in UpperCamelCase and are nouns; they should never be adjectives but the interface should end with the "Interface" suffix.
Note: In UpperCamelCase, each word in the variable name begins with a capital letter.
<?PHP//Good class names are in UpperCamelCase and are nounsclass ElectricEngine{// Class details}/*Good Interface names are written in UpperCamelCaseand end with the Interface suffix*/interface CalculatorInterface{// Interface details}?>
Constants' names are always written in the UPPER case and multiple words in one name are usually separated by an underscore.
<?PHP/*Good constants names are written in UPPER caseand multiple words within a single nameare separated with an underscore*/define("ACCOUNT_DEFICIT", 50);define("MONTHLY_PROFIT", null);define("EMPLOYEES_HIRED", true);echo ACCOUNT_DEFICIT;?>
Free Resources