views:

1468

answers:

5

Is it good practice to initialize a global variable in PHP? The snippet of code seems to work fine, but is it better to initialize (in a larger project, say for performance sake) the variable outside of a function, like in the second scratch of code?

if(isset($_POST["Return"]))Validate();
function Validate(){
    (!empty($_POST["From"])&&!empty($_POST["Body"]))?Send_Email():Fuss();
};
function Send_Email(){
    global $Alert;
    $Alert="Lorem Ipsum";
    mail("","",$_POST["Body"],"From:".$_POST["From"]);
};
function Fuss(){
    global $Alert;
    $Alert="Dolor Sit"
};

function Alert(){
    global $Alert;
    if(!is_null($Alert))echo $Alert;
};

Notice the variable $Alert above is not initialized.

$Alert;
if(isset($_POST["Return"]))Validate();
function Validate(){
    (!empty($_POST["From"])&&!empty($_POST["Body"]))?Send_Email():Fuss();
};
function Send_Email(){
    global $Alert;
    $Alert="Lorem Ipsum";
    mail("","",$_POST["Body"],"From:".$_POST["From"]);
};
function Fuss(){
    global $Alert;
    $Alert="Dolor Sit"
};

function Alert(){
    global $Alert;
    if(!is_null($Alert))echo $Alert;
};

Now notice it is.

I appreciate any answers! Thanks in advance, Jay

A: 

Well, using a variable that has not been initialized will trigger a notice in php, so initializing variables is always better than not initializing them.

smoove666
First, thanks to everyone's fast reply!I don't get a notice no matter if the $Alert is there or not. So is the variable initialized when PHP sees it's first instance, global or otherwise (in the first snippet, ie: line 6)?
Jay
You're probably not seeing the notices because you haven't turned them on, you can either alter your php.ini to enable them, or use this command:error_reporting(E_ALL);The default value for error_reporting tends to vary with the different version of PHP (I think!). But it's best to have it set to E_ALL, as you'll be more aware of your programming mistakes.
Vex
As suggested I've appended the short scrap of code below what we've got already, even so I still see no errors! error_reporting(E_ALL); ini_set("display_errors",1);
Jay
you have to put it above all your code.
smoove666
also, error_reporting(E_ALL | E_STRICT) is the most verbose error level.
smoove666
Sure, I had tried strict too but I'm still not seeing any errors. Even after uploading to a remote server I see no errors!
Jay
put this in a new php file and run it: error_reporting(E_ALL | E_STRICT); ini_set("display_errors",1); $test; echo $test; die();
smoove666
That kicks up a fuss, but my code still executes without errors so PHP perhaps, at $Alert's first instance (global or otherwise) initialises the variable?
Jay
In your code $Alert never is uninitialized, unless you call Alert() before Fuss() or Send_Email()
smoove666
Alert() is called at each page load, Send_Email() or Fuss() are only call on postback, so if you don't postback the page, Send_Email() or Fuss() aren't called, therefore Alert() is called before Send_Email() or Fuss() and $Alert doesn't equal anything, so nothing is echoed. So should it execute or error?
Jay
+1  A: 

Is it good practice to initialize a global variable in PHP?

In my opinion (and I'm not the only one thinking that), it is good practice to not use global variables.

You can find a couple of arguments here.

If you really need to use global variables, though, it's probably better to initialize them ; or use isset to determine if they've been.

Pascal MARTIN
I need it to be global as Alert() is called each time the page loads, and if $Alert is null then no alert will be echoed in the page.
Jay
+2  A: 

In the second example you are still not declaring the variable, the line

$alert;

does not assign $alert a value so it remains undeclared.

If you declare the variable first, you can access it more easily without generating notices:

$alert = '';

if ($alert) {
    //do something with alert
}
Tom Haigh
+3  A: 

Do not use global variables, are a bad parctice and won't be available in PHP6. If you need values available across pages/classes, why don't you create a enumeration class? See an example here: http://riccardotacconi.blogspot.com/2009/05/enumerator-class-in-php.html

Basicaly you include your class and you get the value in this way: QYourClass::Alert

rtacconi
The value of a constant can't be set during runtime like a global by definition. Anyhow, thanks for your answer.
Jay
A: 

I don't think this is doable so I'm scrapping it. Global variables are being dropped in PHP6 and a Constant, by definition can't have it's value changed. Thanks to everyone, I appreciate each answer and all who contributed.

Jay
Yours was not the answer. You should give the credit to the one who helped you, if any. Otherwise, don't select yours as answer as I've already read it somewhere above.
Fabio Milheiro