Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
致命错误:在第18行的C:\xampp\htdocs\php\beging php 5\mysql\form_test.php中调用未定义的函数reg_form()_Php - Fatal编程技术网

致命错误:在第18行的C:\xampp\htdocs\php\beging php 5\mysql\form_test.php中调用未定义的函数reg_form()

致命错误:在第18行的C:\xampp\htdocs\php\beging php 5\mysql\form_test.php中调用未定义的函数reg_form(),php,Php,嗨,伙计们 我是PHP新手,我所指的PHP书籍中说:“在实际函数定义出现在代码中之前,可以调用函数。”。在下面的代码中,我调用_reg_form();\u在下面定义函数之前,但我是错误的获取者。我做错了什么 谢谢你看 <?php include('common_db.inc'); include('validation.php'); if(!$link=db_connect()) die(sql_error()); if($_POST['submit']) { $userid = $

嗨,伙计们

我是PHP新手,我所指的PHP书籍中说:“在实际函数定义出现在代码中之前,可以调用函数。”。在下面的代码中,我调用_reg_form();\u在下面定义函数之前,但我是错误的获取者。我做错了什么

谢谢你看

<?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
}
?> 

您正在
if()
块的
else
子句中定义函数。函数调用很可能发生在其他块中,因此调用时函数尚未被解析:

if (..) {
   reg_form();
} else {
   function reg_form() { ... }
}

这是行不通的。函数应该在代码的顶层定义,在任何函数或逻辑构造之外。

您正在定义一个条件函数。我强调说:

函数在被引用之前不需要定义,除非函数是有条件定义的

您的代码,简称:

if ($something) {
     reg_form(); // use function (not defined)
} else {
     function reg_form() {
       // define function only if (!$something)
     }
}
因此,您的函数只在另一个分支中定义。您需要这样的东西,函数的定义总是执行的:

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
}

您的函数注册表在调用它的范围之外定义。

为什么不先定义函数?我不确定你所做的是可能的还是不可能的(我猜不是),但这并不重要,因为在任何情况下这都是一种可怕的编码风格PHP没有这样的函数作用域概念。有条件定义的全局函数仍然存在于全局作用域中,但直到执行包含其定义的代码分支时,它才存在。