Home headlines A confusing question about if else!

A confusing question about if else!

Jul 10, 2020 pm 02:36 PM

A confusing question about if else!

这个本来是之前在微博上有个同学说他经常用来面试别人,大概是说,对于如下代码,你觉得会输出啥:

$a = true;
if ($a) {
  echo “true”;
} else label: {
  echo “false”;
}
Copy after login

当时觉得有点偏,没想写,今天中午又有人问我,我想那就介绍下这个原因吧.

首先,上面的代码输出truefalse, 如果你知道原因,那就不用继续往下看了,如果不知道,那么:

这块让人比较迷惑的原因可能是因为,我们会很直观的认为:

label : {
  statement;
}
Copy after login

应该是一个整体, 就好比类似:

if ($a) {
} else switch($a) {
}
Copy after login

或者:

if ($a) {
} else do {
} while (!$a);
Copy after login

因为在PHP的语法设计中,if else本质上是:

if_stmt:
 if_stmt_without_else T_ELSE statement
Copy after login

也就是说,else后面可以接一切statement,如果条件不成立,执行流就跳到else后面的statement,而while, switch都可以归约为statement。

但label这块稍微有点特别(可以说是一个设计违反直觉的”缺陷”吧), 在zend_language_parser.y中:

statement:
  ...
  | T_DO statement T_WHILE '(' expr ')' ';' {...}
  | T_SWITCH '(' expr ')' switch_case_list {...}
  | T_STRING ‘:’ { $$ = zend_ast_create(ZEND_AST_LABEL, $1); }
Copy after login

大家可以看到, do while, switch 都会联合他们的body归约为statement(语句),但标签(label)有点不同,”label :”本身会规约为一条statement, 这就导致了这个看起来比较迷惑的问题的出现,他本质上就变成了:

$a = true;
if ($a) {
 echo "true";
} else {
 label: ;  //单独的一条语句
}
echo "false";
Copy after login

最后多说一句,我忘了之前在那看到的,说是这个世界上本无elseif,有的只不过是else (if statement),本质上其实就跟这个意思是一样的。 就是,else后面可以接语句(statement)。

善用这个结合switch, for, do while等,有的时候可以让我们的代码更精简。

比如,我们要遍历处理一个数组,当数组的长度为零的时候,要做点其他事,那很多人可能会这么写:

if (count($array)) {
  for ($i = 0; $i < count($array); $i++) {
  }
} else {
  //数组为空的逻辑
}
Copy after login

但你也可以写成:

if (count($array) == 0) {
   //数组为空的逻辑
} else for ($i = 0; $i < count($array); $i++) {
}
Copy after login

至于这俩中写法孰好孰坏, 那就是萝卜白菜了。

最后,大家如果在实际中遇到类似让大家觉得迷惑的问题,可以留言,也许以后也可以单独成文。

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)