douxi8759 2014-09-17 11:08
浏览 22
已采纳

foreach声明没有检查所有条件

I'm new to PHP. I'm trying to check that no field is empty in a certain form, so I used a foreach statement but it only checks one by one instead of all at once.

Here is my code:

<?php

if (isset($_POST[submit])) {
    ValidateUser();
}

function ValidateUser() {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $details = array($username, $password);

    foreach($details as $detail) {
        if (!empty($detail)) {
            echo "hurrayy";
        }
    }
}

?>

So instead of displaying "hurrayy" when BOTH username and password are not empty, it displays as long as one of them is not empty. Please help.

  • 写回答

4条回答 默认 最新

  • doudouji2016 2014-09-17 12:29
    关注

    You don't need to assign them to an array and loop

    function ValidateUser() {
        if(!empty($_POST['username']) AND !empty($_POST['password'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];
        echo "Hurray!";
        }
    }
    

    8 lines down to 5, no loop and no array assignment.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动