Php函数未定义


Php function not define

我有一个默认值为none的函数,例如:

function output_message($message="") {
  if (!empty($message)) { 
    return "<p class='"message'">{$message}</p>";
  } else {
    return "";
  }
}

然后我在我的登录表单上回显消息。如果有错误,它会显示一条消息,但如果没有错误,它应该什么都不做。当我的页面加载时,它显示我的函数没有定义。下面是我的html页面。

<?php
require_once("../../includes/functions.php");
require_once("../../includes/session.php");
require_once("../../includes/database.php");
require_once("../../includes/user.php");
if($session->is_logged_in()) {
  redirect_to("index.php");
}
// Remember to give your form's submit tag a name="submit" attribute!
if (isset($_POST['submit'])) { // Form has been submitted.
  $username = trim($_POST['username']);
  $password = trim($_POST['password']);
  // Check database to see if username/password exist.
  $found_user = User::authenticate($username, $password);
  if ($found_user) {
    $session->login($found_user);
    redirect_to("index.php");
  } else {
    // username/password combo was not found in the database
    $message = "Username/password combination incorrect.";
  }
} else { // Form has not been submitted.
  $username = "";
  $password = "";
}
<html>
  <head>
    <title>Photo Gallery</title>
    <link href="../stylesheets/main.css" media="all" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <div id="header">
      <h1>Photo Gallery</h1>
    </div>
    <div id="main">
        <h2>Staff Login</h2>
        <?php echo output_message($message); ?>
        <form action="login.php" method="post">
          <table>
            <tr>
              <td>Username:</td>
              <td>
                <input type="text" name="username" maxlength="30" value="<?php echo htmlentities($username); ?>" />
              </td>
            </tr>
            <tr>
              <td>Password:</td>
              <td>
                <input type="password" name="password" maxlength="30" value="<?php echo htmlentities($password); ?>" />
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <input type="submit" name="submit" value="Login" />
              </td>
            </tr>
          </table>
        </form>
    </div>
    <div id="footer">Copyright <?php echo date("Y", time()); ?>, Kevin Skoglund</div>
  </body>
</html>
<?php if(isset($database)) { $database->close_connection(); } ?>

我不知道为什么会出现这个错误,因为在我的函数中,我在函数中设置了$message的默认值。output_message($message=")。

有人能看看我的代码,告诉我为什么它告诉我我的函数没有定义吗。谢谢

根据您的评论,似乎不是未定义的函数,而是变量。

问题不在于函数本身,如果没有向函数提供变量,则变量$message被设置为空字符串。

问题出在你的函数调用上:

<?php echo output_message($message); ?>

在这里,您使用一个变量调用函数,该变量也称为$message,但与函数中的变量$message完全无关。这个全局变量不存在,这就是php向您发出警告的原因。

你定义函数的方式,意味着你可以把它称为:

<?php echo output_message(); ?>

没有任何问题;如果未提供变量,则函数中的$message变量将设置为空字符串。

但是,使用:

<?php echo output_message($any_variable_name); ?>

$any_variable_name未在您所在的作用域(在本例中为全局作用域)中定义时,将生成警告。

我真的不知道你在哪里使用output_message,但我的第一个猜测是那些函数"redirect_to"可能是问题所在。

这些函数大多数时候都是这样做的:

   header( 'Location: http://some_where_on.yourweb' );

检查您是否在使用output_message函数的.php文件中进行了正确的include。

此外,我建议遵循Phil和jeroen的建议:

  • 丢失?>before标记
  • 添加init_set("display_errors","On");错误报告(E_ALL)
  • 初始化$message等变量以避免警告和副作用
  • 尽量不要把渲染和逻辑混在一起(这个是我的)