PHP variables

Variables are "containers" used to store information:

Examples

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

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

2. The variable name must start with a letter or underscore character

3. Variable names can only contain alphanumeric characters and underscores (A-z, 0-9 and _)

4. Variable names cannot contain spaces

5. Variable names are case-sensitive ($y and $Y are two different variables)

The following is demonstrated through some examples:

<?php   
$var = 'hello';      //正确  
$var123 = 'hello';    //正确  
3var = 'hello';    //错误  
$_var = 'hello';     //正确  
$@#var = 'hello';     //错误 
?>

PHP statements and PHP variables are case-sensitive.

#Create (declare) PHP variables

PHP has no command to declare variables.

A 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.

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.

We will explain about PHP data types later.

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 (local)

global (global)

static (static)

parameter (parameter)

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 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";
?>

Running example »

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

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 myTest()
{
global $x,$y;
$y=$x+$y;
}
myTest();
echo $y; // 输出 15
?>

PHP will All global variables are 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.

Regarding arrays, we will introduce them in detail in the following chapters. Can access: PHP array

The above example can be written like this:

Instance

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

When a function completes, all 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:

Instance

<?php
function myTest()
{
static $x=0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>

Then, each time When the function is called, the variable will retain the value it had when the function was last 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 myTest($x)
{
echo $x;
}
myTest(5);
?>

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

Variable variables

Variable variables are--before the declared variable, add the variable symbol. Please see the following example:

<?php 
//定义了一个变量叫作 $shu 将$shu这个变量的值设为字符串的biao
$shu = 'biao'; 
//定义了一个【变量】$biao。将他的值设置为鼠标
$biao = '鼠标';
//$$shu 就是可变变量:在已声明的变量$shu前又加上了一个变量符
echo $$shu;
?>

Description of the above process: The value of $shu is 'biao' of the string. Adding a $ (dollar sign) before $shu can be understood as the following deformation process:

$$shu

${$shu} Divide it into two pieces and look at it

${'biao'} interprets the variable $shu as biao

$biao and $biao is also a variable. The corresponding value is: mouse

After understanding, you can try it yourself .

Use variables in combination with HTML tags

If there are two variables $name = "tom", $age = 20. Below we will use these two variables in two ways as an example:

<?php 
$name = "tom"; 
$age = 20; 
?> 
<ul>
  <li> name : <?php echo $name; ?> </li> 
  <li> age : <?php echo $age; ?> </li> 
</ul>
<?php 
$name = "tom"; 
$age = 20; 
echo "<ul>";
echo "<li> name: " . $name . "<li>";
echo "<li> age: " . $age . "<li>";
echo "</ul>";
?>

The above two methods have the same effect, and we hope to use them flexibly.


Continuing Learning
||
<?php $x = 5; $y = 6; $z = $x + $y; echo $z; ?>
submitReset Code