Home  >  Article  >  Backend Development  >  PHP属于解析型语言,请问以下语句性能上有什么区别吗?

PHP属于解析型语言,请问以下语句性能上有什么区别吗?

WBOY
WBOYOriginal
2016-06-06 20:37:111130browse

<code># 使用if进行判断
if( !defined('ROOT_PATH') ) define('ROOT_PATH', realpath('./').DIRECTORY_SEPARATOR);
# 使用or进行判断
defined('ROOT_PATH') or define('ROOT_PATH', realpath('./').DIRECTORY_SEPARATOR);
!defined('ROOT_PATH') and define('ROOT_PATH', realpath('./').DIRECTORY_SEPARATOR);
</code>

回复内容:

<code># 使用if进行判断
if( !defined('ROOT_PATH') ) define('ROOT_PATH', realpath('./').DIRECTORY_SEPARATOR);
# 使用or进行判断
defined('ROOT_PATH') or define('ROOT_PATH', realpath('./').DIRECTORY_SEPARATOR);
!defined('ROOT_PATH') and define('ROOT_PATH', realpath('./').DIRECTORY_SEPARATOR);
</code>

if效率要比短路写法效率高,因为if只需要判断if括号里的条件是否为真,如果为真,直接执行条件里的代码,不管里面代码返回的是真是假。而or或者and在执行后面代码后,会判断返回值是否为真。

个人表示,性能上没有区别
都是1次判断或者1次判断+定义

自己测试一下不就知道了

<code>    (1==1) || print(2);
    (1==1) && print(2);
</code>

结果显示,如果是或,第一个为真,则不会运行后面的表达式,如果第一个为假,则会运行
如果是与,如果第一个为假,则不会运行后面的表达式,否则则运行
所以跟if的效果是一样的,而且写法更简洁,尤其适合开始的预定义

if里只有一次计算。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn