1

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?

3
  • does configuration.php also have $link defined? Commented Jun 12, 2015 at 16:07
  • I can't put $link in configuration.php, as it gets composed with values that somefunction() creates. Commented Jun 12, 2015 at 16:12
  • if configuration gets loaded first, you'll get an undefined variable notice right off the bat. So, you'll need to figure out how that gets loaded. There's no way for your other file to pull in that configuration if $link hasn't been defined in there. Your question is unclear at this point as to how everything is loaded and supposed to tie in with each other. Commented Jun 12, 2015 at 16:16

2 Answers 2

2

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]");    
}
Sign up to request clarification or add additional context in comments.

3 Comments

cool, will try it out right now :). I already thought of that, but I'd like to use $link within $event_confirmation_message without breaking the message in two parts.
No, can't do in above sample as explained by @marc-b
Ok, I don't see any other options but to create $beginning_of_the_message (global), $link(local) and $end_of_the_message(global) variables and use that way.
1

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...

1 Comment

If only I could travel back in time to see what Marie-Antoinette looked like. I just don't want her to offer me any of her cake though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.