grahamgr3 1 Posted April 10, 2014 Report Share Posted April 10, 2014 In the following code I am validating a url in a practice page I am using from the stuff I learned in the book. When I enter an invalid url in my form, I get an $url undefined error when running the code below. I am guessing the error is pretty easy to spot, but I am new at this. if (filter_var($scrubbed['url'], FILTER_VALIDATE_URL)){ $url = mysqli_real_escape_string($dbc, $scrubbed['url']); } else { echo '<p class="error">Please enter a valid url</p>'; } Quote Link to post Share on other sites
HartleySan 826 Posted April 10, 2014 Report Share Posted April 10, 2014 Because the $url variable is only defined when the if conditional is met. If the else branch is executed instead, then the $url variable never exists. Quote Link to post Share on other sites
grahamgr3 1 Posted April 10, 2014 Author Report Share Posted April 10, 2014 How could I rephrase this if else condition so that it would make sure $url is defined. Quote Link to post Share on other sites
HartleySan 826 Posted April 10, 2014 Report Share Posted April 10, 2014 Add $url = ''; below the echo statement within the else block. Quote Link to post Share on other sites
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.
Note: Your post will require moderator approval before it will be visible.