PHP variablesLOGIN

PHP variables

Variables are "containers" used to store information:

Everyone is in junior high school time. Teachers often teach you this.

Excuse me, Li Lei and Han Meimei, if:

x = 5

y = 6

then x + y equals how much? Everyone will answer without hesitation. x + y equals 11.

Next let’s look at the following junior high school mathematics knowledge. What is the result of x + y?

x = 5

y = 6

x = 8

I guess everyone will do the same The unhesitating answer: The result of x + y is 14.

This is a variable!


Several characteristics of variables:

1. x = 5 Assign the value 5 on the right to x

2 on the left. The second section x = 8, and the final result of x + y is equal to 14, indicating that x is in the operation (execution) from top to bottom. Can be reassigned.


PHP Variables

Our variables in PHP is also like this. But there are a few Features:

1. It must start with $. For example, the variable , Chinese, _ is not a special symbol

5. Variable names must be meaningful (don’t write variable names like xxx, aaa, ccc)

6. $ is called the dollar sign, an English word :dollar. PHP variables must start with a dollar sign. Picture showing how “money” it is to do PHP

##Create (declare) PHP variables


PHP has no command to declare variables. The variable is created when you first assign a value to it:

<?php
 $txt="Hello world!";
 $x=5;
 $y=10.5;
 ?>
In the execution of the above statement, the variable txt will hold the value Hello world!, and the variable x will hold the value 5 .

Note

: When you assign a text value to a variable, please add quotation marks around the text value.


PHP is a weakly typed language

In the above example , we notice that it is not necessary to declare the data type of this variable to PHP.

PHP will automatically convert the variable into the correct data type based on its value.

In a strongly typed programming language, we must declare (define) the type and name of the variable before using it.


PHP variable scope

The scope of a variable is the part of the script where the variable can be referenced/used.

PHP has four different variable scopes:

· local      

·   global

·                                                                                ##Local and global scope Local and global scope (local global)

Variables defined outside all functions have global scope. In addition to functions, global variables can be accessed by any part of the script. To access a global variable in a function, you need to use the global keyword.


Example
Variables declared inside a PHP function are local variables and can only be used inside the function Visit:

<?php
 header("Content-type:text/html;charset=utf-8");
 $x=5; // 全局变量
 
 function myTest()
 {
     $y=10; // 局部变量
     echo "<p>测试函数内变量:<p>";
     echo "变量 x 为: $x"; //输出错误 Notice: Undefined variable:
     echo "<br>";
     echo "变量 y 为: $y";
 }
 
 myTest();
 
 echo "<p>测试函数外变量:<p>";
 echo "变量 x 为: $x";
 echo "<br>";
 echo "变量 y 为: $y";  //输出错误 Notice: Undefined variable:
In the above example, the myTest() function defines the $x and $y variables. The $x variable is declared outside the function, so it is a global variable, and the $y variable is declared inside the function, so it is a

local variable.

When we call the myTest() function and output the values ​​of two variables, the function will output the value of the local variable $y, but cannot output the value of $x because the $x variable is determined outside the function

meaning, cannot be used within a function. If you want to access a global variable in a function, you need to use the global keyword.

Then we output the values ​​of the two variables outside the myTest() function. The function will output the value of all local variables $x, but cannot output the value of $y because the $y variable is in the function

Definition, belongs to local variables.

Note You can use the same variable name in different functions, because the variable names defined in these functions are local variables and only act within that function.



PHP global keyword

global Keywords are used to access global variables within functions.

Example

#To call a global variable defined outside a function within a function, we need to add the global keyword before the variable in the function:

<?php
 $x=10;
 $y=20;
 function test(){
     global $x,$y;      //使用global关键字
 
     $y=$x+$y;
 }
 test();
 echo $y;//输出30

PHP stores all global variables in an array named $GLOBALS[index]. index holds the name of the variable. This array can be accessed inside the function, and can also be used directly to update global variables.

The above example can be written like this:

<?php
 $x=5;
 $y=10;
 
 function myTest()
 {
     $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y'];
 }
 
 myTest();
 echo $y;
 
 ?>



Static scope


#When a function completes, all of its variables are usually deleted. However, sometimes you want a local variable not to be deleted.

To do this, use the static keyword the first time you declare the variable:

Example

<?php
 function myTest()
 {
     static $x=0;
     echo $x;
     $x++;
 }
 myTest();
 myTest();
 myTest();
 myTest();
 
 ?>
Then, each time the function is called, the variable will retain the value when the function was called before.


Note

: This variable is still a local variable of the function.


Parameter scope

Parameters are local variables whose values ​​are passed to the function through the calling code.

Parameters are declared in the parameter list, as part of the function declaration:

Example

?php
 
 function myTest($x)
 {
     echo $x;
 }
 
 myTest(name);
 
 ?>

We will discuss this in more detail in the PHP Functions chapter.



Next Section
<?php header("Content-type:text/html;charset=utf-8"); $x=5; // 全局变量 function myTest() { $y=10; // 局部变量 echo "<p>测试函数内变量:<p>"; echo "变量 x 为: $x"; //输出错误 Notice: Undefined variable: echo "<br>"; echo "变量 y 为: $y"; } myTest(); echo "<p>测试函数外变量:<p>"; echo "变量 x 为: $x"; echo "<br>"; echo "变量 y 为: $y"; //输出错误 Notice: Undefined variable: ?>
submitReset Code
ChapterCourseware