简体   繁体   English

在PHP中的函数中使用全局变量,就像在Javascript中一样

[英]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: 我知道,与JS相反,调用函数时必须包含var:

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. 但是由于我在函数中使用了许多不同的var和数组,并且由于大多数var的主要来源是$ _GET数组(在使用extract($ _ GET)之后),所以我不确定如何执行此操作。

is there a way to make a PHP function behave like a JS function for that manner? 有没有办法使PHP函数的行为类似于JS函数?

You should use the "global" keyword, which tells the compiler to look for global variables. 您应该使用“ global”关键字,它告诉编译器寻找全局变量。

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

You can use the global keyword: 您可以使用global关键字:

$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. WordPress在其代码库中大量使用了这种方法,这使得调试非常棘手。 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. 为您的Web应用程序使用OOP结构。

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. 它还有助于正确组织变量,因此与用户有关的所有变量都可以在User类中。 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. 由于在PHP中不必在使用变量之前先声明变量,因此以下代码对于函数所引用的变量是模棱两可的。

$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. PHP假设函数范围内的所有变量都是局部变量,除非使用函数global关键字将它们声明为来自全局范围,否则PHP消除了歧义。

$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关键字访问global vars:

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: 否则,您可以使用预定义的$GLOBALS数组:

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 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 5.3中的上下文管理都不允许在不使用“ global”关键字的情况下访问全局变量。

Php.net has a post about the topic here . Php.net在此处有关于该主题的帖子。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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