折影轻梦
文章74
标签43
分类4
利用 goto 来防止 if 嵌套

利用 goto 来防止 if 嵌套

流程控制中的 if 是比较简单的逻辑判断,但是在多条逻辑判断中很容易造成 if 嵌套,逻辑复杂度较高,让人感到十分混乱。

万恶的 if 嵌套

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
$a = array(
    'state' => '1',
    'notice' => '注册成功!',
);

if ($_GET['username'] != '') {
    if (strlen($_GET['username']) > 4 or !strlen($_GET['username']) < 11) {
        $name = array('root','admin','GETmaster','master','webmaster','mixcm','administrator','sb','shabi');
        if (!in_array($_GET['username'], $name)) {
            if (preg_match("/^[a-zA-Z\s]+$/", $_GET['username'])) {
                $a['state'] = '0';
                $a['notice'] = '用户名必须为英文!';
            }
        } else {
            $a['state'] = '0';
            $a['notice'] = '非法用户名!';
        }
    } else {
        $a['state'] = '0';
        $a['notice'] = '请输入大于4字符,且小于11个字符的用户名!';
    }

} else {
    $a['state'] = '0';
    $a['notice'] = '用户名不能为空!';
}

echo json_encode($a,JSON_UNESCAPED_UNICODE);

于是在实际操作中就会想方设法避免以上格式。便有了 goto 和 表数据。

  • 逻辑清晰
  • 后期修改容易
  • 但需要防止 goto 滥用,建议只定义一个,列如本例就只定义了 end

利用 goto 解决(以下代码中有使用到表数据)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
$a = array(
    'state' => '1',
    'notice' => '注册成功!',
);

if ($_GET['username'] == '') {
    $a['state'] = '0';
    $a['notice'] = '用户名不能为空!';
    goto end;
}

if (strlen($_GET['username']) < 4 or strlen($_GET['username']) > 11) {
    $a['state'] = '0';
    $a['notice'] = '请输入大于4字符,且小于11个字符的用户名!';
    goto end;
}

$name = array('root','admin','GETmaster','master','webmaster','mixcm','administrator','sb','shabi');
if (in_array($_GET['username'], $name)) {
    $a['state'] = '0';
    $a['notice'] = '非法用户名!';
    goto end;
}

if (!preg_match("/^[a-zA-Z\s]+$/", $_GET['username'])) {
    $a['state'] = '0';
    $a['notice'] = '用户名必须为英文!';
    goto end;
}

end:
echo json_encode($a,JSON_UNESCAPED_UNICODE);
本文作者:折影轻梦
本文链接:https://nexmoe.com/1280.html
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可