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
Solved

EASY: question on PHP's scope

Posted on 2007-03-26
3
225 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

837 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