【发布时间】: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="").
谁能看看我的代码并告诉我为什么它告诉我我的函数没有定义。谢谢。
【问题讨论】:
-
在
<html>之前缺少结束?>?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