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

主宰稳场 提交于 2019-12-20 06:49:31

问题


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] 
       $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?


回答1:


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.




回答2:


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



回答3:


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.




回答4:


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




回答5:


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


来源:https://stackoverflow.com/questions/9215368/using-global-vars-within-a-function-in-php-the-way-you-do-it-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!