PHP variables

What is a variable

A variable refers to the amount that can change in value during program execution. Variables are identified by a name (variable name). The system allocates a storage unit for each

variable in the program. The variable name is essentially the name of the computer memory unit. Therefore, the data in memory

can be accessed with the help of variable names.

Variables are "containers" used to store information:

Example

<?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 variable

Similar to algebra, you can assign a certain value to a PHP variable (x=5) or 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:

· Variables start with a $ symbol, followed by the name of the variable

· Variable names must start with letters or underscore characters

·                                                                                                                                                                                                                                      – ’’’’ With the number of alphanumeric characters and underscores (A‐z, 0‐9, and _ )

·        Variable names cannot contain spaces $Y are two different variables)

$var = 'hello'; //Correct

$var123 = 'hello'; //Correct

$123var = 'hello'; // Error
$_var = 'hello'; //Correct
$@#var = 'hello'; //Error

PHP statement 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;
 ?>

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.

Variable assignment: refers to giving a specific data value to a variable. For string and numeric type variables, assignment can be achieved through "=".

Grammar: <?php $name=value;?>

name is the name of the variable. You can choose it at will according to your needs and preferences, but it is best to use words related to functions or functions. .

value is the value to be assigned to the variable.

example:

<?php
 $color="red";
$_name="jack";
?>


PHP is a weakly typed language

In the above example, We note 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 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 PHP functions are local variables and can only be accessed inside the function:

Example

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

In the above example The myTest() 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 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 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 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 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.

PHP global keyword

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 myTest()
 {
 global $x,$y;
 $y=$x+$y;
 }
 
 myTest();
 echo $y; // 输出 15
 ?>
PHP will all global variables Stored in an array named $GLOBALS[index]. index holds 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:

Example

<?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();
 
 ?>

Then, each time the function is called, the variable will retain the value from the previous time the function was called.

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

Variable variable

A variable variable is a unique variable that allows a variable name to be changed dynamically. The working principle is that the name of the variable is determined by the value of another variable. The implementation process is to add an extra "$" in front of the variable.

Parameter scope

Parameters are local variables that pass values ​​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(5);
 
 ?>

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


System variables

Most of the variables built into the PHP system start with an underscore. The specific usage will be introduced in the following courses

• $ Globals Storage all the global variables in the current script. Store the data in the form submitted by the GET method

• ​ $_POST Store the data in the form submitted by the POST method

• ​​$_COOKIE Obtain or set the variable array stored in the user's browser Cookies

• $ _Files Storage Upload files Submit the current script data

• $ _env Storage The current web environment variable

• $ _Request storage all the request arrays in the form, including of which include Everything in $_GET, $_POST, $_COOKIE, and $_SESSION

$_SESSION An array of session variables that stores the current script

Continuing Learning
||
<?php function myTest($x) { echo $x; } myTest(5); ?>
submitReset Code