Fatal error: Call to undefined function reg_form() in C:\xampp\htdocs\php\BEGINNING PHP 5\mysql\form_test.php on line 18
Hy Guys,
I'm new to PHP, The PHP book I am referring it says - "Functions can be called before the actual function definition appears in code.". In the following code, I am calling _reg_form();_ before the function is defined below but I am the getting the error. What am I doing wrong.
Thanks for taking a look.
<?php
include('common_db.inc');
include('validation.php');
if(!$link=db_connect()) die(sql_error());
if($_POST['submit'])
{
$userid = $_POST['userid'];
$userpassword=$_POST['userpassword'];
$username=$_POST['username'];
$userposition = $_POST['userposition'];
$useremail=$_POST['useremail'];
$userprofile=$_POST['userprofile'];
$result=validate();
if($result==0)
{
reg_form();
}
else
{
mysql_query("INSERT INTO user VALUES(NULL,'$userid',Password('$userpassword'),'$username','$userposition','$useremail','开发者_开发技巧$userprofile')",$link);
}
}
else
{
?>
<?php
function reg_form()
{
echo "<table border='1'>
<form action='form_test.php' method='post'>
<tr><td>Desired ID:</td>
<td><input type='text' size='12' name='userid' /></td></tr>
<tr><td>Desired Password:</td>
<td><input type='password' size='12' name='userpassword' /></td></tr>
<tr><td><input type='hidden' name='submit' value='true' />
<input type='submit' value='Submit' />
<input type='reset' value='Reset' /></td></tr>
</form>
</table>";
}
reg_form();
?>
<?php
}
?>
You are defining a conditional function. The manual you are referring to says this (emphasis mine):
Functions need not be defined before they are referenced, except when a function is conditionally defined
Your code, shortened:
if ($something) {
reg_form(); // use function (not defined)
} else {
function reg_form() {
// define function only if (!$something)
}
}
Therefore, your function is only defined in the other branch. You'd need something like this, where the definition of your function is always executed:
if ($something) {
reg_form(); // use function (already defined since the script was loaded)
} else {
// something else
}
// not conditional - will be loaded when script starts
function reg_form() {
// define function
}
You're defining the function within the else
clause of an if()
block. Most likely the function call is occuring from one of the other blocks, so the function will not have been parsed yet at the time of the call:
if (..) {
reg_form();
} else {
function reg_form() { ... }
}
will not work. THe function should be defined at the top level of the code, outside of any functions or logic constructs.
Your function reg_form is defined outside of scope in which it is called.
精彩评论