Jump to content

Should I Worry About A Notice, If I've Accounted For It?


DVigneault
 Share

Recommended Posts

Hey all–

 

I've got a function that uses data from a variable from a user. Originally it was set up as follows:

 

 

<?php
function($_POST['variable']) {
if (isset($_POST['variable']) && preg_match($_POST['variable'], $pattern)) {
//Do something
}
else {
//Do something else
}
?>

 

However, when I do this before giving any input, I get a notice saying that the variable isn't set. Now I realize that the notice is telling me something that I am accounting for in my function, and that the user won't see the notices when I turn off error reporting. However, the notice still makes me uncomfortable--should I be worrying about it? And, if so, how do I change it? I know that I can check to see if the variable is set before feeding it to the function, but this seems to partially defeat the purpose of having the function (if I need to re-write the same code over and over again each time I call it).

 

Any thoughts or advice?

 

Best,

 

–Davis

Link to comment
Share on other sites

I don't think you've quite grasped the logic behind creating user-defined functions, to be honest. As the opening line states that you want to save the value, passed to the function when calling it, should be stored in the $_POST['variable'] index. After which you check if it's set.

Now, not only will it always be set at that point, as you have to send a value to the function, but I also suspect that storing in there isn't quite what you wanted. Reason for that, is that you'll overwrite any previous value in that index when calling the function.

You've also used an anonymous function, which I don't quite see the reason for. At least not from the limited example you've posted.

 

I think you'll find that this code performs more to your expectations:

// First set the result variable to a default.
$output = 'No value';

// Then check if we have a value submitted.
if (isset ($_POST['variable'])) {
   // We do, now send it to the function for processing.
   $output = do_stuff ($_POST['variable']);
}

// Let's define what the function should do.
function do_stuff ($variable) {
   // Define the pattern we're looking for, and search for it.
   $pattern = "/test \d+/";
   if (!preg_match ($pattern, $variable, $matches)) {
       // No match found, return error/message.
       return "Pattern not found";
   }

   // Do stuff, and return the result.
   $result = "Found ".$matches[0];
   return $result;
}

Edited by Christian F.
Link to comment
Share on other sites

Thanks to both of you for your responses!

 

@Christian -- I see that using "function" as the name in my example was a bad idea. The actual function is called buildInputField and is defined in an include. My question was really whether there was a way to check if the user has submitted the POST variable within the function, to prevent needing to wrap the function with isset each time I call it, without getting a notice. Per your code, it seems like performing that check before calling the function is the way to go.

 

Best, and thanks again,

 

–Davis

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.