RE: [PHP] passing new variables back from a function
| From: | Marc Burgauer | Date: | Thu, 07 Sep 2000 14:25:36 +0000 |
| Subject: | RE: [PHP] passing new variables back from a function | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-15716@lists.php.net to get a copy of this message | ||
You have a problem with the variable scope. The variables you assign within
your function are not "known" outside of it and so you can't reference them.
Two ways to solve the problem:
1.) Make the variables global:
global $name, $valuecode;
function explode_var($varname)
{
global $name, $valuecode;
$vararray = explode(":", $varname);
$name = $vararray['0'];
$valuecode = $vararray['1'];
}
explode_var($details);
echo "name = $name";
2.) Return the values explicitly from your function (I prefer to do this).
The problem here is that you can only return one variable, but you want two
values, so it will be an array variable:
function explode_var($varname)
{
$vararray = explode(":", $varname);
$result["name"] = $vararray['0'];
$result["value"] = $vararray['1'];
return $result;
}
$splited = explode_var($details);
echo "name = " . $splited["name"];
HTH
Marc
> -----Original Message-----
> From: Matt Williams [SMTP:matt@yewlands.com]
> Sent: Thursday, September 07, 2000 3:08 PM
> To: php-general@lists.php.net
> Subject: [PHP] passing new variables back from a function
>
> Hi
>
> I am trying to write a function that will split a string a return the
> values
> as new variables
> Here's what I've got
>
> function explode_var($varname)
> {
> $vararray = explode(":", $varname);
> $name = $vararray['0'];
> $valuecode = $vararray['1'];
>
> }
>
> explode_var($details);
> echo "name = $name";
>
> But it does not output anything. If I put the echo inside the function it
> outputs correctly but I want to pass the variable back to the script so /i
> can do other stuff to it.
>
> Many thanks for any help
>
> M@
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>