I have a function that uses lots of global vars and arrays - e.g.
$a=1;
$b[0]=\'a\';
$b[1]=\'b\';
$c=\'Hello\';
function foo() {
echo \"$a
$b[0]
You should use the "global" keyword, which tells the compiler to look for global variables.
function foo() {
**global $a, $b, $c;**
echo "$a
$b[0]
$b[1]
$c";
}
You can access global vars with global
keyword:
global $a=1;
global $b[0]='a';
global $b[1]='b';
global $c='Hello';
function foo() {
global $a, $b, $c;
echo "$a
$b[0]
$b[1]
$c";
}
Otherwise you can use predefined $GLOBALS
array:
function foo() {
echo "$GLOBALS['a']
$GLOBALS['b'][0]
$GLOBALS['b'][1]
$GLOBALS[c]";
}
Here you have more info:
http://php.net/manual/en/language.variables.scope.php
Since in PHP you do not have to declare variables before they are used, the following code is ambiguous as to what variable the function is referring to.
$a = 'ten';
function foo($a) {
echo($a);
}
foo(10); // Outputs: 10
PHP removes the ambiguity by assuming that all variables in the scope of a function are local, unless they are declared in the function to come from the global scope by using the global
keyword.
$a = 10;
function foo($b) {
global $a;
echo($a);
}
foo('ten'); // Outputs: 10
In general the use of global variables is discouraged since it introduces tight-coupling between your objects, decreases the readability and locality of code, pollutes the global namespace and increases the mental-load on developers having to remember how many/what global variables they have.
When ever you have a function, the context management in PHP 5.3 doesn't allow access to global variables without the use of the "global" keyword.
Php.net has a post about the topic here.
function foo() {
global $a, $b, $c;
echo "$a
$b[0]
$b[1]
$c";
}
You can use the global keyword:
$a = "Hello World";
$b = "Hello World";
function outputStrings(){
global $a, $b;
echo $a." - ".$b;
}
$b = "Goodbye World";
outputStrings(); // ouputs "Hello World - Goodbye World"
However, its best not to use this structure. Its generally confusing and will make your code difficult to maintain. Wordpress uses this approach a lot in their code base and it makes for very tricky debugging. Other plugins and code can interject and modify global variables, changing the output of your script.
What would be better would be to either:
This way you can use objects instead of just random global variables. This gets around the issue of you accidentally overwriting a global variable in the course of a script. It also helps to organise the variables correctly, so all variables concerning users can be in the User
class. It makes more sense to structure it like that.
class User {
private $firstName;
private $secondName;
private $gender;
public function __construct($fname, $sname, $gend){
$this->firstName = $fname;
$this->secondName = $sname;
$this->gender = $gend;
}
public function outputDetails(){
echo $this->firstName." ".$this->secondName." is ".$this->gender;
}
}
$user = new User("Thomas", "Clayson", "Male");
$user->outputDetails();
Just like you've shown in your example. This is the generally accepted standard way of doing this. You should always pass in variables like this, it helps you define scopes and a proper structure. Also it means you know what the value of variables is, as you own them and pass them to functions, rather than just plucking variables from global scope.