473,770 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1584
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("be fore", "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("be fore", "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.wx s.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.andyhsoftwa re.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.w xs.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.c om...
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
1956
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
2559
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 in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
3
9083
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 problem i faced while using Response.Redirect is that the page that is directed to, does not looks as desired..the textboxes are not visible anymore and so as
4
1266
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
4614
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 Server.Transfer always throws the ThreadAbortException. MSDN acknowledges that this is a unque exception that will be automatically rethrown - i.e. it can't be swallowed. Does anyone know if there is extar code I can write (maybe something in the...
1
1613
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 page that lists the student's guests. At the bottom of the datagrid is an 'add a new guest' button. Users click on this button an it takes them to a new page where they can add a new guest. So they type in the guest information and click on the...
7
3634
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 from DWORD data;
7
4136
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 the graph image to his hard drive, just like right-clicking on the image and selecting 'save image as...' There's a link next to the graph that should open the save image dialog. The link calls the following php script:
1
2041
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
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.