在函数内部的全局变量中使用局部变量

时间:2015-06-12 16:04:35

标签: php

所以,我有这篇文章,我想从其他地方来到一个函数内部。我把它放在“configuration.php”中,并希望在“functions.php”文件中使用

configuration.php

$event_confirmation_message = "Your awesome submission has been approved -  $link. ";

的functions.php

somefunction(several arguments go here){
//code that does other stuff with the function arguments
//then we need to send the confirmation message
            global $event_confirmation_message; //to change, see configuration.php 
            $link = "http://www." . $city . "events.info/index.php?option=events&main_id=" . $row['main_id'];
            mail($email, "Please check your your submission", $event_confirmation_message, "From: contact@me.info");    
        }

一切正常,邮件发送,确认邮件到达,但发送的电子邮件中的$ link是空白的(空的,未定义的?)。因此局部变量$link在某种程度上不会在全局变量$event_confirmation_message内得到处理。有什么我做错了吗?

2 个答案:

答案 0 :(得分:2)

这样做:

// configuration.php
$event_confirmation_message = "Your awesome submission has been approved - ";

//functions.php
somefunction(several arguments go here){
    global $event_confirmation_message;
    $link = "http://www." . $city . "events.info/index.php?option=events&main_id=" . $row['main_id'];
    $msg = $event_confirmation_message . $link;
    mail($email, "Please check your your submission", $msg, "From: contact@me.info");    
}

答案 1 :(得分:1)

PHP无法计时旅行。

解析/加载configuration.php时,将评估/替换$link中的

configuration.php。因此,当您在其他地方使用变量时,$event_confirmation_message将不再包含变量。它将包含$link定义时$event...中的任何文字。

这非常像在商店买蛋糕,并想知道为什么你找不到它所制作的鸡蛋/面粉/牛奶/糖 - 所有这些都在面包店被“摧毁”而你只是蛋糕...

相关问题