Php函数未定义
我有一个默认值为nothing的函数,例如:Php函数未定义,php,model-view-controller,oop,class,function,Php,Model View Controller,Oop,Class,Function,我有一个默认值为nothing的函数,例如: function output_message($message="") { if (!empty($message)) { return "<p class=\"message\">{$message}</p>"; } else { return ""; } } 函数输出\消息($message=”“){ 如果(!empty($message)){ 返回“{$message}”; }否则{ 返
function output_message($message="") {
if (!empty($message)) {
return "<p class=\"message\">{$message}</p>";
} else {
return "";
}
}
函数输出\消息($message=”“){
如果(!empty($message)){
返回“
”;
}否则{
返回“”;
}
}
然后我在我的登录表单上回显消息。如果有错误,它将显示一条消息,但如果没有错误,它应该什么也不做。当我的页面加载时,它说我的函数没有定义。下面是我的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(); } ?>
我真的不知道您在哪里使用输出消息,但我的第一个猜测是那些函数“重定向到”可能是问题所在
这些函数大部分时间都执行以下操作:
header( 'Location: http://some_where_on.yourweb' );
检查您是否在使用output_message函数的.php文件中进行了正确的包含
此外,我建议遵循Phil和jeroen的建议作为评论:
- 缺少?>在标记之前
- 添加初始设置(“显示错误”、“打开”);错误报告(E_全部)
- 初始化变量,如$message,以避免警告和副作用
- 尽量不要把渲染和逻辑混为一谈(这是我的)
根据您的评论,似乎没有定义的不是函数,而是变量
问题不在于函数本身,如果没有向函数提供变量,则变量
$message
被设置为空字符串
问题在于函数调用:
<?php echo output_message($message); ?>
没有任何问题;如果未提供变量,则函数中的$message
变量将设置为空字符串
但是,使用:
<?php echo output_message($any_variable_name); ?>
当$any\u variable\u name
未在您所处的范围内定义时(在本例中为全局范围),将生成警告。在
之前缺少一个结束?>
?在哪里定义了output\u message()
函数?另外,我建议您为开发启用适当的错误报告。将其放在脚本的顶部<代码>ini设置(“显示错误”,“打开”);错误报告(E_全部)
您可能还需要初始化$message
变量,以避免出现未定义变量的警告(与未定义函数无关…。@phil它位于文档顶部包含在我的includes文件夹中的函数文件中。@Jeroen当我将函数构建为output\u message($message)时,我以为是在定义它=“”)我设置了默认值。当我在函数外部的文档顶部定义$message=”“时,一切正常-所以现在我的问题是为什么我的默认值在函数中不起作用。我通过输出_message($message=“”)设置了默认值。
<?php echo output_message($any_variable_name); ?>