我想检查数组中的名称,如果匹配显示一条消息。问题是条件语句会转到 else 语句。
<?php
$names = ['user1','user2','test1','test2'];
$name = "user1";
for($i=0; $i < count($names) ; $i++) {
if($name == $names[$i]) {
echo "you are registered";
}else {
echo "you are unregistered";
}
}
?>
因此,一旦找到注册用户,您就不会遍历所有数组,请尝试使用此构造
$names = ['user1','user2','test1','test2'];
$name = "user1";
for($i=0; $i < count($names) ; $i++) {
if($name == $names[$i]) {
echo "you are registered";
// found a user, terminate the for loop
break;
}
// if you dont find a registeted user you will do this
echo "you are unregistered";
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句