PHP Data type (1) _ PHP Tutorial

Source: Internet
Author: User
Tags type null
Data type in PHP (1 ). PHP supports eight primitive types. Four scalar types: boolean (integer) float (float) (floating point number, also known as "double") string (string). PHP supports eight primitive types.

Four scalar types:

Boolean)

Integer)

Float ")

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)

Feedback (callback)

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: If you want to view the value and type of an expression, use var_dump ().

Note: 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$bool = TRUE;  // a boolean$str  = "foo";  // a string$int  = 12;    // an integerecho gettype($bool); // prints out "boolean"echo gettype($str);  // prints out "string"// If this is an integer, increment it by fourif (is_int($int)) {   $int += 4;}// If $bool is a string, print it out// (does not print out anything)if (is_string($bool)) {   echo "String: $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.

Boolean

This is the simplest type. Boolean represents the TRUE value, which can be TRUE or FALSE.

Note: The Boolean type is introduced in PHP 4.

Syntax

To specify a Boolean value, use the keyword TRUE or FALSE. Both are case insensitive.

<?php$foo = True; // assign the value TRUE to $foo?>

You usually use some operators to return boolean values and pass them to process control.

// == is an operator which test// equality and returns a booleanif ($action == "show_version") {   echo "The version is 1.23";}// this is not necessary...if ($show_separators == TRUE) {   echo "n";}// ...because you can simply typeif ($show_separators) {   echo "n";} 

Convert to a Boolean value

To convert a value to a boolean value explicitly, use (bool) or (boolean) to forcibly convert the value. However, in many cases, forced conversion is not required, because this value is automatically converted when an operator, function, or flow control requires a boolean parameter.

See type tricks.

When converted to boolean, the following values are considered FALSE:

Boolean value FALSE

Integer value 0 (0)

Floating point value: 0.0 (0)

Blank string and string "0"

Array without member variables

Objects without cells

Special type NULL (including unset variables)

All other values are considered to be TRUE (including any resources ).

<?phpecho gettype((bool) "");        // bool(false)echo gettype((bool) 1);        // bool(true)echo gettype((bool) -2);        // bool(true)echo gettype((bool) "foo");    // bool(true)echo gettype((bool) 2.3e5);    // bool(true)echo gettype((bool) array(12)); // bool(true)echo gettype((bool) array());  // bool(false)?> 

1

The http://www.bkjia.com/PHPjc/446741.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446741.htmlTechArticlePHP supports eight primitive types. Four scalar types: boolean (integer) float (floating point) (floating point, also known as "double") string (string) two 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: 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.