PHP basic tutorial 2: basic data types

Source: Internet
Author: User
This section describes the php basic data types of php variables in four Tag formats of php.

Content described in this section
  • Brief introduction to php

  • Four Tag formats of php

  • Php variables

  • Basic data types of php

Brief introduction to PHP

Zookeeper php is an open-source scripting language mainly used for web development. the syntax absorbs the features of C, Java, and perl, which is conducive to learning and widely used, compared with other programming languages, php embeds programs into HTML documents for execution, making dynamic pages made using PHP faster.

Four Tag formats of PHP

When the php code is written to a fixed format, the parser will execute the php code. There are four common PHP labels:

We generally use the first format. It is also the most common format.

PHP variables

Variable "Variable variables" indicates the number that can be changed without a fixed value.

Variable creation aims to store data for subsequent operations on data.

Naming rules for variable names: it must contain numbers, letters, and underscores and cannot start with a number.

Common naming rules:

  • Camper naming method: username-> userName: starts from the second word and starts with an upper-case letter.

  • Pascal's name method: username-> UserName: the first letter of each word is capitalized.

  • Underline naming rules: username> user_name: all words are in lower case. separate words with underscores:

In the future, the naming rules of function names can also be summarized into these three points.

PHP variable names have the following features: one must be added before the variable$Symbol, that is, shift + 4. for the convenience of development, the variable name should generally have meaning.

PHP basic data type

Zookeeper uses the data type statement in most languages. data types can classify the data we use in development for better management.

Basic data types of PHP:

  • Basic data types: integer (int/integer), float/double, boolean, and string)

  • Composite data type: array, object)

  • Special data types: null and resource)

Integer data type

The   integer data type is the common negative integer and positive integer;

The integer type in PHP can be octal (every eight to one), decimal, and hexadecimal (every sixteen to one ).

$ A = 2; // decimal $ a = 023; // octal $ a = 0x23; // hexadecimal

Octal: there is a 0 in front, which indicates octal.

Hexadecimal: There are two 0x in front, indicating the hexadecimal format.

The size of an integer is limited. The integer length of PHP is related to the platform. generally, it is 4 bytes, and the four bytes are 32 bits. Therefore, the length of PHP is usually the 31 power of 2, one of them represents the symbol bit.

In PHP, you can use the system constant PHP_INT_MAX to obtain the maximum value. However, when the integer value exceeds the maximum value, it is automatically converted to the float type.

Note: PHP does not support unsigned numbers.

Floating point data type

The floating point data type is the decimal number. It is also called double precision or real number.

The character string length is also related to the platform. generally, it is 1.8e308 and has a 14-digit decimal number precision. The decimal precision is generally calculated from the first non-zero number on the left.

Echo 123456.78912345123; // output result 123456.78912345 -- the last 123 is rounded to echo 0000123456789.256486587441; // output result 123456789.25649 -- 6587441 rounded to the first 8 to 9

The comparison of floating point models should be handled according to different situations.

  1. If two floating point numbers are directly assigned values, you can directly compare them.

  2. If one or more of the two floating point numbers is the result of the operation, you need to use the abs or round function to expand the multiple and then compare.

Boolean data type

Boolean data type is true or false. However, when values of other data types are true or false.

The following conditions are regarded as false:

  • Integer value 0

  • Floating point value 0.0

  • Empty string and string '0'

  • Array that does not contain any elements

  • Objects that do not include any members

  • Special type null

  • SimpleXML object generated from the null tag.

String data type

The character string is composed of a series of characters with quotation marks, while the string in PHP can be up to 2 GB;

We often use strings during development, and the definition of strings can be roughly divided into four types:

  • Single quotes

  • Double quotation marks

  • Heredoc (double quotation marks)

  • Nowdoc (single quotes)

Single quotes: when a string defines single quotes, the parser does not parse the variables in the string. But directly output.

$ A = 12; $ B = 'this is a single quotation mark $ a'; echo $ B; ----- result ------ This is a single quotation mark $

Double quotation marks: the parser parses the variables in the string.

$ A = 12; $ B = "this is a single quotation mark $ a"; echo $ B; ----- result ------ This is a single quotation mark 12

Heredoc: When the defined string is too long, you can consider the method in the user. this method will parse the variables in the string.

$ A = 12; $ B = <
 
  

Note: EOP cannot contain spaces or other characters.

Nowdoc: This method does not parse the variables in the string.

$ A = 12; $ str = <'coo 'this is a nowDoc $ a; COO; echo $ str; ----- result ------ This is a nowDoc $;
Basic data type conversion

The   data type is converted from one type to another. Conversion can be divided into two types:Automatic type conversion and forced type conversion;

In general, var_dump () can be used when we do not know the data type. This function can print the specific information of the data, including the data type. The PHP help document provides a way to get data.GetType ()However, the document explicitly states that it is not recommended:

