简体   繁体   中英

Using global vars within a function in PHP the way you do it in Javascript

I have a function that uses lots of global vars and arrays - eg

$a=1;
$b[0]='a';
$b[1]='b';
$c='Hello';

function foo() {
 echo "$a 
       $b[0] 
       $b[1] 
       $c";
}

I understand that as opposed to JS, you have to include the vars when you call the function:

function foo($a,$b,$c)

but since I'm using lots of different vars and arrays in the function, and since the main origin of most of the vars is the $_GET array (after using extract($_GET)), I'm not sure how to do this.

is there a way to make a PHP function behave like a JS function for that manner?

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 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:

Use an OOP structure for your web app.

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();

Pass variables into functions

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM