PHP Programming Best Practices and Coding Styles

Best Practices and Coding Styles to learn to become a better PHP Programmer

Dipu Raj
TechLaboratory
10 min readMay 11, 2017

--

We are all good programmers and we know how to write good programs, but there are some hidden things we might have missed in the programming world. Knowing it could help us to improve our code in speed and readability. I would like to point out few tips and practices in PHP that I found useful.

As you know, PHP (Hypertext Preprocessor) is a widely-used, free, efficient, and versatile programming language that allows developers to create dynamic content, basically used for developing web based applications. Let’s dig a little bit deep into it to see how we can improve the way we write it. This article is meant to help PHP beginners and above, If you haven’t learned the fundamentals of PHP programming yet, take your time to learn and come back.

1. Avoid short PHP tags

Short tags (<? ?>, <?=?>) are easy to use, but it is not always reliable, because short tags can be disabled using the short_open_tag ini setting and not all servers are enabled it by default. So the code written in short tags is not portable across servers unless you have access to the ini settings. Another reason to avoid short tag is it may confuse with <?xml ?> notation. The recommended use of the PHP tag is <?php

Plus, if you are sure that your PHP version is 5.4.0 and above, you are allowed to use <?= ?> tag too. It is a shortcut syntax of echo and it is pretty handy to print a variable in the inline code. Since PHP 5.4.0, this tag is made free from the short_open_tag ini configuration setting.

Now, push yourself hard to avoid using this tag

Well, in the history there are other tags are also used that are abandoned in the latest versions(PHP7+). Just listing them to add it your DONT’S list.

2. Avoid closing tag in PHP only files

If your file contains only PHP code, the closing tag (?>) is optional. It is recommended to avoid closing tag because it prevents unwanted white-space or new lines being added at the end of files. That is handy to use output buffering and we will be able to add headers to the response later.

3. Code Formatting and Layout

It doesn’t matter in-terms of code execution if we didn’t format the code well. But it does matter, when another person came to read it or another team try to update it or even worst when we come back to review our own code after a long time.

Well formatted code is easier to understand and it look consistent, clean and readable. It didn’t take too much effort, you can use the automatic code formatting feature of your IDE — most of the good IDE have that option, or you take a little time to format it yourself. Read more about PSR-2: Coding Style Guide which is a coding style standard for code formatting.

See an example of well indented code:

And this is NOT, don’t do like this

4. Write Comment Code

Comments are the part of code that helps to describe what is a piece of code or a function or a class is doing. Writing comments is a good practice to let others know what you’re doing or to remind yourself what you did. You don’t want to comment every line in your code but do when you think it is necessary to understand your code later.
These are the examples of commenting styles on PHP

5. Choosing Names

Naming your variables, functions, or classes are important. While naming, you should consider these factors

  1. The name you chose should be meaningful
  2. The name should be in a language everybody will understand (preferably in English)
  3. Follow consistent naming convention

There are many naming standards with slight variations, like PSR-1: Basic Coding Standard or Zend Framework Naming Conventions. You can choose any style based on your convenience, just make sure it is consistent throughout the project.
Mostly I use the following naming convention style

  • ClassName
  • function_name
  • $variable_name
  • CONSTANT_NAME

Here are the list of best read about naming conventions

6. Understanding ‘echo’

The PHP echo statement is a language construct (not exactly a function) used to output data to the screen. echo and print both do the same thing, but echo is a little faster. echo can take multiple parameters, and it is faster than concatenating strings.

For example...

is faster and neater than…

Also you can use expressions

which is better than…

But echo has no return value, so it cannot be used in expressions. So the following code is invalid!

instead you can use like this

7. Using variables

It is not necessary to initialize variables in PHP however it is a good practice. Uninitialized variables will have a default value, relying on the default value may end up in unexpected results of your program. So it is recommended to initialize your variables before using them, and that will make your code much cleaner.

Also avoid using unwanted variable declarations. Variables cost memory, so removing unwanted variable declaration can reduce memory load. For example…

In the above code we can definitely avoid the variable $my_name, like...

8. Use of Ternary Operators

A simple if/else statement can be changed to make use of ternary operators. It makes your code shorter, readable and saves few lines too. The usage is simple…

An example usage of ternary operators

Is identical to this if/else statement

9. Use of Comparison Operators

PHP have strict and loose comparison operators, the difference is strict comparison validates value and type while loose comparison only validates value. You can use any of the comparison, but to avoid unexpected program behavior, you should be very clear what you are doing.

It is not a big deal to choose, if you want to compare the value and the type go for strict comparison otherwise use loose ones.

For example, you are validating a POST value and you don’t know in which type it will come always and you only care about the value. Then go for loose comparison

But, for example, you are validating a return value from a function and you really care the type of the return value. In the below example if you don’t use strict comparison the condition will pass if the function return ‘1’, which will be a serious flaw.

Another very common example for this is while using strpos to compare strings. Here in the below example, if you use != for comparison, it print output as 'string not found', which is wrong.

The reason is strpos return 0, which is the position of the string and the loose comparison consider 0 as false, hence the unexpected result. So the correct check is as follows...

10. About Strings

Let us take a look at common ways of specifying strings and its use in PHP.

Single quoted

Enclosed in single quotes (') is a simple way to specify a string. It doesn't parse any special characters, but it allow escape character with a backslash (\), generally for escaping a single quotes and backslash itself in the string. So this way is ideal if you want to output a special character literally, such as \r or \n, and this method is good if you have a simple string to specify.

Double quoted

Enclosed in double-quotes (") is a another way to specify a string, and PHP will interpret more escape sequences for special characters. So \n in the string really output a newline instead of literally printing it. And variables specified in the string will be parsed and replaced with its value, if available. This method is ideal if you have to specify variables or special characters in your string.

There are many talk about performance of both string specifications. Some point out that single quoted is faster because it doesn’t include much parsing. But in my opinion both give same performance on normal use, a few parsing doesn’t make noticeable performance lack. So use any method you want as the situation demands, unless you want to do micro-optimization.

Heredoc and Nowdoc are the other complex way of specifying strings, they are not used very common. You can read more about them on PHP strings manual.

11. About Arrays

Array in PHP is optimized for several different uses, it can be used as an array, list, hash table, dictionary, collection, stack, queue, etc. Let’s look into some cool features of PHP Array probably don’t use commonly.

Since PHP 5.4, we get the short array syntax, which allow to use [] instead of array(). Checkout the short and regular syntax example...

Square brackets and curly braces can be used for accessing array elements. Both line of code have same effect in the below example…

Since PHP 5.4, we can directly access the result of a function or method as array (array dereferencing).

The foreach loop provides an easy way to traverse an array

Since PHP 5, we can manipulate array value directly by passing them by reference.

Use quotes if you have array key as a string. It still works if you don’t, but that is not recommended. The reason is that PHP assume it as a constant if you don’t enclose in quotes, and PHP automatically converts it into a string if there is no defined constant in that name, that’s why it still works, but we are giving the compiler an extra load. Also if you already have a constant with the same name, you get weird results.

Conclusion

I hope that the tips and practices discussed here was helpful to you. However, programming is vast, don’t stop learning, try new things and be up to date. If you think there is an incorrect information or missing clarification, please let know with your comment.

Happy Coding!

--

--

Dipu Raj
TechLaboratory

Full stack engineer. Believes the code should be as beautiful as the design. http://dipu.me