Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在另一个自定义函数中声明自定义函数_Php_Function_Declare - Fatal编程技术网

Php 在另一个自定义函数中声明自定义函数

Php 在另一个自定义函数中声明自定义函数,php,function,declare,Php,Function,Declare,我的第一项职能: 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 com

我的第一项职能:

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;
    }
}
第二功能:

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()可以吗?如果无法设置db连接,我还会收到电子邮件吗?(假设服务器的邮件函数一直工作)

如果在db\u connect\u error\u mail()之前调用die,则不会,因为函数停止执行任何进一步的代码,所以不会收到电子邮件


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

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