Table of Contents
[Basics] PHP variables and variable scope, php variable scope
Home Backend Development PHP Tutorial [Basics] PHP variables and variable scope, php variable scope_PHP tutorial

[Basics] PHP variables and variable scope, php variable scope_PHP tutorial

Jul 12, 2016 am 08:52 AM
local variables

[Basics] PHP variables and variable scope, php variable scope

Newly learned PHP, interesting syntax, record it.

1. Scope of variables

The scope is only divided into two parts: Global and Local. Global is relative to the entire .php file, and Local is the local minimum scope, which is the closest scope to the variable, such as: in a function, in a class, etc.

2. Declaration of variables

It may be due to the unique $ symbol of PHP variables, so no keywords are required to declare PHP variables (except in classes), and they are automatically created the first time they are assigned.

The class is quite unique. Since the class has member attributes private, public, and protected, keyword modification is required when declaring variables in the class. Use the keyword mentioned earlier or the keyword var, but not both.

3. Sample description

<span> 1</span> <?<span>php
</span><span> 2</span>     
<span> 3</span>     <span>$VarFile</span> = "Var_File";   <span>//</span><span>变量声明</span>
<span> 4</span>     
<span> 5</span>     <span>$nr</span> = <span>array</span>("\n","\r","\n\r","\r\n"<span>);
</span><span> 6</span>     
<span> 7</span>     
<span> 8</span> <span>class</span><span> TestClass
</span><span> 9</span> <span>{    
</span><span>10</span>     <span>/*</span>
<span>11</span> <span>    *    分别用Var声明和private关键字声明
</span><span>12</span> <span>    *   $var = value; 这种不可以
</span><span>13</span> <span>    *    var private $var; 这种不可以
</span><span>14</span> <span>    *    var private $var = value; 这种不可以
</span><span>15</span> <span>    *    var $var; 这种不可以    
</span><span>16</span>     <span>*/</span>
<span>17</span>     
<span>18</span>     <span>var</span> <span>$VarClass</span> = 'VarClass declared by keyword Var'<span>;            
</span><span>19</span>     <span>private</span> <span>$VarClass2</span> = 'VarClass2 declared by keyword private'<span>;
</span><span>20</span>     
<span>21</span>     <span>/*</span>
<span>22</span> <span>    *    增加global关键字,使用global作用域的变量
</span><span>23</span> <span>    *   函数中声明变量方法与全局一样
</span><span>24</span>     <span>*/</span>
<span>25</span>     
<span>26</span>     <span>function</span><span> GetAllVar() {
</span><span>27</span>         <span>global</span> <span>$VarFile</span><span>;
</span><span>28</span>         <span>$VarFunction</span> = 'Var_Function'<span>;
</span><span>29</span>         
<span>30</span>         <span>$Temp</span> = "<span>$VarFile</span>=".<span>$VarFile</span>."\r\n".
<span>31</span>                 "VarClass=".<span>$this</span>->VarClass."\r\n".
<span>32</span>                 "VarClass2=".<span>$this</span>->VarClass2."\r\n".
<span>33</span>                 "VarFunction=".<span>$VarFunction</span>."\r\n"<span>;
</span><span>34</span>         
<span>35</span>         
<span>36</span>         <span>return</span> <span>str_replace</span>(<span>$GLOBALS</span>['nr'],"<br/>",<span>$Temp</span><span>);
</span><span>37</span> <span>    }
</span><span>38</span>     
<span>39</span>     
<span>40</span> 
<span>41</span> <span>}
</span><span>42</span>     
<span>43</span>     <span>$MyClass</span> = <span>new</span> TestClass("zzy"<span>);
</span><span>44</span>     <span>echo</span> <span>$MyClass</span>-><span>GetAllVar();
</span><span>45</span>     
<span>46</span> ?>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1126174.htmlTechArticle[Basics] PHP variables and variable scope, php variable scope, new to PHP, interesting syntax, records Down. 1. The scope of variables is divided into two parts: Global and Local. Global is...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1252
24
What is the difference between local variables and global variables of a C++ function? What is the difference between local variables and global variables of a C++ function? Apr 19, 2024 pm 03:42 PM

