When the user must enter a lot of data correctly before the data can be stored the code usually looks like this with nested ifs and lots of braces at the end.
[php]
if ( empty ($_POST[‘id’])) // Check for the required value.
{
printMessage(“Please enter a logid!”);
}
else
{
$id = $_POST[‘id’];
if ( empty ($_POST[‘pass’])) // Check for the required value.
{
printMessage(“Please enter a password!”);
}
else
{
$pass = $_POST[‘pass’];
if ( $_POST[‘pass’] != $_POST[‘vpass’]) // Do the passwords match
{
printMessage(“The passwords do not match!”);
}
else
{
$vpass = $_POST[‘vpass’];
if ( empty ($_POST[‘email’])) // Check for the required value.
{
printMessage(“Please enter an email!”);
}
else
{
$email = $_POST[‘email’];
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
printMessage(“Please enter an valid email!”);
}
else
{
$itExists = true;
$itExists = loginExists($id, $host );
if ($itExists)
{
printMessage(“This UserID is already registered!”);
}
else
{
$lastChangeDate = time();
$query = “INSERT INTO Customerinfo_cui (username_key_cui, password_cui, email_cui, date_lastchange_cui)
VALUES ( ‘$id’, ‘$pass’, ‘$email’ , $lastChangeDate)”;
require (‘r_connectToDB.htm’);
if (!mysql_query($query,$con))
{
die('
Error insert: ’ . mysql_error());
}
else
{
printMessage(“You are now registered. Please go to Home Page!”);
gotoAPage(null);
$_SESSION['username'] = $id;
$_SESSION['password'] = $pass;
$_SESSION['email'] = $email ;
$_SESSION['lastChangeDate'] = $lastChangeDate;
}
}
}
}
}
}
}
[/php]
I don’t like the indenting further & further so I came up with the following substitution.
[php]
do
{
if ( empty ($_POST['id'])) // Check for the required value.
{
printMessage("Please enter a logid!");
break;
}
$id = $_POST['id'];
if ( empty ($_POST['pass'])) // Check for the required value.
{
printMessage("Please enter a password!");
break;
}
$pass = $_POST['pass'];
if ( $_POST['pass'] != $_POST['vpass']) // Do the passwords match
{
printMessage("The passwords do not match!");
break;
}
$vpass = $_POST['vpass'];
if ( empty ($_POST['email'])) // Check for the required value.
{
printMessage("Please enter an email!");
break;
}
$email = $_POST['email'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
printMessage("Please enter an valid email!");
break;
}
$itExists = true;
$itExists = loginExists($id, $host );
if ($itExists)
{
printMessage("This UserID is already registered!");
break;
}
$lastChangeDate = time();
$query = "INSERT INTO Customerinfo_cui (username_key_cui, password_cui, email_cui, date_lastchange_cui)
VALUES ( '$id', '$pass', '$email' , $lastChangeDate)";
require ('r_connectToDB.htm');
if (!mysql_query($query,$con))
{
die('<br>Error insert: ' . mysql_error());
}
printMessage("You are now registered. Please go to Home Page!");
gotoAPage(null);
$_SESSION['username'] = $id;
$_SESSION['password'] = $pass;
$_SESSION['email'] = $email ;
$_SESSION['lastChangeDate'] = $lastChangeDate ;
}while (false);
[/php]
The working code is at the end and no indentation & dangling }. Please comment