Home > Article > Backend Development > What are the common variables in PHP programming?
In PHP programming, variables are the basic unit of storing values and are used to store and use data during program execution. In PHP, variables can be assigned different data types, including integers, floating point, strings, arrays, etc. In this article, we will introduce common variables and their usage in PHP programming.
Simple variables are the most common variable type. They can store regular data types such as integers, floating point numbers, and strings. In PHP, the initial value of undefined variables is NULL. The following are a few examples:
Integer variable:
$num1 = 12; $num2 = -345; $num3 = 0x80 ;
Floating point variable:
$float1 = 1.234; $float2 = 10.2e3; $float3 = 4E-10;
String variable:
$str1 = "Hello World!"; $str2 = 'PHP is great!';
An index array is a collection of values controlled by a numeric index key. It is usually used to store a set of ordered data. In PHP, we can create an indexed array using the array()
function. The following is an example:
$colors = array("Red", "Green", "Blue");
The values of an array can be accessed using its index value, for example:
echo $colors[0]; // 输出 "Red" echo $colors[1]; // 输出 "Green" echo $colors[2]; // 输出 "Blue"
You can also use a loop structure to traverse the array:
foreach($colors as $value){ echo $value . "<br>"; }
When traversing the array , you can use key
and value
to represent key values and array element values:
foreach($colors as $key => $value){ echo $key . " = " . $value . "<br>"; }
Associative array is A collection of values controlled by a string index key, usually used to store an unordered set of data. In PHP, we can create associative arrays using the array()
function. Here are a few examples:
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); $months = array("Jan"=>"31", "Feb"=>"28", "Mar"=>"31", "Apr"=>"30");
The value of an array can be accessed using its key value, for example:
echo $age["Peter"]; // 输出 "35" echo $months["Jan"]; // 输出 "31"
When traversing an associative array, the foreach
structure can also be used:
foreach($age as $key => $value){ echo $key . " is " . $value . " years old.<br>"; }
In PHP, variables can be global or local. Global variables are defined and used outside the function, while local variables are defined and used inside the function. Local variables are destroyed when the function completes execution, while global variables exist throughout the execution of the program.
In order to access global variables inside a function, we need to use the global
keyword declaration in the function:
$num = 10; function test(){ global $num; echo $num; } test(); // 输出 "10"
Local variables can also be created and used inside the function:
function test(){ $num = 100; echo $num; } test(); // 输出 "100"
Static variables are local variables defined inside the function, but unlike ordinary local variables, static variables will not is destroyed and its value continues to be saved until the next function call. This is useful when you need to track changes in certain values. The following is an example:
function test(){ static $num = 0; echo $num; $num++; } test(); // 输出 "0" test(); // 输出 "1" test(); // 输出 "2"
At each function call, the value of the static variable $num
continues to increase.
In summary, these are common variable types and usages in PHP programming. Mastering the basic concepts and usage of these variables is very important for developing high-quality PHP programs.
The above is the detailed content of What are the common variables in PHP programming?. For more information, please follow other related articles on the PHP Chinese website!