Variables are "containers" used to store information.
Syntax for defining a variable:
$变量名 = 值;
Example of using variables:
<?php $x=5; $y=6; $z=$x+$y; echo $z; ?>
Run
As you can see from this example, the variable name starts with $, indicating that it is a variable. Variable names start with letters (a-z, A-Z) or underscores _, and can be followed by any letters or numbers and underscores, but not spaces.
PHP variable rules:
Note: PHP statements and PHP variables are case-sensitive.
The following variable names are valid:
$var_char $varChar $_varChar $var_char5
PHP has no command to declare variables.
A variable is created the first time you 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 save the value Hello world!, and the variable x will save the value 5 .
Note: When you assign a text value to a variable, please put quotation marks around the text value.
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.
The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has four different variable scopes:
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:
<?php $x=5; // 全局变量 function myTest() { $y=10; // 局部变量 echo "<p>Test variables inside the function:<p>"; echo "Variable x is: $x"; echo "<br>"; echo "Variable y is: $y"; } myTest(); echo "<p>Test variables outside the function:<p>"; echo "Variable x is: $x"; echo "<br>"; echo "Variable y is: $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 be inside the function Use, 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 and is a local variable. .
Note: 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.
The global keyword is used to access global variables within a function.
To call a global variable defined outside the function within a function, we need to add the global keyword before the variable in the function:
<?php $x=5; $y=10; function myTest() { global $x,$y; $y=$x+$y; } myTest(); echo $y; // 输出 15 ?>
PHP stores all global variables in an array called $GLOBALS[index]. index 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:
<?php $x=5; $y=10; function myTest() { $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y']; } myTest(); echo $y; ?>
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:
<?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: The variable is still local to the function.
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:
<?php function myTest($x) { echo $x; } myTest(5); ?>
Original address: http://www.manongjc.com/php/php_variables.html
php related reading:
php str_pad() function completes the string to the specified length
Analysis and explanation of usage of php str_replace() string replacement function
php str_ireplace() function is case-insensitive to replace characters in a string
php str_getcsv() function parses the CSV string into an array
php sscanf() function usage and examples