473,231 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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

Jul 17 '05 #1
7 1554
have you read any documentation whatsoever?
http://www.php.net/manual/en/language.functions.php

and don't crosspost!

Jul 17 '05 #2
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

Jul 17 '05 #3

"Marco J.L." <lu******@yahoo.news.news> wrote in message
news:d3**********@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";
}
?>


Not doable. PHP doesn't expose API functions through which you can
manipulate the variable space of the parent scope.
Jul 17 '05 #4
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;
}
Jul 17 '05 #5
On Thu, 14 Apr 2005 20:52:58 -0400, "Chung Leong" <ch***********@hotmail.com>
wrote:
"Marco J.L." <lu******@yahoo.news.news> wrote in message
news:d3**********@reader08.wxs.nl...
how can I transfer a variable from a function to the part after the
function?
<?php

$var="before function";
func_xxxxx();
echo $var;

Function func_xxxxx() {
$var = "after function";
}
?>


Not doable. PHP doesn't expose API functions through which you can
manipulate the variable space of the parent scope.


What's the "global" statment, if not exactly this? "Parent scope" could mean
various things, but the OP posted a function call from the top-level scope -
this means he can use global to change that variable.

<?php
$var="before function";
func_xxxxx();
echo $var;

function func_xxxxx() {
global $var
$var = "after function";
}
?>

If the "parent scope" were another function then yes, you'd be out of luck;
you should pass a reference to the function to the inner function (you probably
should anyway even when coming from the global scope...).

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #6
In article <3n********************************@4ax.com>,
Andy Hassall wrote:
On Thu, 14 Apr 2005 20:52:58 -0400, "Chung Leong" <ch***********@hotmail.com>
wrote:
"Marco J.L." <lu******@yahoo.news.news> wrote in message
news:d3**********@reader08.wxs.nl...
how can I transfer a variable from a function to the part after the
function?
<?php

$var="before function";
func_xxxxx();
echo $var;

Function func_xxxxx() {
$var = "after function";
}
?>


Not doable. PHP doesn't expose API functions through which you can
manipulate the variable space of the parent scope.


What's the "global" statment, if not exactly this? "Parent scope" could mean
various things, but the OP posted a function call from the top-level scope -
this means he can use global to change that variable.

<?php
$var="before function";
func_xxxxx();
echo $var;

function func_xxxxx() {
global $var
$var = "after function";
}
?>

If the "parent scope" were another function then yes, you'd be out of luck;
you should pass a reference to the function to the inner function (you probably
should anyway even when coming from the global scope...).


How about this?

$var = 'before';
$var = after($var);
echo $var;

function after($s) {
return 'after';
}
Jul 17 '05 #7
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:3n********************************@4ax.com...
What's the "global" statment, if not exactly this? "Parent scope" could mean various things, but the OP posted a function call from the top-level scope - this means he can use global to change that variable.


I wasn't terribly clear in my post. What I meant was manipulating variables
in the parent scope irregardless of whether it's global or not. Basically,
what extract() and compact() are able to do.
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: InSeCo | last post by:
Hello, How can you delete spaces within a variabele (php4)? $var = "This is an example variabele." To : $var = "Thisisanexamplevariabele."
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
3
by: Justin | last post by:
Hi, Im confused here over the usage of Response.Redirect and Server.Transfer. I used frameset for my work, what are the proper usages of the two methods that seems working similar.. The...
4
by: Gopal Prabhakaran | last post by:
Dear All, Error : Invalid path for child request 'http:///localhost//childApplication//reciveControls.aspx'. A virtual path is expected. Is this statement is valid ?
9
by: Mark | last post by:
Hello I'm trying to use a Server.Transfer in a try-catch (I cannot put it outside the Try-Catch as it is nested deep within a component that is called in a try-catch loop) The problem is that the...
1
by: Jim Bayers | last post by:
This has been driving me crazy. server.transfer doesn't set the focus to the page I transfer to so when the user does a refresh, the results aren't what's expected. I have a datagrid on one...
7
by: monkeydragon | last post by:
how to would you transfer a DWORD variable from inside of a function to the caller ex. InvokeProcessData(LPDWORD prtDW) { ... // we have created, initialized and processed // dword variable...
7
by: shadowman | last post by:
Hello all: I have a web app that creates an image of a graph (as a png), based on user input of a combination of drop-down box items. I'm trying to add a function that allows the user to save...
1
by: susan | last post by:
Hoi, In een formulier roep ik een module aan. Hoe kan ik de waarde van een variabele uit de module terughalen naar mijn formulier? Dank jullie, Susan
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.