php variables

PHP Variables

Variables are "containers" used to store information:

Example (1)

<?php
 $x=5;
 $y=6;
 $z=$x+$y;
 echo $z;
 ?>

Similar to algebra

x=5
y=6
z=x+y

In algebra, we use letters (like x) and assign values ​​to them (like 5).

From the above expression z=x+y, we can calculate the value of z to be 11.

In PHP, these letters are called variables.

Variables are containers used to store data.

PHP variables

Similar to algebra, PHP variables can be assigned a value (x=5) or an expression (z=x+y).

Variables can have very short names (such as x and y) or more descriptive names (such as age, carname, totalvolume).

PHP variable rules

· Variable name can only include letter number characters and the lower strokes (A-Z, 0-9, and _)

## · Variable name cannot include spaces

· Variable name is distinguished size. Written ($y and $Y are two different variables)

PHP statements and PHP variables are case-sensitive.

Create (declare) PHP variables

PHP has no command to declare variables.

The variable is created when you first assign a value to it:

Instance

<?php
$txt="Hello world!";
$x=5;
$y=10.5;
echo $txt;
echo $x;
echo $y;
?>

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, and the variable Y will hold the value 10.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 noticed that it is not necessary to declare the data type of the 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 three different variable scopes:

·                                                                                                                                                                                                      

#Local and global scope

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.

Variables declared inside a PHP function are local variables and can only be accessed inside the function:

Example

<?php 
 $x=5; // 全局变量 
 
 function text () 
 { 
     $y=10; // 局部变量 
     echo "<p>测试函数内变量:<p>"; 
     echo "变量 x 为: $x"; 
     echo "<br>"; 
     echo "变量 y 为: $y"; 
 }  
 
 text (); 
 
 echo "<p>测试函数外变量:<p>"; 
 echo "变量 x 为: $x"; 
 echo "<br>"; 
 echo "变量 y 为: $y"; 
 ?>

In the above example, the text () function defines $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 text () 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 defined outside the function and cannot 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 text () function. The function will output the value of all local variables $x, but cannot output the value of $y because the $y variable is defined in the function. Belongs to local variables.

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

global keyword

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

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

Example

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

PHP will all global variables Stored in an array named $GLOBALS[demo]. demo saves the name of the variable. This array can be accessed inside the function or used directly to update global variables.

The above example can be written like this:

Instance

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

Static scope

When a function completes, its All variables are normally 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 text ()
 {
 static $x=0;
 echo $x;
 $x++;
 }
 
 text ();
 text ();
 text ();
 
 ?>

Then, each time the function is called, the The variable will retain its value from the last time the function was called.

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 text ($x)
 {
 echo $x;
 }
 
 text (5);
 
 ?>
Continuing Learning
||
<?php $txt="Hello world!"; $x=5; $y=10.5; echo $txt; echo $x; echo $y; ?>
submitReset Code