search

Home  >  Q&A  >  body text

Why does my switch automatically output the first case?

<form action="" method="get">
    <label for="score">请输入成绩:</label><input type="number" id="score" name="score" max="100" value="">
    <button>提交</button>
</form>
$init = $_GET['score'];
switch($init){
    case $init >=90 :
    echo '<p>厉害了,我的学霸!</p>';
    break;
    case $init >=80 :
    echo '<p>再接再厉,努力吧!</p>';
    break;
    case $init >=70 :
    echo '<p>差太多啦!得加油赶上了!!</p>';
    break;
    case $init >=60 :
    echo '<p>建议你留级吧!!</p>';
    break;
    default:
        break;
}

1.png

Obviously no parameters have been passed to the form yet, so how come the first case is output? At this time, the $init variable is empty. How can it be judged successful? ? What's going on? ?

MArtianMArtian1868 days ago1243

reply all(2)I'll reply

  • 卢小强

    卢小强2019-11-17 09:12:23

    If you submit without inputting anything, the submitted value will be empty. Empty does not meet any switch conditions, so it will start execution from the first CASE and will not stop until it encounters break

    reply
    0
  • MArtian

    Will switch run even if the conditions are not met? That's it, thank you

    MArtian · 2019-11-17 12:41:30
  • Cancelreply