Naming conventions used by developers in PHP

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.

Naming variables, functions, iterators, and exceptions

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.

Example: Naming variables

<?PHP
/*
Good variable, function, iterator and exception
instance names are written in
lowerCamelCase, 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]);
?>

Naming classes and interfaces

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.

Example: Naming class and interface

<?PHP
//Good class names are in UpperCamelCase and are nouns
class ElectricEngine
{
// Class details
}
/*
Good Interface names are written in UpperCamelCase
and end with the Interface suffix
*/
interface CalculatorInterface{
// Interface details
}
?>
PHP class and interface naming convention

Naming constants

Constants' names are always written in the UPPER case and multiple words in one name are usually separated by an underscore.

Example: Naming constants

<?PHP
/*
Good constants names are written in UPPER case
and multiple words within a single name
are separated with an underscore
*/
define("ACCOUNT_DEFICIT", 50);
define("MONTHLY_PROFIT", null);
define("EMPLOYEES_HIRED", true);
echo ACCOUNT_DEFICIT;
?>

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved