There are always several ways to do the same things in PHP. Some of them are so close that one even wonder which one to choose. In fact, if they are interchangeable, why even choose ? Here is a list of seven syntax you can choose in PHP, freely.
The choice relies first on technical ground, like speed, security, less buggy code. Then, it tends to be done at human level, like readability, team conventions and confusion removal. Finally, it boils down to simple preference, or unquestioned-ten-years habits. My usual moto is to make a choice, stick to it and don’t try to convince people to adopt it, unless they join the project. After all, this is a mere question of character building.
So, here are seven syntax you can choose in PHP, and that should be consistent. They are not coding conventions, such as putting the curly bracket { on the next or next line : those are keywords equivalence, or code layout.
Echo or print
One of the eternal rear-guard battle of PHP. Echo takes an unlimited number of arguments, while print only takes one, which leads to concatenating everything at print time. Besides, print returns 1, while echo returns void. And there is also <?=, in templates.
I remember using print for debug, and echo for actual output, leading to easier check for debug purposes.
Die or exit
Both of them stop the code. Actually, die uses the T_EXIT token, and the behavior is exactly the same. Dying with a string is used as final message, while an integer is returned as error code. They are totally identical, and should be replaced by exceptions or assert() when possible.
Array() or []
Of course, [] is vastly superior. Less characters to write, more compact expressions, modern PHP. Anyone trying to convince you to use array() is an old programmer, with die-hard habits. Just like me. And even, consider that PHP 7.1 has also hired the same syntax for list() : that will cause pain only to static analysers. On the other hand, lots of code still uses array(), and it is important for backward compatitibliy. Otherwise, you’re free to choose.
true, FALSE or Else ?
This convention is ternary, not binary : you may encounter the tenants of lowercase, the one of upper case and the mixed-feelings tenants of mixed case. The most popular option is possibly PSR-2’s convention.
Which globals ?
Global variables, besides being avoided by recent standards, may be accessed by global keyword, or via $GLOBALS variables. $GLOBALS are always available, and may be faster for single interaction. On the other hand, global is cleaner, and faster for multiple variables.
array_push() and $array[]
array_push() is a functioncall, while [] is an operator. The second is faster, and a lot more compact, so common wisdom is all for []. When using array_pop(), it makes sense to use its sister function array_push(), though that may be the only reason I ever met. Could we suggest a ][ pop operator ? Other langages have << and >> to push and pop.
Yoda comparison
Should literals be on the left side or the right side ? There are some impact in termes of readability : it helps avoiding the infamous assignation instead of comparison, but it also makes the code harder to read. In the end, PHP doesn’t make a difference.
Missing exchangeable PHP syntax
Do you know other PHP syntax that are totally exchangeable ? Leave us a mail or a tweet, and we’ll add them to the Exakat analysis tool.
They didn’t make it to this list
I thought of other alternative syntax, but less them on the side :
- array_merge() and + share some features for arrays, but may behave too differently. We didn’t consider them here, though the limit is close with echo/print, which also behave differently.
- Include/include_once don’t have the same behavior, but require/include could be one.
- func_get_args() and … : func_get_args() returns all the arguments, while … may collect only a part of them, so they are no exchangeable.
- Casting to string with a .”, or to integer with + 0 or * 1 ? Those are probably mis-usage of the langage.
- Concatenation versus interpolation could be an interesting one : ” $a $b ” versus $a.’ ‘.$b. I’m sure I’m not consistent with this one.
- ‘ versus ” : they don’t have the same behavior.
Test your preferences with Exakat
In the same time, you may check your own code with Exakat, and learn about consistent and inconsistent behavior. Simply run it on your code, and review the report.