PHP tutorial data types and variables
PHP is a weak type, a variable that does not need to be declared beforehand, and does not require a specified type. PHP variables are $ plus variable names, PHP variables are case-sensitive. For example, $my = ' my ' in the previous example.
The types of variables supported by PHP include: boolean, Integer, float, string, array, and object. The first four kinds are very common, also similar to other languages, do not introduce more. Arrays and objects are described in detail later in this article.
PHP has functions to detect the types of objects, which are getttype. GetType returns a string whose value can be array,boolean,double,integer,object,resource,string and unknow type. PHP also supports explicit type conversions, and syntax is similar to C.
Conversion operator to
Arrays (Array)
(BOOL) (Boolean) Boolean type
(int) (integer) Integer
(object) objects
(float), (double), (real) floating-point number
(String) string
For example:
The code is as follows:
$str = ' A string ';
$num = 15;
$numstr = ' 123.3 ';
Echo GetType ($STR), '
';
Echo GetType ($num), '
';
Echo GetType ($NUMSTR), '
';
$numstr = (float) $numstr;
Echo GetType ($NUMSTR);
?>
The output is:
String
Integer
String
Double
There are also functions that can be used to determine whether a variable is of a certain type, such as Is_array (), Is_bool (), and so on, with similar usage.
3. function and Variable scope
PHP declares functions in a simple way, in the following form:
The code is as follows:
function functionname (parameters) {
function body
}
You do not need to specify a return type, and you do not need to specify the variable type in parentheses, as long as you have the variable name. For example:
The code is as follows:
function Taxedprice ($price, $taxrate) {
Return $price * (1+ $taxrate);
}
Echo taxedprice (100, 0.03);
?>
By default, PHP is passing parameters by value, and changing the value of the parameter within the function does not change the value of the out-of-function variable, but PHP also supports pass-by-reference, syntax and C-consistent,& $paramname, for example, the following classic example:
The code is as follows:
function Swap1 ($x, $y) {
$t = $x; $x = $y; $y = $t;
}
Function Swap2 (& $x,& $y) {
$t = $x; $x = $y; $y = $t;
}
$a =3; $b = 5;
SWAP1 ($a, $b);
printf ("A is%d, B is%d
", $a, $b);
SWAP2 ($a, $b);
printf ("A is%d, B is%d
", $a, $b);
?>
Output Result:
A is 3, and B is 5
A is 5, and B is 3
PHP's functions also support the default values of parameters, and the syntax is the same as C. For example:
The code is as follows:
function Taxedprice ($price, $taxrate =0.03) {
Return $price * (1+ $taxrate);
}
echo taxedprice (100);
?>
The scope of variables is described below. PHP's variable scope and C are very similar, there are local variables, function parameters, global variables, static variables 4 kinds. A local variable is a variable declared within a function, which is a variable declared at the first part of the functions, a variable that is not declared in a function is a global variable, and a global variable can be accessed anywhere, but unlike C, if the value of a global variable is to be modified explicitly in the function, it is a global variable. Otherwise, PHP will declare a local variable with the same name and overwrite it. For example:
The code is as follows:
$taxrate = 0.03; Global
function Change1 () {
$taxrate +=1;
}
function Change2 () {
Global $taxrate;
$taxrate +=1;
}
Change1 ();
Echo $taxrate, '
';
Change2 ();
Echo $taxrate, '
';
?>
The result of the output is:
0.03
1.03
PHP also has a super global variable. Super global variables are predefined by the PHP system and are primarily used to access information about the environment, such as current user sessions, user operating environments, and local environments. A super global variable is an array, such as a $_server that stores information about the server. $_get,$_post,$_files,$_cookie stores the client with get submission, and post submission information, uploaded files, cookie information, etc. The use of these variables is simple, and what information is needed to find only
Variables of 4 variables
Unlike static languages such as C, the PHP variable name itself can be a variable, which is handy for the need to dynamically generate many variables. For example:
The code is as follows:
$r = "Hello";
$ $r = "I am Hello";
Echo $hello;
?>
The output is: I am hello
5. Process Control Statements
The main include, if else, While,for,do While,switch. These are very similar to the C language and are basically consistent. Not much to do the introduction. Somewhat different, PHP ElseIf is a keyword that is linked together while the C language is the else if
http://www.bkjia.com/PHPjc/631347.html www.bkjia.com true http://www.bkjia.com/PHPjc/631347.html techarticle PHP Tutorial data types and variables PHP is a weak type, a variable that does not need to be declared beforehand, and does not require a specified type. PHP variables are $ plus variable names, PHP variables are case-sensitive ...