This article explains a few of the PHP coding guidlies that is recommended to to follow, including a few tips and tricks. Coding guidelines differ between coder and is no correct or incorrect way.
|Definining variables and functions
When naming variables, keep them all lower case. If a variable name has multiple words, separate them with underscores – like so:
If you use a piece of code which is used many times throughout your code, it is better to place it in a function. This will reduce the amount of code in your scripts and will most likely increase performance. Remember if you want to pass variables to a function defined outside of the function remember to declare them global. IE –
As wtih variable naming, function names should be declared using the same conventions as I explained above. IE –
When assigning a variable to a number, there is no need for quotes so simply use
When incrementing a number, increment it like so –
In this case there is no space as we are not using the equals sign to assign it a new value.
Even thought the variable is parsed inside double quotes, double quotes are parsed slowly, so simply use
Bearing the speed of double quotes, if you wanting to echo a static string, use single quotes. IE –
Specific technique:If you are using an if() statement, which is consists of a simple if…else consider using a ternary operator. Take the following code:
For a very small if() statement, it takes up a lot of room. This can easily be changed to
This takes up a lot less room and is more tidier (in a simplistic case).
In the above code, double quote marks (“) are parsed slowly, so where possible try not to use them (with exception of “