Basic data types for PHP basic tutorial Two

Source: Internet
Author: User

What this section explains

    • Simple Introduction to PHP

    • PHP's four label formats

    • Variables for PHP

    • PHP's underlying data type

Simple Introduction to PHP

PHP is an open-source scripting language, mainly used for web development, grammar absorbs the C language, Java and Perl features, conducive to learning, the use of a wide range of PHP-made dynamic pages compared with other programming languages, PHP is to embed the program into the HTML document to execute, execution faster.

PHP's four label formats

When PHP's code is written in a fixed format, the parser executes the PHP code, and there are four common PHP tags:

We typically use the first format. is also the most common format.

PHP variables

Variable: Refers to a number that can be changed without a fixed value.

The purpose of creating variables is to store the data so that it can be manipulated later.

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

Common Naming conventions:

    • Camel-named: username->>>username: Start with the second word, capitalize the first letter

    • Pascal's nomenclature: Username->>>username: Capitalize the first letter of each word

    • Underline naming: Username->>>user_name: Words are lowercase, and words are underlined to separate common naming rules:

The naming conventions for subsequent function names can also be summed up in these three points.

PHP variable name has a feature: The variable must be preceded by a $ symbol, that is, SHIFT + 4, and for the sake of development convenience, the name of the variable generally have meaning.

PHP Basic Data types

Most language languages have data types, and data types can classify the data we use in development and manage it better.

Basic data type classification for PHP:

    • Base data type: Integer (Int/integer), float (float/double), Boolean (Bool/boolean), string

    • Composite data types: Arrays (array), objects (object)

    • Special data type: null (NULL), resource (Resource)

Integer data type

The integer data type is the negative integer that we normally use, positive integer, etc.

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

$a = 2; Decimal $ A = 023;//octal $ A = 0x23;//hex

Octal: There is a 0 in front, which means octal.

Hex: Preceded by two 0x, which indicates hexadecimal.

The size of an integral type has a limit. While the length of the integer type of PHP and the platform is generally 4 bytes, and 4 bytes is 32 bits, so the average PHP is 2 31 times, one of which represents the sign bit.

The maximum value can be obtained in PHP using the system constant Php_int_max. However, when the value of an integer exceeds the maximum value, it is automatically converted to the float type.

Note: Unsigned numbers are not supported in PHP.

Floating-point data types

Floating-point data types are the decimals we usually say. Also known as double precision or real number.

The word length of a string is also related to the platform, typically 1.8e308, and has a precision of 14 decimal digits. The precision of the decimal is generally calculated from the first number that is not a 0 from the left.

echo 123456.78912345123;//output 123456.78912345--at the back of the 1234, the Echo 0000123456789.256486587441;// Output results 123456789.25649--6587441 Rounding forward a 8 change 9

For the comparison of floating-point type, we should handle it according to different situations.

    1. If two floating-point numbers are directly assigned, they can be compared directly.

    2. If there is one or more of the two floating-point numbers, it is necessary to use the ABS or round function to enlarge the multiplier after comparison.

Boolean Data types

The Boolean data type is also true (true) or False (false), but in addition to these two forms, when the values of other data types are in a certain condition there is a true and false.

The situation will be false:

    • Integer value 0

    • Floating-point value 0.0

    • empty string and string ' 0 '

    • An array that does not include any elements

    • Objects that do not include any members

    • Special type NULL

    • The SimpleXML object generated from an empty tag.

String data type

A string is a series of characters enclosed in quotation marks, while the maximum string in PHP can make 2GB;

In development we often use strings, and about the way the string is defined, can be broadly divided into four kinds:

    • Single quotation marks

    • Double quotes

    • Heredoc (double quotes)

    • Nowdoc (single quote)

Single quotation marks: When a string defines a single quotation mark, the parser does not parse the variables in the string. Instead, it outputs directly.

$a = n; $b = ' This is a single quote $ A '; echo $b;-----result------This is a single quote $ A

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

$a = n; $b = "This is a single quote $ A"; echo $b;-----result------This is a single quote 12

Heredoc: When you define a string that is too long, consider the way in which the variables in the string are parsed.

$a = n; $b = <<<eop//the definition of this capital letter can be arbitrary. This is a heredoc of type $ A; Eop;echo $b;-----result------This is a type of heredoc 12;

Note: There can be no spaces or any other characters before or after EOP

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

$a = n; $str = <<< ' COO '    this is a nowdoc$a; Coo;echo $str;-----result------this is a nowdoc$a;

Basic Data type Conversions

Data types are conversions from one type to another. Conversion can be divided into two types of automatic type conversion, forced type conversion ;

