Summary of eight basic data types in PHP

Source: Internet
Author: User
Tags comparison table
PHP supports eight basic data types. For more information about php learning, see.

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 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, 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,NoUse gettype () andIs_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 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.

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: 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.