grahamgr3 Posted April 10, 2014 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>'; } Link to comment Share on other sites More sharing options...
HartleySan Posted April 10, 2014 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. Link to comment Share on other sites More sharing options...
grahamgr3 Posted April 10, 2014 Author Share Posted April 10, 2014 How could I rephrase this if else condition so that it would make sure $url is defined. Link to comment Share on other sites More sharing options...
HartleySan Posted April 10, 2014 Share Posted April 10, 2014 Add $url = ''; below the echo statement within the else block. Link to comment Share on other sites More sharing options...
Recommended Posts