In general, Var_dump () can be used when we do not know what type of data it is, and this function can print specific information about the data, including the type of data. The PHP Help document also provides a way to get the type of data GetType () But the document clearly indicates that it is not recommended:

Automatic type conversion

As described earlier, when the data of an integer crosses its range, it is automatically converted to a floating-point type. This type of auto-complete conversion is automatic type conversion.
Scenarios for automatic type conversions:

The integer number is too large to automatically convert to floating point type.

$a = php_int_max;//represents the maximum value var_dump ($a) of the integer type; Output $ A of type $ A = Php_int_max + 1; The value of the $a is greater than the maximum value of the integer type. Var_dump ($a);-----result------Int (2147483647) float (2147483648)

Automatic type conversions occur frequently when numeric operations are performed.

$a = 1; Integral type $b = 3.14; Floating-point $res = $a + $b; Calculates the and var_dump of two numbers ($res); Output type-----result------float (4.14)
$a = 10;//integral type $b = 4;//integer $res = $a/$b; Divide var_dump ($res);-----results------float (2.5)

Forcing type conversions

You can use forced type conversions when we want to force the current type to be converted to another type:

using bool Settype (mixed &

Type)

$a = 100; Integral type Settype ($a, ' float ');//coercion type conversion var_dump ($a);-----result------float (100)

The possible values for the $type are:

    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, for "double" used in older versions is now disabled)

    4. "String"

    5. "Array"

    6. "Object"

    7. "Null" (from PHP 4.2.0)

Usage type

$a = n; $b = (string) $a; Var_dump ($b);-----result------String (3) "200"

Use the function to get the corresponding data type, such as Intval, Floatval, Boolval, Strval

$a = 12.923;//floating-point $b = Intval ($a); Var_dump ($b);-----result------Int (12)

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

Null data type

Null: Indicates that a variable has no value, and the only possible value of the null type is NULL.

A variable is considered NULL in the following cases:

    • is assigned a value of NULL

    • has not been assigned a value

    • by Unset ()

Other data types, arrays, objects, and resources are described later.

Special note : in PHP when we want to output a number, we can use echo to output, and the string connection using a point . Said. You can also output by Var_dump (), var_dump () can output the type of the current data.

Summarize

The basic data types are what we have to master, and in future development, the data we manipulate is the basic data type.

What this section explains

    • Simple Introduction to PHP

    • PHP's four label formats

    • Variables for PHP

    • PHP's underlying data type

Simple Introduction to PHP

PHP is an open-source scripting language, mainly used for web development, grammar absorbs the C language, Java and Perl features, conducive to learning, the use of a wide range of PHP-made dynamic pages compared with other programming languages, PHP is to embed the program into the HTML document to execute, execution faster.

PHP's four label formats

When PHP's code is written in a fixed format, the parser executes the PHP code, and there are four common PHP tags:

We typically use the first format. is also the most common format.

PHP variables

Variable: Refers to a number that can be changed without a fixed value.

The purpose of creating variables is to store the data so that it can be manipulated later.

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

Common Naming conventions:

    • Camel-named: username->>>username: Start with the second word, capitalize the first letter

    • Pascal's nomenclature: Username->>>username: Capitalize the first letter of each word

    • Underline naming: Username->>>user_name: Words are lowercase, and words are underlined to separate common naming rules:

The naming conventions for subsequent function names can also be summed up in these three points.

PHP variable name has a feature: The variable must be preceded by a $ symbol, that is, SHIFT + 4, and for the sake of development convenience, the name of the variable generally have meaning.

PHP Basic Data types

Most language languages have data types, and data types can classify the data we use in development and manage it better.

Basic data type classification for PHP:

    • Base data type: Integer (Int/integer), float (float/double), Boolean (Bool/boolean), string

    • Composite data types: Arrays (array), objects (object)

    • Special data type: null (NULL), resource (Resource)

Integer data type

The integer data type is the negative integer that we normally use, positive integer, etc.

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

$a = 2; Decimal $ A = 023;//octal $ A = 0x23;//hex

Octal: There is a 0 in front, which means octal.

Hex: Preceded by two 0x, which indicates hexadecimal.

The size of an integral type has a limit. While the length of the integer type of PHP and the platform is generally 4 bytes, and 4 bytes is 32 bits, so the average PHP is 2 31 times, one of which represents the sign bit.

The maximum value can be obtained in PHP using the system constant Php_int_max. However, when the value of an integer exceeds the maximum value, it is automatically converted to the float type.

Note: Unsigned numbers are not supported in PHP.

Floating-point data types

Floating-point data types are the decimals we usually say. Also known as double precision or real number.