Scenarios for automatic type conversion:

If the integer value is too large, it is automatically converted to the floating point type.

$ A = PHP_INT_MAX; // represents the maximum value of an integer. var_dump ($ a); // The type of $ a = PHP_INT_MAX + 1 is output; // the value of $ a is greater than the maximum value of an integer. Var_dump ($ a); ----- result ------ int (2147483647) float (2147483648)

Automatic type conversion is often performed when numeric operations are performed.

$ A = 1; // Integer $ B = 3.14; // floating point $ res = $ a + $ B; // calculate the sum of the two numbers and var_dump ($ res ); // output type ----- result ------ float (4.14)
$ A = 10; // Integer $ B = 4; // Integer $ res = $ a/$ B; // remove var_dump ($ res ); ----- result ------ float (2.5)
Forced type conversion

  When we want to forcibly convert the current type to another type, we can use force type conversion:

Use bool settype (mixed &

Type)

$ A = 100; // integer settype ($ a, 'float'); // force type conversion var_dump ($ a); ----- result ------ float (100)

The possible value of $ type is:

  1. "Boolean" (or "bool", starting from PHP 4.2.0)

  2. "Integer" (or "int", starting from PHP 4.2.0)

  3. "Float" (only available after PHP 4.2.0, and "double" used in earlier versions is disabled now)

  4. "String"

  5. "Array"

  6. "Object"

  7. "Null" (from PHP 4.2.0)

Usage type

$ A = 200; $ B = (string) $ a; var_dump ($ B); ----- result ------ string (3) "200"

Use the function to obtain the corresponding data types, such as intval, floatval, boolval, and strval.

$ A = 12.923; // float type $ B = intval ($ a); var_dump ($ B); ----- result ------ int (12)

Note: When the floating point type is converted to an integer type, the number after the decimal point is automatically ignored and is not rounded down.

NULL data type

Optional values NULL: indicates that a variable has no value. The only possible value of the NULL type is NULL.

The variable is considered NULL in the following cases:

  • The value is NULL.

  • Not assigned

  • Unset ()

Other data types, arrays, objects, and resources will be described later.

Note: In PHP, when we want to output a number, we can use echo for output, while the string connection uses a vertex... You can also use var_dump () to output the current data type.

Summary

The basic data type of zookeeper makes it necessary for us to master. in future development, all the data we operate on will be the basic data type.

Content described in this section
  • Brief introduction to php

  • Four Tag formats of php

  • Php variables

  • Basic data types of php

Brief introduction to PHP

Zookeeper php is an open-source scripting language mainly used for web development. the syntax absorbs the features of C, Java, and perl, which is conducive to learning and widely used, compared with other programming languages, php embeds programs into HTML documents for execution, making dynamic pages made using PHP faster.

Four Tag formats of PHP

When the php code is written to a fixed format, the parser will execute the php code. There are four common PHP labels:

We generally use the first format. It is also the most common format.

PHP variables

Variable "Variable variables" indicates the number that can be changed without a fixed value.

Variable creation aims to store data for subsequent operations on data.

Naming rules for variable names: it must contain numbers, letters, and underscores and cannot start with a number.

Common naming rules:

  • Camper naming method: username-> userName: starts from the second word and starts with an upper-case letter.

  • Pascal's name method: username-> UserName: the first letter of each word is capitalized.

  • Underline naming rules: username> user_name: all words are in lower case. separate words with underscores:

In the future, the naming rules of function names can also be summarized into these three points.

PHP variable names have the following features: one must be added before the variable$Symbol, that is, shift + 4. for the convenience of development, the variable name should generally have meaning.

PHP basic data type

Zookeeper uses the data type statement in most languages. data types can classify the data we use in development for better management.

Basic data types of PHP:

  • Basic data types: integer (int/integer), float/double, boolean, and string)

  • Composite data type: array, object)

  • Special data types: null and resource)

Integer data type

The   integer data type is the common negative integer and positive integer;

The integer type in PHP can be octal (every eight to one), decimal, and hexadecimal (every sixteen to one ).

$ A = 2; // decimal $ a = 023; // octal $ a = 0x23; // hexadecimal

Octal: there is a 0 in front, which indicates octal.

Hexadecimal: There are two 0x in front, indicating the hexadecimal format.

The size of an integer is limited. The integer length of PHP is related to the platform. generally, it is 4 bytes, and the four bytes are 32 bits. Therefore, the length of PHP is usually the 31 power of 2, one of them represents the symbol bit.

In PHP, you can use the system constant PHP_INT_MAX to obtain the maximum value. However, when the integer value exceeds the maximum value, it is automatically converted to the float type.

Note: PHP does not support unsigned numbers.

Floating point data type

The floating point data type is the decimal number. It is also called double precision or real number.

