尝试通过给定输入回显条件,但结果是给定输入的单个结果条件的三倍

时间:2019-03-13 22:51:52

标签: php html

**您好,这是代码,请回答我的问题。我正在尝试通过给定的输入来回响条件,但是结果是我想要的给定输入的单个结果条件的三倍**

<?php
$arrayName = array('bravo', 'alpha', 'jhony');

foreach ($arrayName as $key) {
    if (isset($_REQUEST['num1']) && $_REQUEST['num1'] == $key) {
        echo "yes". $_REQUEST['num1']. "Available";
    } else {
        echo "Sorry!".$_REQUEST['num1']." is not available";
    }
}
?>


<form action="" method="get" accept-charset="utf-8">
<input type="text" name="num1">
<button type="submit">submit</button></form>

1 个答案:

答案 0 :(得分:2)

使用in_array而不是遍历白名单:

$arrayName = array('bravo','alpha','jhony' );


if(isset($_REQUEST['num1']) && in_array($_REQUEST['num1'], $key)){
 echo "yes". $_REQUEST['num1']. "Available";
}
else{
    echo "Sorry!".$_REQUEST['num1']." is not available";
}