Data types in PHP

Source: Internet
Author: User
Tags comparison table php tutorial scalar type null

Data types in PHP

Four scalar types:
Boolean (boolean)
Integer)
Float (float, also called double)
String (string)
Two composite types:
Array)
Object)
There are two special types:
Resource)
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:
<? Php Tutorial
$ A_bool = TRUE; // a boolean
$ A_str = "foo"; // a string
$ A_str2 = 'foo'; // a string
$ An_int = 12; // an integer

Echo gettype ($ a_bool); // prints out: boolean
Echo gettype ($ a_str); // prints out: string

// If this is an integer, increment it by four
If (is_int ($ an_int )){
$ An_int + = 4;
}

// If $ bool is a string, print it out
// (Does not print out anything)
If (is_string ($ a_bool )){
Echo "String: $ a_bool ";
}
?>

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.
 
 
 
#
 
II. PHP Data types

Int zjj = 100; // This assignment is correct.

Int zjj = "hello, world"; // This compiler will prompt an error

In PHP, you do not need to declare the data type of the variable. You can assign values directly. See the following

$ Zjj = 100; // indicates that zjj is an integer.

$ Zjj = "hello, world" // indicates that zjj is of the string type.

The data types in PHP are divided into two types: scalar data type and compound data type.

Scalar data types include:

Boolean integer floating point (foat is also called double)

Composite data includes:

Array object)

There are two types of special data

Resource NULL)

1. Boolean is the simplest variable. Boolean variables: true and false

To set a Boolean variable, you only need to assign these two values to the variable. Even keywords in PHP can be used as variable names.

<? Php
$ True = "I am a variable". True; // This is a bit of a bit. Sometimes it is printed on a book.
Echo ($ True );
Echo ("<BR>"); // plays the role of carriage return
Echo ("I am a keyword". True)
?>

Execution result: I am variable 1 // Here true is 1

I am keyword 1

True and false actually represent values 1 and 0. Therefore, true is displayed as 1 in the output. However, when assigning values to variables, if the value is 1, it is treated as an integer, if it is assigned true, it is treated as a Boolean value.

When converted to Boolean, the following values are considered false

(1) Boolean value false

(2) integer value 0 (0)

(3) floating point value: 0.0 (0)

(4) blank string and string "0"

(5) arrays without member variables

(6) objects without cells (applicable only to php4)

(7) Special type NULL

All other values are considered TRUE to include any resource.

2. Integer

+ 2147483647 to-2147483647

 

<? Php
$ Int_D = 2147483647; // decimal value assignment
Echo ($ int_D );
Echo ("<br> ");
$ Int_H = 0x7FFFFFFF; // hexadecimal value assignment
Echo ($ int_H );
Echo ("<br> ");
$ Int_O = 017777777777; // octal value assignment
Echo ($ int_O );
Echo ("<BR> ");
?>

Execution result:

2147483647
2147483647
2147483647

If the specified number exceeds the inter range, it will be interpreted as float.

3. Floating point

1.7E-308 to 1.7E + 308

<? Php
$ Float_1 = 90000000000;
Echo ($ float_1 );
Echo ("<br> ");
$ Float_2 = 9E10;
Echo ($ float_2 );
Echo ("<br> ");
$ Float_3 = 9E + 10;
Echo ($ float_3 );
?>

Execution result:

90000000000
90000000000
90000000000

4. String

There are three methods to define strings in PHP

1. Single quotes.

$ Single_str = 'I am a personal ';

To output single quotes in browsing, you must add the escape character "" to the string.

<?
$ Single_str = 'I have been enclosed in single quotes! <Br> '; // normal output
Echo $ single_str;
$ Single_str = 'output single quotes: 'Hey, I'm in single quotes' <br> '; // you can use an escape character to output single quotes.
Echo $ single_str;
$ Single_str = 'output double quotation marks: "I'm in double quotation marks" <br> '; // normal output
Print $ single_str;
$ Single_str = 'dollar sign for losing $'; // normal output
Print $ single_str;
?>

Execution result:

