Code Comments
Programming Forum and web based access to our favorite programming groups.Hello,
how can I transfer a variable from a function to the part after the
function?
thnx,
Marco J.L.
<?php
$var="before function";
func_xxxxx();
echo $var;
Function func_xxxxx() {
$var = "after function";
}
?>
Post Follow-up to this message"Marco J.L." <luyendyk@yahoo.news.news> kirjoitti
viestissä:d3jpkc$7jk$1@reader08.wxs.nl...
> Hello,
>
> how can I transfer a variable from a function to the part after the
> function?
>
> thnx,
>
> Marco J.L.
>
>
> <?php
>
> $var="before function";
> func_xxxxx();
> echo $var;
>
> Function func_xxxxx() {
> $var = "after function";
> }
> ?>
<?php
$var="before function";
func_xxxxx();
echo $var;
function func_xxxxx() {
global $var;
$var = "after function";
}
?>
or
<?php
$var="before function";
$var = func_xxxxx();
echo $var;
function func_xxxxx() {
return "after function";
}
?>
Post Follow-up to this messagehave you read any documentation whatsoever? http://www.php.net/manual/en/language.functions.php and don't crosspost!
Post Follow-up to this messageMarco J.L. wrote:
> Hello,
>
> how can I transfer a variable from a function to the part after the
> function?
>
> thnx,
>
> Marco J.L.
>
>
> <?php
>
> $var="before function";
> func_xxxxx();
> echo $var;
>
> Function func_xxxxx() {
> $var = "after function";
> }
> ?>
Use the global statement.
<?php
$var = 'before function';
func_xxxxx();
echo $var;
function func_xxxxx() {
global $var;
$var = 'after function';
}
?>
Ken
Post Follow-up to this messageMarco J.L. wrote:
> Hello,
>
> how can I transfer a variable from a function to the part after the
> function?
>
> thnx,
>
> Marco J.L.
>
>
> <?php
>
> $var="before function";
> func_xxxxx();
> echo $var;
>
> Function func_xxxxx() {
> $var = "after function";
> }
> ?>
>
>
>
I believe you can do this if you pass the variable by reference:
$var = "before function";
myFunc($var);
echo $var;
function myFunc(\$foo) {
$foo = str_replace("before", "after", $foo);
}
But a more elegant way is to return the variable back to the program:
$var = "before function";
$var = myFunc($var);
echo $var;
function myFunc($foo) {
// Do some manipulation on $foo and set it to $bar
$bar = str_replace("before", "after", $foo);
return $bar;
}
Post Follow-up to this messageMarco J.L. wrote:
> Hello,
>
> how can I transfer a variable from a function to the part after the
> function?
>
> thnx,
>
> Marco J.L.
>
>
> <?php
>
> $var="before function";
> func_xxxxx();
> echo $var;
>
> Function func_xxxxx() {
> $var = "after function";
> }
> ?>
>
>
>
I believe you can do this if you pass the variable by reference:
$var = "before function";
myFunc($var);
echo $var;
function myFunc(\$foo) {
$foo = str_replace("before", "after", $foo);
}
But a more elegant way is to return the variable back to the program:
$var = "before function";
$var = myFunc($var);
echo $var;
function myFunc($foo) {
// Do some manipulation on $foo and set it to $bar
$bar = str_replace("before", "after", $foo);
return $bar;
}
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.