Solved

EASY: question on PHP's scope

Posted on 2007-03-26
3
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Web Development 6 71
How to close my php post page. 3 42
Php variable to be sent back 3 35
Tell me if I understand this correctly... 6 21
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 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