Natalia
Natalia

Reputation: 417

using a local variable inside a global variable inside a function

So, I have this piece of text that I would like to come from elsewhere to inside a function. I put it in "configuration.php" and would like to use in a file "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: [email protected]");    
        }

It all works, the mail is sent, the confirmation message arrives, but $link in the email that is sent is blank (empty, non-defined?). So the local variable $link somehow does not get processed within the global variable $event_confirmation_message. Is there something I am doing wrong?

Upvotes: 1

Views: 46

Answers (2)

Jayesh Chandrapal
Jayesh Chandrapal

Reputation: 684

Do like this:

// 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: [email protected]");    
}

Upvotes: 2

Marc B
Marc B

Reputation: 360672

PHP cannot time travel.

$link in your configuration.php will be evaluated/replaced when configuration.php is parsed/loaded. Therefore your $event_confirmation_message will NOT contain a variable anymore when you use the variable elsewhere. It'll contain whatever text was in $link at the time $event... was defined.

This is very much like buying a cake at the store, and wondering why you can't find the egg/flour/milk/sugar that it's made of - all of that was "destroyed" in the bakery and you have just cake...

Upvotes: 1

Related Questions