Thursday, April 8, 2004, 3:59:39 AM, you wrote:

>> I have this very large and long if statement:
>> if
>>  (!empty($_SESSION['add']['type'])
>>  && !empty($_SESSION['add']['start_date']
>>  && !empty($_SESSION['add']['end_date'])
>>  && !empty($_SESSION['add']['name'])
>>  && !empty($_SESSION['add']['county'])
>>  && !empty($_SESSION['add']['discription'])
>>  && !empty($_SESSION['add']['StartingDay'])
>>  && !empty($_SESSION['add']['StartingMonth'])
>>  && !empty($_SESSION['add']['StartingYear'])
>>  && !empty($_SESSION['add']['EndingDay']})
>>  && !empty($_SESSION['add']['EndingMonth'])
>>  && !empty($_SESSION['add']['EndingYear']))
>>  {//run the insert query}

<snipped>

> As I understand it, !empty is redundant (at least, it's always worked for
> me, but I'm no expert).

In a perfect world you would be right, however if the var isn't set
then you'd get a notice (depending on your error reporting levels).

So I personally prefer to check against the !empty ...

---
That being said I have one more point to make:
Your if statement will fail if any  isn't satisfied. But you have no
way to tell which one was missing...

On pages where multiple fields are required I usually use something
like:

$errors = array();
$check_vars = array('type', 'start_date', 'end_date', ...);

foreach ($check_vars as $var) {
  if (empty($_SESSION['add']["$var"])) {
    $errors[] = $var;
    // or something like: $errors = get_error_desc_for($var);
    // ... just making this up.
  }
}
if (count($errors)) {
  $output_errors = implode('<br />', $errors);
} else {
  // run query
}

While I know that this contains a lot more code it's rather easy to
add another element to check against, and it allows me to pinpoint
exactly what's gone wrong ...

ok. not exactly what you asked for ... I was just in the mood :)

Richard

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to