if statement basic rule

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Ludwe
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Oct 19, 2017 12:53 pm

Thu Oct 19, 2017 1:09 pm

Hows it Guys.

I'm a new php student and I am having difficulty understanding this code which uses a foreach and if statement. This is part of a program that checks the empty fields submitted from a form. The program has all the fields as required fields exept 'Middle Name' in the form. The code Im refering to is below:

foreach($_POST as $field => $value)
{
if($field != "middle_name")
{
if(empty($value))
{
$blank_array[] = $field;
}
}
}

The $_POST array submitted from the form is as follows:

Array
(
[first_name] => Ludwe
[middle_name] =>
[last_name] => Mgqaliso
[phone] => 0817143661
)

I understand the program starts by walking through the $_POST array. Inside the foreach statement we have an if statement that checks if the $field variable is not equal to the $_POST key value "middle_name".

My question is: The condition should not be true because when the foreach statement gets to the $_POST key value "middle_name" it will find that it does exist making the $field != "middle_name" argument inside the first if statement false. So why does the program carry on executing the this code inside the first if statement:

if(empty($value))
{
$blank_array[] = $field;
}

Please see below the full program for reference.

<?php
/* Program name: checkBlank.php
* Description: Program checks all the form fields for
* blank fields.
*/
?>

<html>
<head>
<title>Customer Phone Number</title>
<style type='text/css'>
<!--
form { margin: 1.5em 0 0 0; padding: 0; }
.field { padding-top: .5em; }
label { font-weight: bold; float: left; width: 20%;
margin-right: 1em; text-align: right; }
#submit { margin-left: 35%; padding-top: 1em; }
-->
</style>
</head>

<body>

<?php

echo "<pre>";
print_r ($_POST);
echo "</pre>";


/* set up array with all the fields */
$labels = array( "first_name" => "First Name",
"middle_name" => "Middle Name",
"last_name" => "Last Name",
"phone" => "Phone");

/* check each field except middle name for blank fields */
foreach($_POST as $field => $value)
{
if($field != "middle_name")
{
if(empty($value))
{
$blank_array[] = $field;
}
}
}

/* if any fields were blank, display error message and redisplay form */

if(@sizeof($blank_array) > 0) //blank fields are found
{
echo "<p><b>You didn't fill in one or more required fields. You must enter:</b><br />\n";

/* display list of missing information */
foreach($blank_array as $value)
{
echo "&nbsp;&nbsp;&nbsp;{$labels[$value]}<br />\n";
}
echo "</p>";

/* redisplay form */
echo "<form action='$_SERVER[PHP_SELF]' method='POST'>\n";
foreach($labels as $field => $label)
{
$good_data[$field]=strip_tags(trim($_POST[$field]));

echo "<div class='field'>
<label for='$field'>$label</label>
<input type='text' name='$field'
id='$field' size='65'
maxlength='65'
value='$good_data[$field]' />\n
</div>\n";
}

echo "<div id='submit'><input type='submit'
value='Submit Phone Number' />\n";

echo "</div>\n</form>\n</body>\n</html>";
exit();
}

echo "All required fields contain information";
?>

Thanks in advance :)

chorn
php-forum Active User
php-forum Active User
Posts: 405
Joined: Fri Apr 01, 2016 2:18 am

Fri Oct 20, 2017 1:58 am

your example is insuffient, works as expected

https://3v4l.org/tD7BR

Post Reply