简体   繁体   English

PHP变量全局和静态

[英]Php variable global and static

I am new to PHP. 我是PHP新手。 I'm studying variables scopes. 我正在研究变量范围。

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function. 在函数外部声明的变量具有全局范围,并且只能在函数外部访问。

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function. 在函数内声明的变量具有局部范围,只能在该函数内访问。

The global keyword is used to access a global variable from within a function. global关键字用于从函数内部访问全局变量。

To do this, use the global keyword before the variables (inside the function) 为此,请在变量之前(在函数内部)使用global关键字

Normally, when a function is completed/executed, all of its variables are deleted. 通常,当一个函数完成/执行时,它的所有变量都会被删除。 However, sometimes we want a local variable NOT to be deleted. 但是,有时我们希望不删除局部变量。 We need it for a further job. 我们需要它来做进一步的工作。

I need to declare variable within function to be global so I can get access to it from outside the function and to be static at the same time so I can keep the value of the variable after execution of the function and use it again. 我需要将函数内的变量声明为全局变量,以便可以从函数外部对其进行访问,并且可以同时为静态变量,以便可以在函数执行后保留变量的值,然后再次使用它。 I tried 我试过了

global static $x; 

but it doesn't work. 但这不起作用。

I need to know if I'm thinking in wrong way case I'm new to PHP. 我需要知道我是否以错误的方式思考,如果我是PHP新手。

<?php 
$x = 5;
function myTest() {

   echo "x is equal to".$GLOBALS['x']."";
   $GLOBALS['x']++;

} 
myTest();
myText();

?>

it executes only the first myTest(). 它仅执行第一个myTest()。 and the second one display an error Fatal error: Uncaught Error: Call to undefined function myText() 第二个显示错误致命错误:未捕获的错误:调用未定义的函数myText()

just declare it in global scope then use $GLOBALS[] array or global keyword to use that variable in a function. 只需在全局范围内声明它,然后使用$GLOBALS[]数组或global关键字在函数中使用该变量即可。 And as they hold the value even after function execution you don't need static keyword as well. 并且由于它们即使在函数执行后仍保留值,因此您也不需要static关键字。

study $GLOBALS , Variable scope 研究$ GLOBALS可变范围

you can use static or global to keep the value: 您可以使用static或global来保留值:

function doStuff() {
  $x = null;

  if ($x === null) {
     $x = 'changed';
     echo "changed.";
  }
}
doStuff();
doStuff();

the result would be: changed.changed. 结果将是: changed.changed.

if you use: 如果您使用:

function doStuff() {
  static $x = null;

  if ($x === null) {
     $x = 'changed';
     echo "changed.";
  }
}
doStuff();
doStuff();

the result would be changed. 结果将被changed. because static keeps last value even if you call function in multi times also global have the same result because of it's definition so you can also use: 因为即使您多次调用函数,static也会保留最后一个值,而global也由于其定义而具有相同的结果,因此您还可以使用:

global $x;

in the function and result would be same: changed. 在功能和结果上是相同的:已changed.

You have typo problem in your code (second calling of your function): 您的代码中有错字问题(第二次调用函数):

function myTest() ....

Then you called it: 然后,您将其命名为:

myTeXt();

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

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