微信号:gh_76276767b824

介绍:定期发送PHP相关资讯

Seven syntax you can choose in PHP

2016-12-28 16:43 dams

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.


 
PHP周报 更多文章 The Delicious Evils of PHP 妙用php中的register_shutdown_function和fastcgi_finish_request PHP7 的抽象语法树(AST)带来的变化 深入理解PHP7之zval 喜大普奔,PHP7正式发布!
猜您喜欢 马云:“电子商务”将消失 新五通一平将引领未来 【Matlab量化投资】根据期货高频数据和期货交易所交易规则以及BS方法判断高频交易方向和多空主力建仓减仓行为(附源码!!!) MySQL的Crash Safe和Binlog的关系 Android设计相关的网站推荐 揭秘白帽黑客:优秀女白帽子比大熊猫还稀罕