3. Coding Standards
Naming Conventions:
Variables: Use camelCase (e.g., $userName)
Functions: Use camelCase (e.g., getUserName())
Classes: Use PascalCase (e.g., UserManager)
Constants: Use UPPER_CASE (e.g., MAX_USER_COUNT)
Variables:
// Good: Use descriptive names for variables.
$userName = 'John Doe';
// Bad: Avoid using single-character or non-descriptive names.
$u = 'John Doe';
Functions:
// Good: Use camelCase for function names.
function getUserName() {
return 'John Doe';
}
// Bad: Avoid using lowercase or ambiguous names.
function getuser() {
return 'John Doe';
}
Classes:
// Good
class UserManager
{
// class content
}
// Bad
class user_manager {
// class content
}
Constants:
// Good: Use uppercase letters and underscores for constants.const
cost MAX_USER_COUNT = 100;
// Bad: Avoid using lowercase or camelCase.const
const maxUserCount = 100;
Formatting:
Indentation
Use 4 spaces per indentation level.
// Good
if ($condition) {
echo 'true';
}
// Bad: Avoid inconsistent indentation.
if ($condition) {
echo 'true';
}
Line Length:
Keep lines under 80 characters.
// Good
$longString = 'This is a long string that is split across multiple lines to keep each line under 80 characters.';
Blank Lines:
Use blank lines to separate logical sections of code.
// Good: Separate functions with blank lines.
function firstFunction()
{
// function body
}
function secondFunction()
{
// function body
}
// Bad: Avoid having no separation between functions.
function firstFunction()
{
// function body
}
function secondFunction()
{
// function body
}
Comments:
Use comments to explain non-obvious code.
// Good: Add comments to explain the purpose of the function.
// This function returns the user's name
function getUserName() {
return 'John Doe';
}
// Bad: Avoid inline comments that are not descriptive.
function getUserName() {
return 'John Doe'; // returns name
}