Hi
I am trying to write an email function and ive hit a snag
I need to assign 2 variables in the function with the content of an html email and text email.
Is there anyway of declairing a variiable within a function for use outside of it in the code below?
How do i assign a variable in a function for use outside of
Moderator: General Moderators
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: How do i assign a variable in a function for use outside of
Code: Select all
function assignVar() {
return 5;
}
$result = assignVar();
// $result is now available outside the function and contains the value 5
Code: Select all
function assignVar() {
return array(5,10);
}
list($result1,$result2) = assignVar();
// $result1 and $result2 are now available outside the function and contains the values 5 and 10 respectively
Code: Select all
$result1 = $result2 = null;
function assignVar(&$result1,&$result2) {
$result1 = 5;
$result2 = 10;
}
assignVar($result1,$result2);
// $result1 and $result2 are now available outside the function and contains the values 5 and 10 respectively
Code: Select all
$result1 = $result2 = null;
function assignVar() {
global $result1, $result2;
$result1 = 5;
$result2 = 10;
}
assignVar();
// $result1 and $result2 are now available outside the function and contains the values 5 and 10 respectively
Re: How do i assign a variable in a function for use outside of
+1 for the examples 
If you're not able to return an array (or an object), I personally prefer passing by reference...since it gives more control as to what the variables are called etc...and if you need to call the function multiple times, you can have it assign the value(s) to different variables.

If you're not able to return an array (or an object), I personally prefer passing by reference...since it gives more control as to what the variables are called etc...and if you need to call the function multiple times, you can have it assign the value(s) to different variables.