SoFunction
Updated on 2025-04-08

PHP variables for learning PHP

PHP variables

PHP3 supports the following types of variables:
(I) Internal variables
Mainly include integers, float-point numbers, strings, arrays, and objects.
1 Initialize the variable
To initialize a variable in PHP, you just need to simply assign a value to it. For most types, this is the most straightforward. For arrays and objects, other methods can be used.
2 Initialize the array
Arrays can be assigned values ​​using one of two methods: using a series of consecutive numeric values, or using the array() function (see the Array functions section).
To add consecutive values ​​to an array, you just need to assign the value to the array variable without subscript. This value is added to the array as the last element of the array.
Example: $names[] = "Jill"; // $names[0] = "Jill"  $names[] = "Jack"; // $names[1] = "Jack" is similar to c and perl,
The array subscript also starts from 0.
3 Initialize the object
To initialize an object, you need to use the new statement to create a variable of that type.
class foo {
function do_foo() {
echo "Doing foo."; } }   $bar = new foo;   $bar->do_foo();
4 Variable scope
The scope of a variable is its effective scope. For most PHP variables, there is only one scope. Use local variable scope in user-defined functions.
The variables used in the function are set as local variables by default. For example: $a=1; /* global scope */
Function Test() {  echo $a; /* reference to local scope variable */  }  
Test(); This program will not output anything because the echo statement will output the local variable $a, and $a in the function has never been assigned a value.
You may notice that this is a little different from C. Global variables in C can be directly referenced within the function unless it is covered by a local variable.
This makes it possible that one may not pay attention to modifying the value of the global variable. In PHP, you must explicitly explain to use global variables inside a function.
For example: $a=1; $b=2; Function Sum() {  global $a,$b;
$b = $a + $b; }  Sum();  echo $b; The above program will output "3".
By declaring $a and $b are global variables inside the function, all the desired variables refer to the global. There is no limit to the number of global variables that a function can manipulate.
Another noteworthy part of scope is the static variable.
A static variable exists in a local function, but its value is not lost when the program leaves the function.
Consider the following example: Function Test() {  $a=0;  echo $a;  $a++;  }
This function is useless because every time it calls it first assigns $a to 0 and then hits "0". Self-adding to $a++ has no effect because the variable is after the function call is finished
$a is released. To make the counting program count effectively without losing the current counting result, $a must be named as a static variable:
Function Test() { static $a=0; echo $a; $a++;
} Now, every time the Test() function is called, it will type the value of $a and increase its value. Static variables are essential when using recursive functions.
A recursive function is to call its own function. Be very careful when writing recursive functions.
Because the number of cycles is uncertain. You must make sure that there are sufficient conditions to end the recursion process. Here is a simple recursive function counting to 10:
Function Test() {  static $count=0;  $count++;  
echo $count;  if($count < 10) {  Test();  } }
(II) Dynamic variables Sometimes it is more convenient to use variable variable names. That is, a variable name that can be dynamically assigned and used.
The assignment statement of a normal variable is such as: $a = "hello"; A dynamic variable refers to the value of the variable as the name of a new variable.
In the above example, hello can be used as variable name through double $.
Example: $$a = "world"; At this point, two variables are defined and stored in the PHP symbol tree: the content of $a is "hello", and the content of $hello is "world".
Therefore, the display result of the statement: echo "$a ${$a}"; is exactly equivalent to: echo "$a $hello"; (III) PHP external variables 1. HTML form (GET and POST)
When a form is submitted to PHP3 script, PHP will automatically get the variables in the form. For example:

Name:  

When "submit" is pressed, PHP3 will automatically generate a variable: $name, which contains all the contents entered by the user. 2. IMAGE SUBMIT variable name
When submitting a form, you can use a picture instead of the standard submit button by marking the following: When the user clicks on the picture,
Two additional variables sub_x and sub_y will be sent to the server along with the form. It contains the coordinates of the user clicking on the graph.
Experienced people may notice that the name actually sent by the browser contains a period instead of an underscore, but PHP automatically converts the period into an underscore.
3、HTTP Cookies
PHP supports HTTP cookies. Cookies store data in client browsers to keep in touch with users or verify user identity.
You can use the setcookie() function to set cookies. Cookies are part of the HTTP request header, so they must be returned to the user's browser before any output data is returned.
Call SetCookie() function. It is similar to the limitations of the Header() function. Any cookies returned by the user will be automatically converted to standard PHP variables.
Just like the data of GET and POST methods.
If you want to set multiple values ​​in a cookie, add [] to the name of the cookie
For example: SetCookie("MyCookie[]","Testing", time()+3600);
Note: New cookies will overwrite cookies of the same name already exist in your browser unless they have different paths or domains.
4. Environment variables
PHP automatically converts environment variables into normal variables.
echo $HOME; /* Shows the HOME environment variable, if set. */
Although information from GET, POST and cookie structures will also be automatically converted into PHP variables, it is best to read them explicitly from environment variables to ensure the correct value is obtained.
For this purpose, use the getenv() function. You can also set variables through the putenv() function.
Variable type conversion
PHP does not require (or support) explicit type declarations when variable definitions are defined; the type of a variable depends on the type of its value.
That is, if you assign a string value to the variable var, var becomes a string variable. If you assign an integer value to var, it becomes an integer variable.
An example of PHP automatic type conversion is the addition operator '+'. If any operand is double type, all operands are calculated according to double type.
The result is also double. Otherwise, all operands are calculated according to the integer type, and the result is also integer type. Note: The type of the operand itself does not change;
Type transformation is only done when calculating $foo = "0"; // $foo is a string (ASCII 48) $foo++; // $foo is the string "1" (ASCII 49)
$foo += 1; // $foo is now an integer (2) $foo = $foo + 1.3; // $foo is now a double (3.3)
$foo = 5 + "10 Little Piggies"; // $foo is a double (15) $foo = 5 + "10 Small Pigs"; // $foo is an integer (15)
To change the type of a variable, you can also use the settype() function.
1. Forced type conversion
The cast in PHP is the same as in C: Write the desired type name in parentheses before the variable that needs to be cast.
$foo = 10; // $foo is an integer  $bar = (double) $foo; // $bar is a double  
The allowed casts are: (int), (integer) - cast to integer (real), (double), (float) - cast to double
(string) - cast to string (array) - cast to array   (object) - cast to object
Note: The brackets can contain tabs or spaces, and the following functions will be calculated: $foo = (int) $bar; $foo = ( int ) $bar;
2. String conversion
When a string is calculated as a numerical type, the value and type of the result are determined in the following way.
If the string contains any '.', 'e', ​​and 'E' characters, it is calculated as double type. Otherwise, it is calculated as integer type.
This value starts from the beginning of the string. If the string is a legal number, the value is used, otherwise the value is 0.
A legal number is a sign bit (optional), followed by a one or several digits (can also contain a decimal decimal point), followed by an optional exponent.
An index is a 'e' or 'E' followed by a number or several numbers. $foo = 1 + "10.5"; // $foo is a double (11.5)
$foo = 1 + "-1.3e3"; // $foo is a double (-1299) $foo = 1 + "bob-1.3e3"; // $foo is a double (1)
$foo = 1 + "bob3"; // $foo is an integer (1) $foo = 1 + "10 Small Pigs"; // $foo is an integer (11)
$foo = 1 + "10 Little Piggies"; // $foo is a double (11); the string contains 'e'