is being used outside of the actual test_input function and this works. I thought it wouldn't work because it's outside the function. Or am I misunderstanding?
PHP
Last Comment
zephyr_hex (Megan)
8/22/2022 - Mon
zephyr_hex (Megan)
The first segment of code is not a function. It's a conditional statement, and as such, it is in the same realm / scope as the test_input function.
That being said, some IDE's will throw a warning because the function should be written above the point where it's called. In other words, the function should appear before the conditional statement.
Crazy Horse
ASKER
Yeah, I also thought it strange that they had the function at the bottom but that is how they did it.
Could I make it all into one function if I wanted to use it all as a form validation function?
function test_input($data) { $message = ""; $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data;if (empty($_POST["gender"])) { $message .= "Gender is required"; } else { $gender = test_input($_POST["gender"]); } if($message) { echo "There were errors in your form:<br>" . $message; } else { //submit form } }
That being said, some IDE's will throw a warning because the function should be written above the point where it's called. In other words, the function should appear before the conditional statement.