What are the types of scopes in PHP? What area can be accessed by the scope?

慕斯
Release: 2023-03-10 16:28:01
Original
3416 people have browsed it

The previous article introduced you to "What is the difference between cookie and session in PHP? 》, this article continues to introduce to you what are the types of scopes in PHP? What area can be accessed by the scope? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What are the types of scopes in PHP? What area can be accessed by the scope?

#Scope: The area that a variable (constant) can access.

1. The volume can be defined in ordinary code.

2. Variables can also be defined inside the function.

Strictly speaking, functions in PHP are divided into two types: However, PHP also defines some internal ones that are outside the strict sense, so there are three types in total:.

1. Global volume: It is the volume generally defined by the user (the number of groups is defined externally).

Belongs to the global space: In PHP, it is only allowed to be used in the global space: theoretically, it cannot be used inside the function.

Script cycle: until the end of the script (the last line of code is executed).

2. Local intersection: It is the variable defined inside the return number.

The current warp number space it belongs to: in PHP, it is only allowed to be used within the current warp number itself.

Function cycle: Function execution ends (the function opens an independent memory space in the stack area to run).

3. Super global variables: system-defined variables (predefined variables: s. SERVER, s. POST, etc.).

The super-global space it belongs to: no access restrictions (can be accessed both inside and outside the function).

Specifically, we take the code as an example:

In PHP scope, the default code space is the global space. We use global to define global variables. What we define Variables will be included in superglobal variables by the system. Local variables are defined inside the function. First, the function function is defined (all formal parameters can be understood as local variables), and then inner is output. Secondly, we access the global variable. ;

<?php
//PHP中作用域
//默认的代码空间:全局空间
$global = &#39;global area&#39; ;
//局部变量(函数内部定义)=global area;
function display(){
//所有的形参都可以理解为局部变量
$inner = __FUNCTION__ ;
//访问全局变量
echo $global ;
}
//调用函数
display();
Copy after login

The code results are as follows:

What are the types of scopes in PHP? What area can be accessed by the scope?

We see that the result shows that there is no global variable, which means that we access the global system and report an error when calling the function, but In fact, we have already defined it, indicating that global variables cannot be used in local space.

Recommended learning: php video tutorial

The above is the detailed content of What are the types of scopes in PHP? What area can be accessed by the scope?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!