0

I have now a function like this

function checkInput($content, $emptyMessage = ""){
    if(isset($content)){ 
       return sanitize($content);
    }else{
       return $emptyMessage;
    }
 }

And I use it like this:

/* First get all data out of key and put it in array*/

//now check the input if user has set certain fields
$email = checkInput($user["email"],"No email address given");

echo $email;

Now I get: Uncaught ErrorOrWarningException. I can think of two solutions:

Turning off errors > dont like that one.

Do manually for every field like

if(isset($user["email"])){ 
     $email = sanitize($user["email"]);
}else{
     $email =  "No email address given";
}
echo $email;

That sucks for readibility. Another way would be to try / catch but thats almost the same length to type

2
  • 1
    Why write your own validation logic when you can use something like filter_var() and just worry about passing in the correct options? Commented May 30, 2011 at 1:57
  • I am not sure but I think the problem with your function, the second argument is optional, so it may not be set at all, then you try to return a variable may or may not set. So I suggest to make your second argument not optional and see what happen then. Commented May 30, 2011 at 2:04

3 Answers 3

1

When declaring a function with parameters, the parameter will always be set inside the function.

function checkInput($content, $emptyMessage = "") {
    // $content is always "set" here, no need for isset()

You're getting a warning if you're trying to access a non-existent variable here:

checkInput($user["email"], "No email address given");
  //       ^^^^^^^^^^^^^^
  // if 'email' is not set, a warning is thrown here

You'll always need to make sure the "email" key is set before trying to pass its value into a function.

Having said that, that's probably not the cause of your ErrorOrWarningException. I have no idea where that comes from, it's not standard PHP. I suppose your sanitize function is throwing it. If so, you'll need to try and catch inside your checkInput function.

Sign up to request clarification or add additional context in comments.

Comments

0

If you don't want to use a try/catch, you can do something like this earlier in your code:

function exception_handler($exception) {
    //Insert whatever you want to do when an exception is raised outside of a try/catch
} 

set_exception_handler('exception_handler');

Comments

0

What about something like this:

function checkInput( $var, $emptyMessage = '' )
  if ( isset( $$var ) ) {
    return sanitize( $$var );
  } else {
    return $emptyMessage;
  }
}

or, similarly,

function checkInput( $var, $key, $emptyMessage = '' )
  if ( isset( $var[$key] ) {
    return sanitize( $var[$key] );
  } else {
    return $emptyMessage;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.