DVigneault Posted December 23, 2012 Share Posted December 23, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/272317-should-i-worry-about-a-notice-if-ive-accounted-for-it/ Share on other sites More sharing options...
berridgeab Posted December 23, 2012 Share Posted December 23, 2012 (edited) Set the variable to get rid of the notice or turn notices off. Variables don't have to be preset in PHP, however the compiler will "notice" you about it. Edited December 23, 2012 by berridgeab Quote Link to comment https://forums.phpfreaks.com/topic/272317-should-i-worry-about-a-notice-if-ive-accounted-for-it/#findComment-1401025 Share on other sites More sharing options...
Christian F. Posted December 23, 2012 Share Posted December 23, 2012 (edited) 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 December 23, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/272317-should-i-worry-about-a-notice-if-ive-accounted-for-it/#findComment-1401044 Share on other sites More sharing options...
DVigneault Posted December 23, 2012 Author Share Posted December 23, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/272317-should-i-worry-about-a-notice-if-ive-accounted-for-it/#findComment-1401051 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.