Solved

EASY: question on PHP's scope

Posted on 2007-03-26
3
226 Views
Last Modified: 2008-03-06
Greetings, I have the following problem:

-in ASP, I used to write all the initialization code for my web applications in a separate file; I would declare my variables in the "main" page, include the .asp file with the initialization function and then invoke the function; during this function, all the initialization variables would be initialized, and having declared them beforehand they would still be readable after the initialization function has returned.

It seems that in Php I cannot do this, as the variables which I initialize lose their value when the function returns, even if I have variables with the same name declared in the previous level of the scope.

How do I solve this problem? Is there a better way other than:

a) writing an initialization function for every variable, whose value is returned by the function itself;
b) copy-pasting the initialization code in the same page where I use the variables.

Many thanks.
0
Comment
Question by:Emanuele_Ciriachi
  • 2
3 Comments
 
LVL 24

Accepted Solution

by:
glcummins earned 250 total points
ID: 18793570
You can use the 'global' keyword in your functions:

function myFunction()
{
   global $var; // This is a variable that was initialized in the master script

   $var = 'value;  // Now you are changing the value of $var for the entire script.
}
0
 
LVL 11

Expert Comment

by:JamesCssl
ID: 18793574
use the keyword global before the variable name:

global $var1, $var2;
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18793577
More information can be found here: http://www.php.net/global
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question