{Php Data type}

Source: Internet
Author: User
Tags comparison table
{Php Data type} PHP supports eight basic data types.

Four scalar types:

Boolean (boolean) integer (integer) float (float type, also known as double) string (string)

Two composite types:

Array (array) object (object)

There are two special types:

Resource (resource) NULL (NULL)

To make sure the code is easy to understand, this manual also introduces some pseudo types:

Mixed number callback

And the pseudo variable $ ....

You may also read some references about the double type. In fact, double and float are the same. for some historical reasons, these two names exist at the same time.

The type of a variable is generally not set by the programmer. to be exact, it is determined by PHP at runtime based on the context used by the variable.

Note: To view the value and type of an expression, use var_dump ().

If you just want to get an easy-to-understand expression of the type for debugging, use gettype (). To view a type, use the is_type function instead of gettype. The following are examples:

                            


If you want to forcibly convert a variable to a certain type, you can use the force conversion or settype () function for it.

Note that the variable will show different values in specific situations based on its current type. For more information, see Type tricks. In addition, you can refer to the PHP type comparison table to see examples of comparison between different types.

#

              

2. PHP Data type Int zjj = 100; // This assignment is correct Int zjj = "hello, world"; // This compiler will prompt the error in PHP, you do not need to declare the data type of the variable. you can assign values directly. The following $ zjj = 100; // indicates that zjj is an integer $ zjj = "hello, world" // indicates that zjj is the data type in string PHP and is divided into two types: one is the scalar data type, and the other is the composite data type. Scalar data types are: boolean (boolean) integer (integer) floating point (foat is also called double) composite data include: array (array) object (object) there are two special types of data resources (resource) NULL values (NULL) 1. Boolean is the simplest variable. Boolean variables: true and false must be set with a Boolean variable, you only need to assign these two values to the variable. Even keywords in PHP can be used as variable names. "); // Echo (" I am a keyword ". True) for the carriage return function?> Execution result: I am variable 1 // Here true is 1 I am the keywords 1true and false actually represent the numbers 1 and 0, so true is displayed as 1 in the output, however, when assigning a value to a variable, if the value is 1, it is treated as an integer. if the value is true, it is treated as a boolean. when it is converted to a Boolean value, the following values are regarded as false (1) Boolean values false (2) integer values 0 (0) (3) floating point values 0. 0 (0) (4) blank string and string "0" (5) arrays without member variables (6) objects without cells (applicable only to php4) (7) the special type is NULL, and all other values are considered TRUE to include any resource. 2. integer + 2147483647 to-2147483647 "); $ Int_H = 0x7fffff; // hexadecimal value assignment echo ($ int_H); echo ("
"); $ Int_O = 017777777777; // octal values echo ($ int_O); echo ("
");?> Execution result: 2147483647 2147483647 2147483647 if a specified number exceeds the inter range, it will be interpreted as float 3, float type 1. 7E-308 to 1.7E + 308 "); $ Float_2 = 9E10; echo ($ float_2); echo ("
"); $ Float_3 = 9E + 10; echo ($ float_3);?> Execution result: 90000000000 90000000000 90000000000 4. there are three methods to define string 1 and single quotation marks in string PHP. $ Single_str = 'I am a personal'; to output single quotes in browsing, you must add the escape character "\" to the string. '; // Normal output echo $ single_str; $ single_str = 'output single quotation marks: \' Hey, I'm in single quotation marks \'
'; // The escape character can be used to output single quotation marks echo $ single_str; $ single_str =' output double quotation marks: "I'm in double quotation marks"
'; // Print $ single_str for normal output; $ single_str =' dollar pair: $ '; // print $ single_str for normal output;?> Execution result: I was enclosed in single quotes! Output single quotes: 'Hey, I'm in single quotes 'output double quotes: "I'm in double quotes" double quotation marks: $ (2) double quotation marks $ Double_Str = "I am a person" to output various special characters in a string, you can use the escape character $ Double_Str = "output dollar sign: \ $ "; $ Double_Str = "output Backslash :\\"; "; Echo $ single_str; $ single_str =" output single quotes: 'Hey, I'm in single quotes'
"; // The escape character echo $ single_str; $ single_str =" output double quotation marks: \ "I'm in double quotation marks \" is not required \"
"; // Escape character print $ single_str; $ single_str =" output dollar sign: \ $
"; // Escape character print $ single_str; $ single_str =" output Backslash :\\
"; // Escape character print $ single_str;?> Execution result: output single quotes: 'Hey, I'm in single quotes 'output double quotes: "I'm in double quotes" output dollar sign: $ output Backslash: \ (3) assign values to strings in Heredoc mode Dollar signs $ <br> // both zjj1 and zjj can be named at will. Backslash \ <br> "I love you" <br> 'hate you 'zjj; echo $ zj1_str;?> Execution result: Hello dollar sign $ backslash \ "I love you" 'I hate you' (4), variable in the string "; // The double quotation mark string contains the variable $ str_1 echo $ str_2; $ str_1 = 'My variable value'; $ str_2 = 'str_1: $ str_1
'; // The string value in single quotes echo $ str_2; $ str_1 = "I am the variable value! "; $ Str_2 =" str_1: $ str_12
"; // After the referenced variable name, it contains two more characters, namely $ str_12 echo $ str_2; $ str_1 =" I am the variable value! "; $ Str_2 =" str_1: $ {str_1} 2
"; // After the referenced variable name, it contains two more characters: $ str_12 echo $ str_2;?> Execution result: str_1: the variable value! Str_1: $ str_1str_1: str_1: I am the variable value! 2 from the output, we can see that the double quotation mark string is the meaning of the value assignment, and the value of the variable is directly output. The single quotation mark string contains the variable name, which can be understood as the result of running the statement directly, and directly outputs the variable name instead of the variable value. $ Str_2 = "str_1: $ str_12
"; If the variable is followed by a non-space character, it will confuse the compiler and regard $ str_12 as a variable, which is not defined above, so a null value will be output. To solve this problem, use $ {str_1} 2 or {$ str_1} 2.

Use the PHP function to compare the variable $ x Expression gettype () empty () is_null () isset () boolean: if ($ x)
$ X = ""; String TRUE FALSE TRUE FALSE
$ X = null; NULL TRUE TRUE FALSE FALSE
Var $ x; NULL TRUE TRUE FALSE FALSE
$ X is undefined NULL TRUE TRUE FALSE FALSE
$ X = array (); Array TRUE FALSE TRUE FALSE
$ X = false; Boolean TRUE FALSE TRUE FALSE
$ X = true; Boolean FALSE FALSE TRUE TRUE
$ X = 1; Integer FALSE FALSE TRUE TRUE
$ X = 42; Integer FALSE FALSE TRUE TRUE
$ X = 0; Integer TRUE FALSE TRUE FALSE
$ X =-1; Integer FALSE FALSE TRUE TRUE
$ X = "1 "; String FALSE FALSE TRUE TRUE
$ X = "0 "; String TRUE FALSE TRUE FALSE
$ X = "-1 "; String FALSE FALSE TRUE TRUE
$ X = "php "; String FALSE FALSE TRUE TRUE
$ X = "true "; String FALSE FALSE TRUE TRUE
$ X = "false "; String FALSE FALSE TRUE TRUE

Loose comparison = True false 1 0-1 "1" "0" "-1" NULL array () "php """
TRUE TRUE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE
FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE
1 TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
0 FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE TRUE
-1 TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
"1" TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
"0" FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
"-1" TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
NULL FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE
Array () FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
"Php" TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
"" FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE

Strict comparison = True false 1 0-1 "1" "0" "-1" NULL array () "php """
TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
1 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
0 FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
-1 FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
"1" FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
"0" FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
"-1" FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
NULL FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
Array () FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
"Php" FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
"" FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE

#

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.