PHP代码缩写
适才在 Jblog 的代码中看到一段PHP代码缩写,备忘一下,积累知识。
- !defined('IN_JBLOG') && exit('Access Denied!');
从直觉上看,这段代码也没有什么出众之处,也就是一个 if 判断,Google 了一下,网上没有太多关于PHP代码缩写规则的文章,在php吧看到关于PHP代码 if 的缩写,可以使用三元表达式来处理。
- if($foo) {
- $str = 'yes';
- } else {
- $str = 'no';
- }
可以缩写成:
- ($foo) ? ($str = 'yes') : ($str = 'no');
而:
- if($foo) {
- $str = 'yes';
- }
可以被缩写为:
- $foo && $str = 'yes';