if(); 是什么意思? do,分号就在括号后面吗?
碰巧,在编写一些 PHP 代码时,我不小心在 if 语句后面加上了分号 ;
。例如:
if($a > 1);
{
....
}
我认为 PHP 在这种情况下应该引发错误,但事实并非如此。这种语法应该有意义,我只是想知道它是什么。
据我所知,当添加 ;
时,条件似乎始终为真,但我完全不确定这就是含义。
It happens that, when writing some PHP code, I accidentally put a semicolon ;
right after an if statement. For example:
if($a > 1);
{
....
}
I thought that PHP should raise an error in this case, but it is not. That kind of syntax should have a meaning, I'm just wondering what it is.
For what I could see the condition seems to be always true when the ;
is added but I'm not sure at all this is the meaning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
单个
;
可以被理解为“空语句”,相当于
它只是看起来像这样,因为无论什么情况都会执行块if 语句。
A single
;
can be read as an "empty statement" andis equivalent to
It only seems like it since the block is executed regardless of the if-statement.
添加分号实际上是在大括号之前结束 if 块。这不是真的,只是你在 if 中没有做任何事情。
如果没有大括号,请这样想:
执行 if 内第一个分号之前的所有操作。因此,在您的情况下,第一个分号之前没有任何内容,因此什么也没有发生。
Adding the semi-colon essentially ends the if block before the braces. It isn't true, it's just that you don't do anything in the if.
Think about it like this, if you don't have braces:
does everything before the first semi-colon inside the if. So in your case, there is nothing before the first semi-colon, so nothing happens.