Call to undefined function - though function is defined in the same file

Lines 84-88: [php]if (empty($_POST[‘HomeInfo’])) {
$infoErr = "Information about your home is required. ";
} else {
$HomeInfo = test_input($_POST[‘HomeInfo’]);
}
[/php]

Lines 120-125: [php]function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}[/php]

So, why is it saying it is undefined, when it clearly is defined? And, more importantly, how do I fix it?

[php]if ( isset($_POST[‘HomeInfo’]) && !empty(trim($_POST[‘HomeInfo’])) ) {
$result = html_escape($_POST[‘HomeInfo’]);
} else {
$infoErr = ‘Information about your home is required.’;
}

function html_escape($raw_input)
{
// important! don"t forget to specify ENT_QUOTES and the correct encoding
return htmlspecialchars($raw_input, ENT_QUOTES | ENT_HTML5, “UTF-8”);
}[/php]

This is what I would do and note I didn’t test this out, so I don’t know if this will fully work. However, it should get you close.

PHP can sometimes be very finicky. Take all of your used functions and either move them to the top, before you process anything ( ie use them), or place them in a separate file and include it at the top of the page.

I don’t know what your JavaScript experience is, but best practice is to place all JavaScript at the bottom, just before the closing body tag. The reason, so everything has been created before the JavaScript tries to do anything with it.

Kind of the same principal with PHP.

Thank you!

I moved the function definition to the top of the file and it worked great. I don’t know why I didn’t think of that before, but I appreciate the suggestion!!

Technically functions should work anywhere you put them, but it is best practice to put them before the call. I tend to have functions that I use throughout the website located in the the config.php or utilities.inc.php, so that it will get loaded into memory when a webpage is landed on. It can also be used on multiple pages, it’s quick and dirty way of doing something the procedural way that you know that is going to used over and over again. While I agree that you should have JavaScript at the bottom of the html page, just before the closing body tag, I kind of disagree with PHP. Most of the PHP should be at the top of the page as possible, it’s a server-side language it’s going to be loaded first anyways. There are a few things in PHP that force you to put the code first, such as using namespaces and I believe autoloaders.

Strider, I think you misunderstood. PHP doesn’t want the functions at the bottom, but like JavaScript, it wants them in a particular area, before it starts using them.

Sponsor our Newsletter | Privacy Policy | Terms of Service