I was enclosed in single quotes!
Output single quotes: 'Hey, I'm in single quotes'
Double quotation marks: "in double quotation marks"
Double Dollar sign: $

(2) double quotation marks

$ Double_Str = "I am a person"

To output various special characters in a string, escape characters can be used.

$ Double_Str = "output dollar sign: $ ";

$ Double_Str = "output Backslash :";

<?
$ Double_str = "I have been enclosed in double quotation marks! <Br> ";
Echo $ single_str;
$ Single_str = "output single quotes: 'Hey, I'm in single quotes '<br>"; // escape characters are not required
Echo $ single_str;
$ Single_str = "output double quotation marks:" I am in double quotation marks "<br>"; // escape characters are required.
Print $ single_str;
$ Single_str = "output dollar sign: $ <br>"; // escape character required
Print $ single_str;
$ Single_str = "output Backslash: <br>"; // escape character required
Print $ single_str;
 
?>

Execution result:

Output single quotes: 'Hey, I'm in single quotes'
Double quotation marks: "in double quotation marks"
Output dollar sign: $
Output Backslash:

(3) assign values to strings in Heredoc mode

<?
$ Zjj1_str = <zjj // = <This is equivalent to a link character and obtains data from zjj to zjj.
Hello <br>
Dollar signs $ <br> // both zjj1 and zjj can be named at will.
Backslash <br>
"I love you" <br>
'I hate you'
Zjj;
Echo $ zjj1_str;
?>

Execution result:

Hi!
Dollar sign $
Backslash
"I love you"
'I hate you'

(4) variables in the string

<? Php
$ Str_1 = "I am the variable value! ";
$ Str_2 = "str_1: $ str_1 <br>"; // double quotation mark string contains variable $ str_1
Echo $ str_2;

$ Str_1 = 'here is the variable value ';
$ Str_2 = 'Str _ 1: $ str_1 <br> '; // string value contained in single quotes
Echo $ str_2;

$ Str_1 = "I am the variable value! ";
$ Str_2 = "str_1: $ str_12 <br>"; // 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 <br>"; // after the referenced variable name, it contains two more characters, namely $ str_12.
Echo $ str_2;
?>

Execution result:

Str_1: I am the variable value!
Str_1: $ str_1
Str_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 directly outputs the value of the variable. 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 <br>"; if the variable is followed by a non-space character, it will confuse the compiler and treat $ str_12 as a variable, but it is not defined above, so a null value is 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
$ X = null; NULL TRUE FALSE
Var $ x; NULL TRUE FALSE
$ X is undefined NULL TRUE FALSE
$ X = array (); array TRUE FALSE
$ X = false; boolean TRUE FALSE
$ X = true; boolean FALSE TRUE
$ X = 1; integer FALSE TRUE
$ X = 42; integer FALSE TRUE
$ X = 0; integer TRUE FALSE
$ X =-1; integer FALSE TRUE
$ X = "1"; string FALSE TRUE
$ X = "0"; string TRUE FALSE
$ X = "-1"; string FALSE TRUE
$ X = "php"; string FALSE TRUE
$ X = "true"; string FALSE TRUE
$ X = "false"; string FALSE TRUE
 
Loose comparison =
True false 1 0-1 "1" "0" "-1" NULL array () "php """
TRUE FALSE
FALSE TRUE FALSE
1 TRUE FALSE
0 FALSE TRUE
-1 TRUE FALSE
"1" TRUE FALSE
"0" FALSE TRUE FALSE
"-1" TRUE FALSE
NULL FALSE TRUE
Array () FALSE TRUE FALSE
"Php" TRUE FALSE
"" FALSE TRUE
 
Strict comparison =
True false 1 0-1 "1" "0" "-1" NULL array () "php """
TRUE FALSE
FALSE TRUE FALSE
1 FALSE TRUE FALSE
0 FALSE TRUE FALSE
-1 FALSE TRUE FALSE
"1" FALSE TRUE FALSE
"0" FALSE TRUE FALSE
"-1" FALSE TRUE FALSE
NULL FALSE TRUE FALSE
Array () FALSE TRUE FALSE
"Php" FALSE TRUE FALSE
"" FALSE TRUE

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.