The setting of PHP variables (Variables) is very simple. You can directly set a variable and assign a variable value without declaration. There are many different types of PHP variables, such as common string variables String, integer variables Integer, floating-point numbers Float, boolean Boolean, array array, Object Object ... etc., to provide PHP program a variety of applications, these variables will have patterns in Benpian detailed introduction, PHP learning behind almost impossible not to use variables, PHP absolute Most functions also accept the writing of directly importing PHP variables, so for beginners, it is very important to lay the foundation of PHP variables. Here is a simple example of PHP variables.
PHP variable Variables simple example<?php
$V=1;
$V1="Wibibi";
$x='welcome';
?>
There are three variables in the example. The value of the first variable $V is the number 1, the value of the second variable V1 is Wibibi, and the value of the third variable $x is welcome. All three are PHP variables The standard setting method, the designer does not need to declare any variable name, he can write a variable directly and immediately assign the variable value to the just written variable. The value on the right side of the equal sign (=) is the variable value. In addition, PHP variables It also accepts the assignment of variable values by means of addressing. The means of addressing must be operated by the passing symbol (&).The setting rules of PHP variable Variables Although the setting method ofPHP array is very simple, there are still certain usage rules that must be followed. The basic rules of common PHP variable setting are as follows:- All variables must have a dollar sign ($) at the beginning.
- The variable name can be composed of English letters, numbers, underscores, and hexadecimal characters 0x7f-0xff.
- The first letter of a variable cannot start with a number. If you must use a number, you can write it in ${number}.
- Use the half-shaped equal sign (=) to specify the variable value.
- Accept the variable names of different types connected through the address symbol (&) to be assigned through the address method (assign).
We use these basic rules to write a simple example, and see the effect of variable settings in practical applications.PHP variable Variables setting and output<?php
${5}=5;
echo ${5}; // output the number 5
$a=5;
echo $a; // output the number 5
?>
Output result55
The two variables in the example both start with the dollar sign ($). The first variable ${5} demonstrates how to make the number the beginning of the variable. You can wrap the number in braces, but strictly speaking, this is not the case. It starts with a number. The second variable $a is the standard PHP variable notation. Both variables are given the variable value through the equal sign (=). Echo is a common structural representation of PHP , which can output variable values to the screen. Since we used echo twice in the example, two numbers 5 will be output. If you want these two numbers 5 to be one line each, you must use PHP 's echo line-wrapping technique. Please refer to the description of " PHP echo line-wrapping ". Then there are common PHP variable types and settings.PHP variable type one: string type String<?php
$my_variables="Welcome to Wibibi.";
echo $my_variables;
?>
Output resultWelcome to Wibibi.
The PHP variable setting of the string type can be set through the half-shaped equal sign (=). The right side of the equal sign is the string value, which can be Chinese, English or symbols. If you are setting a string variable, you must use single quotes (") or double quotes ("") on both sides of the variable value to enclose the variable . For the rules of using PHP strings, please refer to: PHP String String .PHP variable type two: integer type Integer<?php
$my_integer_number=20;
echo $my_integer_number;
?>
Output result20
To set an integer type (Integer) PHP variable, you don't need to use single or double quotation marks, just give the value directly through the equal sign.PHP variable type three: floating point number Float<?php
$my_float=3.1415926;
echo $my_float;
?>
Output result3.1415926
The variable setting of floating-point numbers is the same as that of integers.PHP variable type 4: Array type Array<?php
$my_array=array('a','b','c');
print_r($my_array);
?>
Output resultArray (
[0] => a
[1] => b
[2] => c
)
Array patterns for PHP variables to the same value by an equals sign, the difference in the right hand side is an array (the Array), an array of advantages is the large amount of data stored in the same array within, the array is stored as a variable, In this way, when you need to use it, you can easily output a large amount of data. It should be noted here that the output of the array should be processed by the print_r function instead of echo . For the skills of using the array, please refer to: PHP Array () Array function usage .PHP variable type five: Boolean<?php
$MyVariables1=true;
$MyVariables2=false;
$MyVariables3=$MyVariables1+$MyVariables2; //1+0=1, so output 1
$MyVariables4=$MyVariables1-$MyVariables1; //1-1=0, so Output 0
echo $MyVariables3;
echo $MyVariables4;
?>
Output result10
The example sets a total of four different variables, among which $MyVariables1 and $MyVariables2 are the Boolean values of true and false, respectively. If expressed as numbers, they are 1 and 0, so the third variable $MyVariables3 and the fourth variable $MyVariables4 You can use the Bollinger values of the first two variables to do addition and subtraction verification, and then output MyVariables3 and $MyVariables4 to see the results. PHP variables expressed in Bollinger values are usually not used for calculations, but to judge "yes". Or "not".The above is the commonly used PHP variable (Variables) setting methods and various types. Familiarity with these basics is of great help to subsequent PHP design.
Post a Comment
0 Comments