Initialisieren Sie Globale Variablen in PHP

Ist es gute Praxis, initialisiert eine Globale variable in PHP? Das code-snippet scheint zu funktionieren, aber ist es besser, sich zu initialisieren (in einem größeren Projekt, sagen: für Leistung sake) die variable außerhalb der Funktion, wie in der zweiten scratch-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;
};

Beachten Sie die variable $Alarm oben ist nicht initialisiert.

$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;
};

Nun feststellen, es ist.

Ich freue mich über jede Antworten! Vielen Dank im Voraus, Jay

InformationsquelleAutor Jay | 2009-07-23

Schreibe einen Kommentar