循环通过$ _POST。 循环结束后如何停止?

I want this function to return false when for each statement has stopped looping. And true if it's not empty.

function check_empty($post_array){
        $items_in_array = count($post_array); //array length
        echo $items_in_array;
        foreach($post_array as $key=>$value){
            if(empty($value)){
                echo $key . " field cannot be left empty" . "</br>";
            }
        }
        return false;
    }

Note: if i return false inside foreach it will stop right on first iteration i want it to complete iteration and only stop when it has iterated over all the values in that array($post_array)

if i return false inside foreach it will stop right on first iteration i want it to complete iteration and only stop when it has iterated over all the values

So an empty value would be considered an “error” of some sort, and one such error should be enough to make the function return false?

function check_empty($post_array){
    $no_empty_fields = true;
    foreach($post_array as $key=>$value){
        if(empty($value)){
            $no_empty_fields = false;
            // plus whatever additional handling you need here
        }
    }
    return $no_empty_fields;
}

To stop you can use either break; or return;

break if you want to stop for that itteration
return if you want to exit the loop and function compleatley.

function check_empty($post_array){
    $items_in_array = count($post_array); //array length
    echo $items_in_array;
    if($items_in_array < 1){
         echo 'no data submitted';
         return false;
    }
    $errors = '';
    foreach($post_array as $key=>$value){
        if(empty($value)){
            $errors.= $key . " field cannot be left empty" . "</br>";
        }
    }
    if($errors){
         echo $errors;
         return false;
    }
    return true;
}

Its not obvious what you are asking, but the above return false on the first empty value. If no empty values are discovered, the function will return true

you can use break or return false statement to exit from loop.

  foreach($post_array as $key=>$value){
        if(empty($value)){
            echo $key . " field cannot be left empty" . "</br>";
            break;//or return false;
        }
    }

Return or Break; when your conditions are met.

function check_empty($post_array){
        $items_in_array = count($post_array); //array length
        echo $items_in_array;
        $rtVl = false
        foreach($post_array as $key=>$value){
            if(empty($value)){
                echo $key . " field cannot be left empty" . "</br>";
            }
            $rtVl = true;
        }
        return $rtVl;
}