Home  >  Article  >  Backend Development  >  What is the difference between js global variables and php global variables?

What is the difference between js global variables and php global variables?

WBOY
WBOYOriginal
2022-02-21 14:54:041996browse

The difference between js global variables and php global variables: PHP needs to use the global keyword when calling global variables in a function. This keyword is used to import global variables, but it is not required when calling global variables in js. Take advantage of keywords.

What is the difference between js global variables and php global variables?

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.

What is the difference between js global variables and php global variables

PHP global keyword

global keyword is used to access global variables within a function.

To do this, use the global keyword in front of the (internal function) variable:

Example

<?php
$x=5;
$y=10;
function myTest() {
  global $x,$y;
  $y=$x+$y;
}
myTest();
echo $y; // 输出 15
?>

jsglobal variable

In JS Declaring global variables is mainly divided into explicit declaration or implicit declaration, which are introduced below.

Declaration method one:

Use var (keyword) The variable name (identifier) ​​is declared outside the function, which is a global variable, otherwise it is a local variable declared in the function. This method is an explicit declaration. The details are as follows:

var test = 5;  //全局变量
function a()
{
  var cc=3; //局部变量
  alert(test);
}
function b(){alert(test);}

Declaration method two:

Without using var, directly assign a value to the identifier test, which will implicitly declare the global variable test. Even if the statement is within a function, test becomes a global variable when the function is executed.

test = 5;//全局变量 
function a()
{
  aa=3; //全局变量
  alert(test);
}

Advantages of js global variables:

can reduce the number of variables and reduce the time consumption caused by data transfer of actual parameters and formal parameters.

Disadvantages of js global variables:

(1) Global variables are stored in static storage areas. Memory is allocated for them when the program starts running, and the memory is released when the program ends. Compared with the dynamic allocation and dynamic release of local variables, the lifetime is relatively long, so too many global variables will occupy more memory units.

(2) Global variables destroy the encapsulation performance of functions. A function is like a black box, which generally performs input and output through function parameters and return values. The internal implementation of the function is relatively independent. However, if global variables are used in the function, then the statements in the function body can bypass the function parameters and return values ​​for access. This situation destroys the independence of the function and makes the function dependent on global variables. At the same time, it also reduces the portability of this function.

(3) Global variables make the code of the function less readable. Since multiple functions may use global variables, the value of the global variable may change at any time when the function is executed, which is very detrimental to program error checking and debugging.

Therefore, it is best not to use global variables unless it is absolutely necessary.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the difference between js global variables and php global variables?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn