For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > April 2005 > Transfer variabele outside function









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Transfer variabele outside function
Marco J.L.

2005-04-13, 8:55 pm

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";
}
?>



Kimmo Laine

2005-04-13, 8:55 pm

"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";
}

?>


BKDotCom

2005-04-13, 8:55 pm

have you read any documentation whatsoever?
http://www.php.net/manual/en/language.functions.php

and don't crosspost!

Ken Robinson

2005-04-13, 8:55 pm

Marco 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

James Pittman

2005-04-15, 3:56 pm

Marco 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;
}
James Pittman

2005-04-18, 3:56 am

Marco 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;
}
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com