【问题标题】:Declaration of a custom function in another custom function在另一个自定义函数中声明自定义函数
【发布时间】:2013-02-01 07:48:33
【问题描述】:

我的第一个功能:

function db_connect_error_mail ($txt) {
mail(admin_email,mail_subject01,$txt);} //1st three variables come from constants at config.php

第二个功能:

function connectdb() {
  $dbc = mysqli_connect (db_host, db_user, db_password, db_name); //variables come from constants at config.php
  if (!$dbc) 
{
$txt = mysqli_connect_errno().mysqli_connect_error();
db_connect_error_mail($txt);
unset ($txt);
die('custom error message to inform viewer');
} else 
    {
        return $dbc;
    }
}

我的问题:

在 index.php 中调用 connectdb() 是否可行?如果无法设置数据库连接,我还会收到电子邮件吗? (假设服务器的邮件功能一直有效)

【问题讨论】:

  • 猜猜,是的,只要你在index.php 中包含config.php。 :)
  • @Praveen Kumar:是的,我包括了 config.php。
  • 答案是 42(不,您不会收到电子邮件,因为您使用了die - 这会停止执行您的脚本)。
  • 你不会收到邮件,只要你调用 die(),脚本就会停止。将 die() 放在 unset('txt) 之后;
  • 谢谢,我会将 die() 放在 unset('txt) 之后。还有其他不足吗?

标签: php function declare


【解决方案1】:

如果您在 db_connect_error_mail() 之前调用 die 那么不,您将不会收到您的电子邮件,因为die() 函数会停止执行任何进一步的代码。

但是,您可以在发送错误电子邮件后调用 die 函数。这样您将同时获得:通知邮件和错误消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 2013-07-05
    • 2012-10-28
    • 2013-03-13
    • 2020-10-07
    相关资源
    最近更新 更多