OldWest Posted January 6, 2011 Share Posted January 6, 2011 My script IS working, but I can't get around a blank array error when no errors exist. Below is my code, and as you can see, I am being handed this: Notice: Undefined variable: error in C:\wamp\www\php\form_validation.php on line 19 as a result of my ... if (is_array($error)) { ... .. I could do if (@is_array($error)) { (note the @), but I hate using that thing... I've tried several things with no luck, so any ideas welcome at this point. <?php if (isset($_POST['set_test'])) { if (!preg_match("/^[A-Za-z' -]{1,50}$/", $_POST['first_name'])) { $error[] = "Please enter a valid First Name"; } if (!preg_match("/^[A-Za-z' -]{1,50}$/", $_POST['last_name'])) { $error[] = "Please enter a valid Last Name"; } if (is_array($error)) { foreach ($error as $err_message) { echo $err_message . "<br />"; } } } ?> Link to comment https://forums.phpfreaks.com/topic/223594-resolve-array-error-wout-error-suppression-operatior/ Share on other sites More sharing options...
BLaZuRE Posted January 6, 2011 Share Posted January 6, 2011 Well, your error is saying your variable's undefined. You need to check whether it's defined or not before you manipulate it. You can use isset($variable) to determine whether $variable is set ("defined") and not NULL. Link to comment https://forums.phpfreaks.com/topic/223594-resolve-array-error-wout-error-suppression-operatior/#findComment-1155759 Share on other sites More sharing options...
PFMaBiSmAd Posted January 6, 2011 Share Posted January 6, 2011 You can also use if(!empty($error)) (for those of us who would define the array at the start of the code.) Link to comment https://forums.phpfreaks.com/topic/223594-resolve-array-error-wout-error-suppression-operatior/#findComment-1155775 Share on other sites More sharing options...
Maq Posted January 6, 2011 Share Posted January 6, 2011 Quote You can also use if(!empty($error)) (for those of us who would define the array at the start of the code.) Not implying you shouldn't initialize your variables, but the empty() method checks if the variable is set I think. Link to comment https://forums.phpfreaks.com/topic/223594-resolve-array-error-wout-error-suppression-operatior/#findComment-1155794 Share on other sites More sharing options...
BLaZuRE Posted January 6, 2011 Share Posted January 6, 2011 In certain scenarios, a variable can still be set but empty. For example: $var = ''; $var is set, to '' but contains an empty string, therefore empty. Not always necessary to know since it doesn't always come up, but they're not equivalent. Link to comment https://forums.phpfreaks.com/topic/223594-resolve-array-error-wout-error-suppression-operatior/#findComment-1155801 Share on other sites More sharing options...
Pikachu2000 Posted January 6, 2011 Share Posted January 6, 2011 See new comments within the code . . . <?php if (isset($_POST['set_test'])) { $error = array(); // initialize an empty array if (!preg_match("/^[A-Za-z' -]{1,50}$/", $_POST['first_name'])) { $error[] = "Please enter a valid First Name"; } if (!preg_match("/^[A-Za-z' -]{1,50}$/", $_POST['last_name'])) { $error[] = "Please enter a valid Last Name"; } if (!empty($error)) { // if array is not empty, implode to a <br> separated string and echo (eliminates foreach loop's trailing <br>). echo implode( '<br>', $error); } } ?> Link to comment https://forums.phpfreaks.com/topic/223594-resolve-array-error-wout-error-suppression-operatior/#findComment-1155811 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.