By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,405 Members | 1,579 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,405 IT Pros & Developers. It's quick & easy.

Variables Outside Functions

P: n/a
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 ?
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Thu, 2 Jun 2005 10:23:15 +0000 (UTC), "Angelos"
<an*****@redcatmedia.net> wrote:
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 ?


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

Jul 17 '05 #2

P: n/a
Angelos wrote:
ANd the question is : DO I have to pass all the variables that I use
in the function when I call 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.

Berislav
Jul 17 '05 #3

P: n/a
"Angelos" <an*****@redcatmedia.net> kirjoitti viestissä
news:d7**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
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 ?
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


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

}
Jul 17 '05 #4

P: n/a
Ok...
Thanks to all ... That was really helpfull and it was what I wanted to hear
;-)
Take Care everybody !!!
Jul 17 '05 #5

P: n/a
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

Jul 17 '05 #6

P: n/a
John wrote:
A small question, why isn't it good practice to use the global scope
inside functions?
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.

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


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

Jul 17 '05 #7

P: n/a
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.

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.