The difference between C++ local variables and global variables: Visibility: Local variables are limited to the defining function, while global variables are visible throughout the program. Memory allocation: local variables are allocated on the stack, while global variables are allocated in the global data area. Scope: Local variables are within a function, while global variables are throughout the program. Initialization: Local variables are initialized when a function is called, while global variables are initialized when the program starts. Recreation: Local variables are recreated on every function call, while global variables are created only when the program starts.

C++ syntax error: When a function returns a pointer or reference, it cannot return a local variable or temporary object. What should I do? C++ syntax error: When a function returns a pointer or reference, it cannot return a local variable or temporary object. What should I do? Aug 22, 2023 am 09:22 AM

C++ is an object-oriented programming language, and its flexibility and power often provide programmers with great help. However, precisely because of its flexibility, it is difficult to avoid various small errors when programming. One of the most common mistakes is that when a function returns a pointer or reference, it cannot return a local variable or temporary object. So how to deal with this problem? This article will introduce the relevant content in detail. The cause of the problem is that in the C++ language, local variables and temporary objects are dynamically allocated during the running of the function. When the function ends, these local variables and temporary

Data competition analysis of global variables and local variables of Golang functions Data competition analysis of global variables and local variables of Golang functions May 21, 2023 am 08:19 AM

Golang is a strongly typed programming language with features such as efficiency, simplicity, and concurrency, so it is gradually favored by more and more developers. In the development of Golang, the global variables and local variables of functions often involve data competition issues. This article will analyze the data competition problem of global variables and local variables in Golang functions from the perspective of actual coding. 1. Data competition for global variables Golang global variables can be accessed in all functions, so if rigorous design and coding are not carried out

Local variable type inference in Java 10: How to use var keyword in foreach loop Local variable type inference in Java 10: How to use var keyword in foreach loop Jul 29, 2023 pm 03:21 PM

Local variable type inference in Java10: How to use var keyword in foreach loop Introduction: Java10 is an important version after Java9, which introduces many new features and improvements. One of the highly anticipated features is local variable type inference. In Java10, we can use the var keyword to declare local variables and let the compiler automatically infer the variable type based on the expression on the right. In this article, we will explore how to use

Local variable type inference in Java 10: How to use var keyword in lambda expression Local variable type inference in Java 10: How to use var keyword in lambda expression Aug 02, 2023 pm 04:25 PM

Local variable type inference in Java10: How to use the var keyword in lambda expressions Introduction: Java10 introduces a new feature of local variable type inference, which allows us to use the var keyword to infer the type of a local variable when declaring it. While this feature may not be necessary in most cases, in some cases it can improve code readability and simplicity. This article will focus on how to use the var keyword in lambda expressions to implement local variable type inference.

Local variable type inference in Java 10: How to simplify your code using var keyword Local variable type inference in Java 10: How to simplify your code using var keyword Jul 29, 2023 pm 07:32 PM

Local variable type inference in Java10: How to use the var keyword to simplify code Introduction: In Java10, the feature of local variable type inference is introduced. By using the var keyword, the code writing process can be simplified. This article will introduce the use of the var keyword and demonstrate its effect of simplifying the code through sample code. 1. What is local variable type inference? Local variable type inference means that when declaring local variables, you can use the var keyword instead of explicit type declaration. The compiler will express

What is the default value of local variables in Java? What is the default value of local variables in Java? Aug 20, 2023 pm 09:41 PM

Local variables can be declared within methods, codeblocks, constructors, etc. in Java. Local variables are created when program control enters a method, code block, constructor, etc., and are destroyed when program control leaves a method, code block, constructor, etc. In Java, local variables have no default value. This means that they can be declared and assigned before the variable is used for the first time, otherwise, the compiler will throw an error. Example publicclassLocalVariableTest{ publicvoidprint(){ &am

Local variable type inference in Java 10: How to use final var keyword in switch statement Local variable type inference in Java 10: How to use final var keyword in switch statement Jul 31, 2023 pm 12:31 PM

Local variable type inference in Java10: How to use finalvar keyword in switch statement As the Java language continues to evolve, each new version introduces some new features and improvements. In Java10, one of the important new features is local variable type inference. This feature allows developers to use the var keyword instead of explicit type declarations, making the code more streamlined and readable. This article will explore how to use the finalvar switch in a switch statement.

See all articles