Php Data types and variables

Source: Internet
Author: User
Php is of a weak type. a variable does not need to be declared in advance or specified. in php, the variable $ is added with the variable name, and the php variable is case sensitive, for example, in the previous example, $ my & amp; 39; my & amp; 39

Php is of a weak type. a variable does not need to be declared in advance or specified. in php, the variable $ is added with the variable name, and the php variable is case sensitive, for example, in the previous example, $ my = 'my '.

Php supports the following variable types: Boolean, integer, floating-point, string, array, and object. the first four variables are frequently used and are 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.

Conversion operator

(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:

  1. $ Str = 'A string ';
  2. $ Num = 15;
  3. $ Numstr = '192. 3 ';
  4. Echo gettype ($ str ),'
    ';
  5. Echo gettype ($ num ),'
    ';
  6. Echo gettype ($ numstr ),'
    ';
  7. $ Numstr = (float) $ numstr;
  8. Echo gettype ($ numstr );
  9. ?>

Output result:

  1. String
  2. Integer
  3. String
  4. 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.

Function and variable scope,The php method for declaring a function is as follows:

  1. Function functionname (parameters ){
  2. Function body
  3. }

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. The sample code is as follows:

  1. Function taxedprice ($ price, $ taxrate ){
  2. Return $ price * (1 + $ taxrate );
  3. }
  4. Echo taxedprice (100, 0.03 );
  5. ?>

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:

  1. Function swap1 ($ x, $ y ){
  2. $ T = $ x; $ x = $ y; $ y = $ t;
  3. }
  4. Function swap2 (& $ x, & $ y ){
  5. $ T = $ x; $ x = $ y; $ y = $ t;
  6. }
  7. $ A = 3; $ B = 5;
  8. Swap1 ($ a, $ B );
  9. Printf ("a is % d, B is % d
    ", $ A, $ B );
  10. Swap2 ($ a, $ B );
  11. Printf ("a is % d, B is % d
    ", $ A, $ B );
  12. ?>

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:

  1. Function taxedprice ($ price, $ taxrate = 0.03 ){
  2. Return $ price * (1 + $ taxrate );
  3. }
  4. Echo taxedprice (100 );
  5. ?>

The following describes the scope of variables. php variables are very similar to c. There are four types of variables: local variables, function parameters, global variables, and static variables. local variables are the variables declared in the function, function parameters are declared variables in the function header. variables not declared in the function are global variables. global variables can be accessed anywhere, but 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:

  1. $ Taxrate = 0.03; // global
  2. Function change1 (){
  3. $ Taxrate + = 1;
  4. }
  5. Function change2 (){
  6. Global $ taxrate;
  7. $ Taxrate + = 1;
  8. }
  9. Change1 ();
  10. Echo $ taxrate ,'
    ';
  11. Change2 ();
  12. Echo $ taxrate ,'
    ';
  13. ?>

Output result: 0.03, 1.03

Php also has a super global variable, which is predefined by the php system and is mainly used to access information related to the environment, such as the current user session, 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.

Variable

Unlike the static language c, the php variable name itself can be a variable, which is convenient when many variables need to be dynamically generated. for example, the code is as follows:

  1. $ R = "hello ";
  2. $ R = "I am hello ";
  3. Echo $ hello;
  4. ?>
  5. // The output result is: I am hello

Process control statement

It mainly includes, if else, while, for, do while, switch. these are similar to the C language, which is basically the same. we will not introduce them much. Some are different. the elseif keyword of php is a keyword that is connected together, while the C language is else if

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.