With so many different coding styles in use by developers nowadays, I would like to suggest some ideal naming conventions that I have found useful and practical myself.
In HTML and CSS, Camel Case notation eg. myTestclass can be far less than ideal as it might be difficult to spot errors, consider for example the difference between myTestclass and myTestClass which is not so easy to identify but will result in your styles not being applied, and possibly hours of frustration. I prefer using lower case for all elements, with dashes to separate multiple words, e.g. my-test-class. I think it’s far more readable and faster to type than having to introduce capitalised letters.
For simplicity and consistency, I use lowercase characters for all CSS code including elements, classes, and IDs. As for whitespace in CSS I use the following format:
body {
font-size: 20px;
line-height: 150%;
}
If needed CSS code can be minified to increase performance, however while developing I like to use the style above as I find it the easiest for me to read.
Form elements should be named for what they are, not how they work. If you decided to change a radio button to a check box in the future, you will see why having originally named it something like radio-answer turns out not to be such a great idea after all.
PHP Conventions I Use
Take a look at the Zend and PEAR standards and naming conventions:
PHP Naming Guidlines - Zend Framework 1.9+
Classes – PascalCase (UpperCamelCase)
Special Considerations: Class names must match the name of the directory on the file system that contains the class file. Names are to be AlphaNumeric only with the exception of an ‘_’ which is only to be used to show separation in the path. If you have a class file named Car.php in the path ‘/Models/Car.php’ then the class name would have to be Models_Car.
Interfaces – PascalCase (UpperCamelCase)
Special Considerations: See Classes.
MySQL Tables – plural with underscores if necessary
Functions and Methods – camelCase
Special Considerations: AlphaNumeric only with exception of ‘_’ as described here. Names must describe behavior of function or method. Methods that are declared with private or protected visibility modifier must start with an _.
Variables – camelCase
Special Considerations: Names should describe the data in the variable. Variables declared with private or protected visibility modifiers must start with an _.
Constants – ALL_CAPS
Special Considerations: AlphaNumeric and ‘_’ are allowed however ‘_’ must be used to separate words in constants.
Code example:
define("CURRENT_YEAR", date('Y'));
interface Transport
{
public function getDescription();
}
class Models_Car implements Transport
{
private $_make = 'Ford';
private $_model = 'Mustang';
private $_year = 1967;
private function _findAge()
{
return CURRENT_YEAR - $this->_year;
}
public function getDescription()
{
$desc = $this->_year . ", " .
$this->_make . " " .
$this->_model . " is " .
$this->_findAge() . " years old.";
return $desc;
}
}
$car = new Models_Car();
echo $car->getDescription();
Apart from the above conventions by Zend, it is also acceptable to use the following naming structure for functions:
function my_function($args) {
}
Notes:
Pascal Case -> ThisIsPascalCase (Also Called Upper Camel Case)
Camel Case -> thisIsCamelCase (Also Called Lower Camel Case)
Related posts:

Web professional in Malta, Europe. Focusing on building visually stunning websites that are easy to maintain, usually using WordPress as the CMS. Web developing since 1995, loving WordPress for more than 5 years.
Leave a Comment
Let us know your thoughts on this post but remember to place nicely folks!