IF语句中括号的区别是什么? [英] whats the difference in parentheses in IF statements?

查看:72
本文介绍了IF语句中括号的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与PHP&javascript,仍然学习btw,以下内容似乎会产生相同的结果.

I've noticed in my dealings with PHP & javascript, still learning btw, that the following seems to produce the same results.

        if( ( $A==0 ) && ( $B==0 ) ){}

        if( $A==0 && $B==0 ){}

在编程中什么是恰当的术语,这样我就可以了解更多信息.

What is the proper term for this in programming so I can learn more about it.

推荐答案

括号确定了进行比较的顺序.您的示例是一个非常简单的示例,根本不需要括号,但请看一下类似的东西

Parenthesis determine the order in which comparisons are made. Your example is a pretty simple one that doesn't need parenthesis at all, but look at something like this

if ($a == 0 || $b == 0 && $c == 0 || $dd == 0)

这实际上等于

if ($a == 0 || ($b == 0 && $c == 0) || $dd == 0)

因为&&在PHP中首先被评估,因为它的优先级高于 |||

because && is evaluated first in PHP as it has a higher precedence than the ||

在大多数情况下,当您有复杂的条件时,您要确保使用括号,如果不能正确确定操作顺序,则至少可以使代码阅读器清楚地知道要执行的操作.

IN most cases, when you have complex conditionals, you want to make sure to use parenthesis, if not to get the order of operations right, then at least to make it clear to the code reader what you are trying to do.

这篇关于IF语句中括号的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