Jay
Jay

Reputation: 59

Initialize Global Variables in PHP

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

Upvotes: 2

Views: 14820

Answers (6)

Martin Q
Martin Q

Reputation: 11

According to this page global variables are not being abolished in PHP6, but rather register global variables. Global variables have a number of uses (some are good practice, some are not) and are basically secure. Register globals are a serious security loophole and have rightly been deprecated.

Upvotes: 0

Jay
Jay

Reputation: 59

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.

Upvotes: 0

rtacconi
rtacconi

Reputation: 14769

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

Upvotes: 1

Tom Haigh
Tom Haigh

Reputation: 57825

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
}

Upvotes: 4

Pascal MARTIN
Pascal MARTIN

Reputation: 401142

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.

Upvotes: 2

smoove
smoove

Reputation: 4000

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.

Upvotes: 1

Related Questions