2012-01-13 109 views
0

我有一個函數的任何一個默認值,例如:PHP函數沒有定義

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(); } ?> 

我不知道,因爲我的功能我在功能設置$消息的默認值爲什麼我收到此錯誤。 output_message($消息= 「」)。

有人可以看看我的代碼,並告訴我爲什麼它告訴我我的功能沒有定義。謝謝。

+3

在之前缺少一個關閉'?>'? output_message()函數在哪裏定義?另外,我建議你爲開發啓用體面的錯誤報告。將它放在腳本的頂部; 'ini_set('display_errors','On'); error_reporting(E_ALL);' – Phil 2012-01-13 01:11:19

+1

你也可能想要初始化'$ message'變量來避免未定義變量的警告(與未定義的函數無關......)。 – jeroen 2012-01-13 01:15:53

+0

@phil它位於文檔頂部包含的包含文件夾中的函數文件中。 – user982853 2012-01-13 01:32:03

回答

0

我真的不知道你在哪裏使用output_message,但我的第一個猜測是這些函數「redirect_to」可能是問題。

這些功能大部分時間做這樣的事情:

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

檢查是否已做出了正確的包含在正在使用的output_message功能的PHP文件。

此外,我會建議遵循建議菲爾和吉榮告訴爲註釋:

  • 遺漏>標記之前
  • 添加init_set( 「display_errors設置」, 「開」);像$消息使用error_reporting(E_ALL)
  • 初始化變量,以避免警告和副作用
  • 儘量不要混合使用邏輯渲染(這個人是我的)基於您的評論
1

,它似乎是不是沒有定義的函數,而是變量。

問題不在於函數本身,如果沒有變量提供給函數,那麼將變量$message設置爲空字符串。

問題是與你的函數調用:

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

在這裏,你在呼喚一個變量的函數,也稱爲$message,但完全無關,在你的函數變量$message。這個全局變量不存在,這就是爲什麼PHP給你一個警告。

你定義你的函數的方式,意味着你可以這樣調用它:

<?php echo output_message(); ?> 

沒有任何問題;如果未提供變量,則函數中的$message變量將設置爲空字符串。

但是,使用:當$any_variable_name沒有在你的範圍內定義(在這種情況下,全球範圍內)

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

會生成一個警告。