Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Transfer variabele outside function
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";
}
?>




Report this thread to moderator Post Follow-up to this message
Old Post
Marco J.L.
04-14-05 01:55 AM


Re: Transfer variabele outside function
"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";
}

?>



Report this thread to moderator Post Follow-up to this message
Old Post
Kimmo Laine
04-14-05 01:55 AM


Re: Transfer variabele outside function
have you read any documentation whatsoever?
http://www.php.net/manual/en/language.functions.php

and don't crosspost!


Report this thread to moderator Post Follow-up to this message
Old Post
BKDotCom
04-14-05 01:55 AM


Re: Transfer variabele outside function
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


Report this thread to moderator Post Follow-up to this message
Old Post
Ken Robinson
04-14-05 01:55 AM


Re: Transfer variabele outside function
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;
}

Report this thread to moderator Post Follow-up to this message
Old Post
James Pittman
04-15-05 08:56 PM


Re: Transfer variabele outside function
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;
}

Report this thread to moderator Post Follow-up to this message
Old Post
James Pittman
04-18-05 08:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:06 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.