What is the difference between declaring a variable inside a function like globalor public/privateVS, passing it to the function as an argument?
Another related confusion
I recently caused a big headache, trying to pass the aa array variable to a function as globalwell as edit it inside and hope to return it, and it took me several hours to realize that I needed to pass it to the function as an argument by reference, for examplefunctionCall(&$arrayVar);
Secondary question: But I'm still wondering why it doesn’t work to pass the variable as a quality global, then edit it and return it with returnor just by doing something like concatenation into an array of variables?
Another example that I recently encountered is creating a function for PHPMailer, where I pass it a few arguments, such as an email address and message body, but I also need to pass authentication strings, such as an API key, etc. Here, every time I call him:
- I don’t want to give him authentication credentials every time I call the PHMailer function (for example, send an email error message in one of several stages)
- But I want to give him unique arguments every time I call him
, , :
function phpMailer( $mail_to = "to@email.com", $mail_from = "a@b.com" ) {
global $authCredentials;
}
phpMailer("that.guy@there.com", "me@here.com");
: global , ?