Writing Solid PHP Code 2 - Naming and Style
Having a consistent style makes your PHP code more readable and maintainable. Even in projects where you are the only programmer, once the project progresses to a certain size it can become difficult to maintain.
Variable Names
Variable names should be very descriptive. They should give anyone that is not familiar with your script to understand what its for without much digging.
Variable names should all follow the same conventions. It doesn't matter if you use camel casing (YourVariableName), lower camel case (yourFunctionName), or underscores (your_variable_name), as long as all the variable names have the same style.
It is a good idea to include the scope of the variable in the name. For example, if you have a function or set of functions dedicated to parsing a string you may want to start the variable name with "parse."
Function Names
Function names should also be very descriptive and follow a defined convention. Function names should be prefixed with the unit they are in. For example, if you have a file named databaseFunctions.php you might prefix all the functions within that file with dbFuncs_ or something similar.
Function names should describe the action taking place. For example, instead of "StringParser()" use "ParseString()."
Indentation
Indentation is very important as it makes reading the code easier. Proper indentation can help you keep track of loops, conditional statements, and blocks of code. Each level should be indented a step further. The following snippet is a good example of proper indentation:
<?php $loopVariable = 35; for ($loopCounter = 1; $loopCounter < 25; $loopCounter++) { $loopVariable = $loopVariable*$loopCounter; if (($loopVariable % 2) == 1) { echo "Even<br >n"; } } ?>
Using good techniques such as proper naming and indentation may seem unneccesary for small projects. However, for larger projects it is essential for writing maintainable code.











