Home > Backend Development > PHP Tutorial > PHP data types and variables_PHP tutorial

PHP data types and variables_PHP tutorial

WBOY
Release: 2016-07-13 16:59:06
Original
1245 people have browsed it

php tutorial data types and variables
PHP is weakly typed, a variable does not need to be declared in advance, nor does it need to specify a type. Variables in PHP are $ plus the variable name. PHP variables are case-sensitive. For example, $my='my' in the above example.
Variable types supported by PHP include: Boolean, integer, floating point, string, array and object. The first four are very commonly used and are similar to other languages, so I won’t introduce them in detail. Arrays and objects will be introduced in detail later.
There are functions in php to detect the type of objects, they are getttype. gettype returns a string whose value can be array, boolean, double, integer, object, resource, string and unknown type. PHP also supports explicit type conversion, and the syntax is similar to C.
Conversion operator Convert to
(array) array
(bool) (boolean) Boolean
(int) (integer) integer
(object) object
(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 result 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(), etc. The usage is similar.

3. Function and variable scope
The way to declare a function in PHP is very simple, in the following form:
The code is as follows:
function functionname(parameters){
function body
}

There is no need to specify the return type, and there is no need to specify the variable type in parentheses, as long as there is a 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 passes parameters by value. Changing the value of the parameter within the function will not change the value of the variable outside the function. However, PHP also supports passing by reference. The syntax is consistent with C, &$paramname, for example , a classic example below:
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, b is 5
a is 5, b is 3

PHP functions also support default values ​​for 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 introduced below. The variable scope of PHP is very similar to that of C. There are four types: local variables, function parameters, global variables, and static variables. Local variables are variables declared within the function, and function parameters are variables declared at the beginning of the function; variables not declared in the function are global variables, and global variables can be accessed anywhere, but unlike c, if they are in a function To modify the value of a global variable, you need to explicitly specify it as a global variable using the global keyword, 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 output result is:

0.03

1.03

php also has a super global variable. Super global variables are predefined by the PHP system and are mainly used to access environment-related information, such as the current user session, user operating environment and local environment. The super global variable is an array. For example, $_server stores server-related information. $_get, $_post, $_files, and $_cookie respectively store information submitted by the client using get and post, uploaded files, cookie information, etc. The use of these variables is very simple, you only need to look up what information you need

4 variables of variables
Unlike static languages ​​such as C, the variable name of PHP can itself be a variable, which is very convenient when many variables need to be generated dynamically. For example:
The code is as follows:
$r="hello";
$$r="i am hello";
echo $hello;
?>

The output result is: i am hello

5. Process control statement
It mainly includes, if else, while, for, do while, switch. These are very similar to the C language and are basically the same. Not much introduction. There are some differences. PHP's elseif is a keyword that is connected together, while C language is else if

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631347.htmlTechArticlephp tutorial data types and variables php is weakly typed. A variable does not need to be declared in advance, nor does it need to specify a type. . Variables in PHP are $ plus the variable name. PHP variables are case-sensitive...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template