The character string length is also related to the platform. generally, it is 1.8e308 and has a 14-digit decimal number precision. The decimal precision is generally calculated from the first non-zero number on the left.

Echo 123456.78912345123; // output result 123456.78912345 -- the last 123 is rounded to echo 0000123456789.256486587441; // output result 123456789.25649 -- 6587441 rounded to the first 8 to 9

The comparison of floating point models should be handled according to different situations.

  1. If two floating point numbers are directly assigned values, you can directly compare them.

  2. If one or more of the two floating point numbers is the result of the operation, you need to use the abs or round function to expand the multiple and then compare.

Boolean data type

Boolean data type is true or false. However, when values of other data types are true or false.

The following conditions are regarded as false:

  • Integer value 0

  • Floating point value 0.0

  • Empty string and string '0'

  • Array that does not contain any elements

  • Objects that do not include any members

  • Special type null

  • SimpleXML object generated from the null tag.

String data type

The character string is composed of a series of characters with quotation marks, while the string in PHP can be up to 2 GB;

We often use strings during development, and the definition of strings can be roughly divided into four types:

  • Single quotes

  • Double quotation marks

  • Heredoc (double quotation marks)

  • Nowdoc (single quotes)

Single quotes: when a string defines single quotes, the parser does not parse the variables in the string. But directly output.

$ A = 12; $ B = 'this is a single quotation mark $ a'; echo $ B; ----- result ------ This is a single quotation mark $

Double quotation marks: the parser parses the variables in the string.

$ A = 12; $ B = "this is a single quotation mark $ a"; echo $ B; ----- result ------ This is a single quotation mark 12

Heredoc: When the defined string is too long, you can consider the method in the user. this method will parse the variables in the string.

$ A = 12; $ B = <
   
    

Note: EOP cannot contain spaces or other characters.

Nowdoc: This method does not parse the variables in the string.

$ A = 12; $ str = <'coo 'this is a nowDoc $ a; COO; echo $ str; ----- result ------ This is a nowDoc $;
Basic data type conversion

The   data type is converted from one type to another. Conversion can be divided into two types:Automatic type conversion and forced type conversion;

In general, var_dump () can be used when we do not know the data type. This function can print the specific information of the data, including the data type. The PHP help document provides a way to get data.GetType ()However, the document explicitly states that it is not recommended:

Scenarios for automatic type conversion:

If the integer value is too large, it is automatically converted to the floating point type.

$ A = PHP_INT_MAX; // represents the maximum value of an integer. var_dump ($ a); // The type of $ a = PHP_INT_MAX + 1 is output; // the value of $ a is greater than the maximum value of an integer. Var_dump ($ a); ----- result ------ int (2147483647) float (2147483648)

Automatic type conversion is often performed when numeric operations are performed.

$ A = 1; // Integer $ B = 3.14; // floating point $ res = $ a + $ B; // calculate the sum of the two numbers and var_dump ($ res ); // output type ----- result ------ float (4.14)
$ A = 10; // Integer $ B = 4; // Integer $ res = $ a/$ B; // remove var_dump ($ res ); ----- result ------ float (2.5)
Forced type conversion

  When we want to forcibly convert the current type to another type, we can use force type conversion:

Use bool settype (mixed &

Type)

$ A = 100; // integer settype ($ a, 'float'); // force type conversion var_dump ($ a); ----- result ------ float (100)

The possible value of $ type is:

  1. "Boolean" (or "bool", starting from PHP 4.2.0)

  2. "Integer" (or "int", starting from PHP 4.2.0)

  3. "Float" (only available after PHP 4.2.0, and "double" used in earlier versions is disabled now)

  4. "String"

  5. "Array"

  6. "Object"

  7. "Null" (from PHP 4.2.0)

Usage type

$ A = 200; $ B = (string) $ a; var_dump ($ B); ----- result ------ string (3) "200"

Use the function to obtain the corresponding data types, such as intval, floatval, boolval, and strval.

$ A = 12.923; // float type $ B = intval ($ a); var_dump ($ B); ----- result ------ int (12)

Note: When the floating point type is converted to an integer type, the number after the decimal point is automatically ignored and is not rounded down.

NULL data type

Optional values NULL: indicates that a variable has no value. The only possible value of the NULL type is NULL.

The variable is considered NULL in the following cases:

  • The value is NULL.

  • Not assigned

  • Unset ()

Other data types, arrays, objects, and resources will be described later.

Note: In PHP, when we want to output a number, we can use echo for output, while the string connection uses a vertex... You can also use var_dump () to output the current data type.

Summary

The basic data type of zookeeper makes it necessary for us to master. in future development, all the data we operate on will be the basic data type.

The above is the content of the basic data type in PHP basic tutorial 2. For more information, see PHP Chinese website (www.php1.cn )!

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.