PHP eight basic data types summary _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags comparison table
Summary of eight basic data types in PHP. Four scalar types: boolean (boolean) integer (integer) float (float type, also known as double) string (string) two composite types: array (array) object (pair

Four scalar types:

  • Boolean(Boolean)
  • Integer(Integer)
  • Float(Float type, also knownDouble)
  • 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 pseudo variables$....

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, useVar_dump ().

If you want to use a simple expression for debugging, useGettype (). To view a type,NoUseGettype ()And useIs _TypeFunction. The following are examples:

The code is as follows:


$ 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 orSettype ()Function.

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.

Struct boolean (boolean) integer (integer) float (float type, also known as double) string (string) two composite types: array (array) object (pair...

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.