Php Data type and variable _ PHP Tutorial

Source: Internet
Author: User
Php Data types and variables. Php Tutorial data type and variable php are weak type, a variable does not need to be declared in advance, do not need to specify the type. In php, the variable is $ plus the variable name. in php, the variable is case sensitive. in php Tutorial, the data type and variable
Php is of a weak type, and a variable does not need to be declared in advance or specified. In php, the variable is $ plus the variable name. in php, the variable is case sensitive. For example, $ my = 'My in the above example '.
Php supports the following variable types: Boolean, integer, floating point, string, array, and object. The first four are very common and similar to other languages. Introduction to arrays and objects.
Php has functions to detect object types. they are getttype. Gettype returns a string whose values can be array, boolean, double, integer, object, resource, string, and unknow type. php also supports explicit type conversion. The syntax is similar to that of c.
Convert operators
(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 = '192. 3 ';
Echo gettype ($ str ),'
';
Echo gettype ($ num ),'
';
Echo gettype ($ numstr ),'
';
$ Numstr = (float) $ numstr;
Echo gettype ($ numstr );
?>

Output result:

String
Integer
String
Double

Other functions can be used to determine whether a variable is of a certain type, such as is_array () and is_bool (). The usage is similar.

3. function and variable scope
The php method for declaring a function is as follows:
The code is as follows:
Function functionname (parameters ){
Function body
}

You do not need to specify the return type. you do not 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 transmits parameters by value. changing the value of a parameter in a function does not change the value of a variable outside the function. However, php also supports passing parameters by reference, the syntax is the same as that of c. & $ paramname. for example, the following is a 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, B is 5
A is 5, B is 3

Php functions also support the default parameter values. The syntax is the same as that of c. For example:
The code is as follows:
Function taxedprice ($ price, $ taxrate = 0.03 ){
Return $ price * (1 + $ taxrate );
}
Echo taxedprice (100 );
?>

The following describes the scope of a variable. Php's variable scope is similar to c. There are four types of variables: local variables, function parameters, global variables, and static variables. A local variable is a variable declared in a function. a function parameter is a variable declared in the function header. a variable not declared in a function is a global variable, which can be accessed anywhere, however, unlike c, if you want to modify the value of a global variable in a function, you must use the global keyword to explicitly specify that 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 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 information related to the environment, such as the current user session, user operation environment, and local environment. A Super global variable is an array. for example, $ _ server stores server-related information. $ _ Get, $ _ post, $ _ files, and $ _ Cookies respectively store the information submitted by the client using get, post, uploaded files, and cookie information. These variables are easy to use. you only need to find the required information.

4. variable
Unlike the static language c, the php variable name itself can be a variable, which is convenient for dynamic generation of many variables. For example:
The code is as follows:
$ R = "hello ";
$ R = "I am hello ";
Echo $ hello;
?>

Output result: I am hello

5. process control statements
It mainly includes, if else, while, for, do while, switch. These are similar to the C language and are basically the same. Not much. In some cases, php elseif is a keyword and is connected together, while C is else if

The struct data type and variable php are weak. a variable does not need to be declared in advance or specified. In php, the variable is $ plus the variable name. in php, the variable is case sensitive...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.