检查数字=一、二或三+添加是否未设置数字=


Checking if number=ONE, TWO, or THREE + Adding if number= Is NOT Set

这是我到目前为止的代码:

$onetwothree = (isset($_GET['number']) && (
    $_GET['number'] == 'one' ||
    $_GET['number'] == 'two' ||
    $_GET['number'] == 'three'));

如果?number=one?number=two?number=three,这将显示内容,但如果 url 中没有number=,我如何也显示内容?

最简单的方法是使用额外的条件:

if (!isset($_GET['country']) || $onetwothree) {
    // display content
}

如果要根据值显示不同的内容:

if (isset($_GET['number'])) {
    if (!isset($_GET['country'])) {
        // display content
    } else if (in_array($_GET['country'], array('one', 'two', 'three')) {
        // display other content
    }
}

我采用了一个快捷方式并在第二个示例中使用了in_array();如果您想扩展接受值的列表(而不是添加更多$_GET['country'] == 'xxxx'行),它将在将来使事情变得更容易一些。