RE: [PHP] elseif without else
| From: | Martin Towell | Date: | Fri, 01 Mar 2002 01:41:43 +0000 |
| Subject: | RE: [PHP] elseif without else | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-86703@lists.php.net to get a copy of this message | ||
In this case I would assign false to $error before your if's - just to make
sure it's set to something definite - that is unless you have already set it
furthur up in your code...
Martin
-----Original Message-----
From: Erik Price [mailto:pricee@hhbrown.com]
Sent: Friday, March 01, 2002 11:59 AM
To: Jeff Sheltren
Cc: PHP
Subject: Re: [PHP] elseif without else
On Thursday, February 28, 2002, at 07:42 PM, Jeff Sheltren wrote:
> If you are setting a variable inside of the if statements, and then
> checking that variable later, then I would say you should either use an
> else statement to give that variable a default value, or you should set
> the variable to a default value before the if statements. Your code is
> "legitimate", ie. there is no reason you *have* to have an else
> statement.
>
I would have thought that if I set this variable at all, then I wouldn't
be able to use the second part of the script ( if($variable)... ). I
have posted a snippet of the code at the bottom, if you don't mind
checking it out. It works fine -- but it seems strange not to have an
'else'. The bare bones of it is like so:
if (bad user input) {
$error = true;
$error_message = $a;
} elseif (different bad user input) {
$error = true;
$error_message = $b;
} elseif (still different bad user input) {
$error = true;
$error_message = $c;
}
if ($error) {
do some things
and then
echo $error_message;
} else {
present confirmation dialog
}
----
Erik Price
Web Developer Temp
Media Lab, H.H. Brown
pricee@hhbrown.com
for ($i = 1; $i <= $_POST['numfiles']; $i++) {
// check text box for filename - no danger characters!
if (preg_match("/[<>\"'\\/:]/", $_POST["filename{$i}"])) {
$input_error_confirm = true;
$error_message = $filename_is_invalid;
$problem_file_number = $i;
$problem_file_name =
htmlspecialchars($_POST["filename${i}"]);
// check text boxes for filesize, width, and height, must be of
form 0000.00
} elseif ( !preg_match("/^\d{1,4}\.?\d{0,2}$/",
$_POST["filesize${i}"]) ||
!preg_match("/^\d{1,4}\.?\d{0,2}$/",
$_POST["width${i}"]) ||
!preg_match("/^\d{1,4}\.?\d{0,2}$/",
$_POST["height${i}"]) ||
!preg_match("/^\d{1,4}$/",
$_POST["resolution{$i}"]) ) {
$input_error_confirm = true;
$error_message = $filenum_is_invalid;
$problem_file_number = $i;
$problem_file_name =
htmlspecialchars($_POST["filename${i}"]);
// check text boxes to make sure that the default values (0.00) are
not left unchanged
} elseif ( (float) $_POST["filesize{$i}"] < 0.01 ||
(float) $_POST["width{$i}"] < 0.01 ||
(float) $_POST["height{$i}"] < 0.01 ||
(int) $_POST["resolution{$i}"] < 1 ) {
$input_error_confirm = true;
$error_message = $filenum_is_zero;
$problem_file_number = $i;
$problem_file_name =
htmlspecialchars($_POST["filename{$i}"]);
} elseif ($_POST["format{$i}"] == 0) {
$input_error_confirm = true;
$error_message = $no_format_specified;
$problem_file_number = $i;
$problem_file_name =
htmlspecialchars($_POST["filename{$i}"]);
}
}
// if errors in text inputs, error messages must be returned from
this function
if ($input_error_confirm) {
$data_to_return = " <p class=\"warning\">Unfortunately, there
was an error in your data, in file number <b
class=\"label\">${problem_file_number} (" .
stripslashes(${problem_file_name}) . ").</b></p>
<p
class=\"warning\">$error_message</p>\n";
$data_to_return .= addrecord_form($_POST['numfiles']);
// otherwise text inputs are fine, display them to the user for
confirmation
} else {
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php