Variables Outside Functions

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

hello there...
I would like to ask if there is any way of using variables which are outside
functions in the functions without passing them when I call the function...
Complicated eehh ?

 I have that :
if (isset($_GET['category']) && isset($_GET['id'])) {
    switch ($_GET['category']) {
        case 'news':
            echo "<h4>".date('d M Y',strtotime($release_date))."</h4>";
            echo "<h3>".$content_title."</h3>";
            echo "</p>".$body;
else echo $body;
and I want to convert it into that Function :
function displayContent(){
    if (isset($_GET['category']) && isset($_GET['id'])) {
        switch ($_GET['category'])  {
            case 'news':
                echo "<h4>".date('d M Y',strtotime($release_date))."</h4>";
                echo "<h3>".$content_title."</h3>";
                echo "</p>".$body;
    else echo $body;
ANd the question is : DO I have to pass all the variables that I use in the
function when I call it ?
I don't want to set the variables inside the functions because they are used
by other functions as well

May I have your lights PLS ?

Re: Variables Outside Functions

On Thu, 2 Jun 2005 10:23:15 +0000 (UTC), "Angelos"

Quoted text here. Click to load it

you can use the global keyword in your function.
global $variable1,$variable2;
This means that your function will refer to the global version.
more here:

Re: Variables Outside Functions

Quoted text here. Click to load it

1) and you remember that GET is already superglobal, so no problem with that
2) MUCH better than "globalizing" individual vars from here and there is to
make one $CFG-variable or object that is collection of those vars that are
allowed to globalize from coder's point of view
3) of course i understand and appreciate those who say: don't use any global
in fuctions. But wise $CFG doesnt't create problems.

class o() {};
$CFG=new o();
$CFG->body ="DFSDFGSDF";
$CFG->head ="aertaertaer";
function some()
   global $CFG;

  $CFG->body = "<html>....</html>";


Re: Variables Outside Functions

Angelos wrote:
Quoted text here. Click to load it

As James said, you don't; just use the global keyword.

However, it is not a good practice; it's better to pass them by reference as
a parameter.


Re: Variables Outside Functions

Thanks to all ... That was really helpfull and it was what I wanted to hear
Take Care everybody !!!

Re: Variables Outside Functions

A small question, why isn't it good practice to use the global scope
inside functions?

I would say it's not a good practice to pass by reference 10 variables
when you only need to pass one!


Re: Variables Outside Functions

John wrote:
Quoted text here. Click to load it

It's a question of code maintainability.

Writing good, structured code involves trying to decouple (isolate)
functions and modules of your code as much as possible. Basically, this
means that you should attempt to write your code so that one small
change in a function should have minimal effect on any other function
elsewhere in your script/program. The more entangled and intertwined
your code is, the harder it will be to track down bugs caused by
changes propagating through your functions.

If you draw a block diagram of your functions, and then draw lines
between all of them that interact (i.e. share variables or call each
other), in an ideal world it should look like an upside-down "tree",

         |        |       |
        Func1    Func2   Func3
         |        |
         |   ----------
         |   |        |
         | Func4    Func5
    |         |
  Func6     Func7

This is hard to achieve in practice. However, if your diagram looks
more like spaghetti, this is a sign that your code is a mess and badly

Using global variables is one good way of ensuring that your functions
are unnecessarily coupled. Lots of functions all setting and getting
the same variables means that if you change the design of one function
later down the line, there's no guarantee that other bits won't screw
up unexpectedly, because they're not isolated from the changes.

Quoted text here. Click to load it

If you need to pass lots of variables to all your functions, it often
means that you haven't thought about your program's architecture
enough, i.e. that each function does one particular task or whatever.
(I know that is easier said than done! I can't think of an example off
the top of my head.)

Alternatively, if the variables that you're passing around are all
related in some way, you could try using a class to store them in, or
even making the functions members of the class.


Re: Variables Outside Functions

It is also an issue of reusability. If a function is dependent on the
presence of a global variable, it makes it trickier to reuse the
function in a different context.

Site Timeline