Connecting Tech Pros Worldwide Forums | Help | Site Map

Variables Outside Functions

Angelos
Guest
 
Posts: n/a
#1: Jul 17 '05
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;
break;
}
}
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;
break;
}
}
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 ?



James@asdf.com
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Variables Outside Functions


On Thu, 2 Jun 2005 10:23:15 +0000 (UTC), "Angelos"
<angelos@redcatmedia.net> wrote:
[color=blue]
>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 ?[/color]

you can use the global keyword in your function.
i.e
global $variable1,$variable2;
This means that your function will refer to the global version.
more here:
http://uk.php.net/manual/en/language...bles.scope.php

Berislav Lopac
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Variables Outside Functions


Angelos wrote:[color=blue]
> ANd the question is : DO I have to pass all the variables that I use
> in the function when I call it ?[/color]

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.

Berislav


Perttu Pulkkinen
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Variables Outside Functions


"Angelos" <angelos@redcatmedia.net> kirjoitti viestissä
news:d7mmmg$ben$1@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...[color=blue]
> hello there...
> I would like to ask if there is any way of using variables which are[/color]
outside[color=blue]
> functions in the functions without passing them when I call the[/color]
function...[color=blue]
> Complicated eehh ?
> ANd the question is : DO I have to pass all the variables that I use in[/color]
the[color=blue]
> function when I call it ?
> I don't want to set the variables inside the functions because they are[/color]
used[color=blue]
> by other functions as well[/color]

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>";

}


Angelos
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Variables Outside Functions


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


John
Guest
 
Posts: n/a
#6: Jul 17 '05

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!

-
John
http://Talk-PHP.com

Oli Filth
Guest
 
Posts: n/a
#7: Jul 17 '05

re: Variables Outside Functions


John wrote:[color=blue]
> A small question, why isn't it good practice to use the global scope
> inside functions?[/color]

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",
e.g.

Main
|
------------------
| | |
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
structured.

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.

[color=blue]
> I would say it's not a good practice to pass by reference 10 variables
> when you only need to pass one![/color]

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.

--
Oli

Chung Leong
Guest
 
Posts: n/a
#8: Jul 17 '05

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.

Closed Thread