The word length of a string is also related to the platform, typically 1.8e308, and has a precision of 14 decimal digits. The precision of the decimal is generally calculated from the first number that is not a 0 from the left.

echo 123456.78912345123;//output 123456.78912345--at the back of the 1234, the Echo 0000123456789.256486587441;// Output results 123456789.25649--6587441 Rounding forward a 8 change 9

For the comparison of floating-point type, we should handle it according to different situations.

    1. If two floating-point numbers are directly assigned, they can be compared directly.

    2. If there is one or more of the two floating-point numbers, it is necessary to use the ABS or round function to enlarge the multiplier after comparison.

Boolean Data types

The Boolean data type is also true (true) or False (false), but in addition to these two forms, when the values of other data types are in a certain condition there is a true and false.

The situation will be false:

    • Integer value 0

    • Floating-point value 0.0

    • empty string and string ' 0 '

    • An array that does not include any elements

    • Objects that do not include any members

    • Special type NULL

    • The SimpleXML object generated from an empty tag.

String data type

A string is a series of characters enclosed in quotation marks, while the maximum string in PHP can make 2GB;

In development we often use strings, and about the way the string is defined, can be broadly divided into four kinds:

    • Single quotation marks

    • Double quotes

    • Heredoc (double quotes)

    • Nowdoc (single quote)

Single quotation marks: When a string defines a single quotation mark, the parser does not parse the variables in the string. Instead, it outputs directly.

$a = n; $b = ' This is a single quote $ A '; echo $b;-----result------This is a single quote $ A

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

$a = n; $b = "This is a single quote $ A"; echo $b;-----result------This is a single quote 12

Heredoc: When you define a string that is too long, consider the way in which the variables in the string are parsed.

$a = n; $b = <<<eop//the definition of this capital letter can be arbitrary. This is a heredoc of type $ A; Eop;echo $b;-----result------This is a type of heredoc 12;

Note: There can be no spaces or any other characters before or after EOP

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

$a = n; $str = <<< ' COO '    this is a nowdoc$a; Coo;echo $str;-----result------this is a nowdoc$a;

Basic Data type Conversions

Data types are conversions from one type to another. Conversion can be divided into two types of automatic type conversion, forced type conversion ;

In general, Var_dump () can be used when we do not know what type of data it is, and this function can print specific information about the data, including the type of data. The PHP Help document also provides a way to get the type of data GetType () But the document clearly indicates that it is not recommended:

Automatic type conversion

As described earlier, when the data of an integer crosses its range, it is automatically converted to a floating-point type. This type of auto-complete conversion is automatic type conversion.
Scenarios for automatic type conversions:

The integer number is too large to automatically convert to floating point type.

$a = php_int_max;//represents the maximum value var_dump ($a) of the integer type; Output $ A of type $ A = Php_int_max + 1; The value of the $a is greater than the maximum value of the integer type. Var_dump ($a);-----result------Int (2147483647) float (2147483648)

Automatic type conversions occur frequently when numeric operations are performed.

$a = 1; Integral type $b = 3.14; Floating-point $res = $a + $b; Calculates the and var_dump of two numbers ($res); Output type-----result------float (4.14)
$a = 10;//integral type $b = 4;//integer $res = $a/$b; Divide var_dump ($res);-----results------float (2.5)

Forcing type conversions

You can use forced type conversions when we want to force the current type to be converted to another type:

using bool Settype (mixed &

Type)

$a = 100; Integral type Settype ($a, ' float ');//coercion type conversion var_dump ($a);-----result------float (100)

The possible values for the $type are:

    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, for "double" used in older versions is now disabled)

    4. "String"

    5. "Array"

    6. "Object"

    7. "Null" (from PHP 4.2.0)

Usage type

$a = n; $b = (string) $a; Var_dump ($b);-----result------String (3) "200"

Use the function to get the corresponding data type, such as Intval, Floatval, Boolval, Strval

$a = 12.923;//floating-point $b = Intval ($a); Var_dump ($b);-----result------Int (12)

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

Null data type

Null: Indicates that a variable has no value, and the only possible value of the null type is NULL.

A variable is considered NULL in the following cases:

    • is assigned a value of NULL

    • has not been assigned a value

    • by Unset ()

Other data types, arrays, objects, and resources are described later.

Special note : in PHP when we want to output a number, we can use echo to output, and the string connection using a point . Said. You can also output by Var_dump (), var_dump () can output the type of the current data.

Summarize

The basic data types are what we have to master, and in future development, the data we manipulate is the basic data type.

The above is the basic data type of PHP basic tutorial two content, more relevant content please pay attention to topic.alibabacloud.com (www.php.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.