【问题标题】:Php function not definephp函数未定义
【发布时间】:2012-02-09 07:21:32
【问题描述】:

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

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="").

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

【问题讨论】:

  • &lt;html&gt; 之前缺少结束?&gt;output_message() 函数在哪里定义?另外,我建议您为开发启用体面的错误报告。将其放在脚本的顶部; ini_set('display_errors', 'On'); error_reporting(E_ALL);
  • 您可能还想初始化您的$message 变量以避免未定义变量的警告(与未定义函数无关...)。
  • @phil 它位于我的包含文件夹中的函数文件中,该文件夹包含在文档顶部。
  • @Jeroen 我以为我在构建函数时将其定义为 output_message($message="") 我设置了默认值。当我在函数外部的文档顶部定义 $message = "" 时,一切正常 - 所以现在我的问题是为什么我的默认值不在函数中工作。我通过 output_message($message="") 设置默认值。

标签: php model-view-controller oop class function


【解决方案1】:

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

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

问题在于你的函数调用:

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

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

你定义你的函数的方式,意味着你可以这样称呼它:

<?php echo output_message(); ?>

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

但是,使用:

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

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

【讨论】:

    【解决方案2】:

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

    这些函数大部分时间都是这样的:

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

    检查您在使用 output_message 函数的 .php 文件中是否包含正确的内容。

    另外,我建议听从 Phil 和 jeroen 作为 cmets 的建议:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      • 2019-08-11
      • 1970-01-01
      • 2011-10-25
      相关资源
      最近更新 更多