Type in PHP-php Tutorial

Source: Internet
Author: User
Tags type null
PHP type overview

PHP supports eight original 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 (no 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.

Variable types

Var_dump () prints information about a variable.

Gettype (): Get the variable type

  string(1) "a" [1]=> string(1) "b" }var_dump($f); // object(foo)#1 (0) { }var_dump(fn); // string(2) "fn"echo '
';echo gettype($a); // booleanecho gettype($b); // integerecho gettype($c); // doubleecho gettype($d); // stringecho gettype($e); // arrayecho gettype($f); // objectecho gettype(fn); // string?>

Do not use gettype () to test a type, because the returned string may need to be changed in future versions. In addition, because it contains a string comparison, it runs slowly. Use the is _ * function instead.

Is_bool () is_integer () is_float () is_numeric () is_string () is_scalar () is_array () is_object () is_resource () is_null () function_exists () // whether the function has method_exists () // whether the method of an object exists
Boolean type

TRUE and FALSE are case insensitive.

Automatic type conversion to Boolean

When the automatic type is converted to boolean, the following values are considered FALSE.

Boolean value FALSE integer value 0 (0) float type value 0.0 (0) null string, and the string "0" does not include any element array does not include any member variable object (only applicable to PHP 4.0) Special type NULL (including unassigned variables) simpleXML object generated from null tag
Integer

To use the octal expression, add 0 (zero) before the number)
To use a hexadecimal expression, you must add 0x before the number.
To use binary expression, add 0 B before the number.

Integer expressed in binary format is available from PHP 5.4.0.

The length of the integer is related to the platform, although the maximum value is usually about 2 billion (32-bit signed ). The maximum value of a 64-bit platform is usually about 9E18.

PHP does not support unsigned integers

The length of the Integer value can be expressed by the constant PHP_INT_SIZE. after PHP 4.4.0 and PHP 5.0.5, the maximum value can be expressed by the constant PHP_INT_MAX.

Integer overflow

If a given number exceeds the integer range (that is, an integer overflow occurs), it is interpreted as float. Similarly, if the execution result exceeds the integer range, float is returned.

Automatic type conversion to integer type

Convert from Boolean
FALSE will generate 0 (zero), TRUE will generate 1 (one)

From floating point type conversion
When the value is converted from a floating point to an integer, the value is rounded down.

Convert from string

If the string does not contain '.', 'e', or 'e' and its numeric value is within the integer range (defined by PHP_INT_MAX), the string is treated as an integer. All other cases are taken as float values.

 
Float

Length of floating point and platform-related

Rational numbers that can be accurately expressed in decimal form, such as 0.1 or 0.7. no matter how many tails are there, they cannot be accurately expressed by the binary used internally, therefore, the binary format cannot be converted without a slight loss of precision. This will lead to chaotic results: for example, floor (0.1 + 0.7) * 10) usually returns 7 instead of 8 in expectation, because the internal representation of this result is similar to 7.9999999999999991118...
Therefore, never believe that the result of a floating point number is accurate to the last digit, or compare whether two floating point numbers are equal.

If higher precision is required, use any precision mathematical function or GMP function.

NAN

Some mathematical operations produce a result represented by the constant NAN. This result represents an undefined or undefined value in the floating point operation.

We should not compare NAN with other values, including itself. we should use is_nan () to check

String

A string can be expressed in four ways.

Double quotation marks heredoc syntax structure nowdoc syntax structure (from PHP 5.3.0)

The character in string can be accessed and modified by a subscript starting from 0 and containing numbers in square brackets similar to the array structure.

Heredoc, Nowdoc

The Heredoc structure is like a double quotation mark string without double quotation marks.

The identifier referenced at the end must be in the first column of the row, and the name of the identifier must follow the PHP rules like other labels: only letters, numbers, and underscores are allowed, it must start with a letter or underscore.

$ Str = <
    
  

The Nowdoc structure is like a single quotation mark string without single quotation marks.

$str = <<<'EOD'Example of stringspanning multiple linesusing nowdoc syntax.EOD;
Escape

If the string is enclosed in single quotes ('), only single quotes (') and backslash (\) can be escaped (\)

If the string is enclosed in double quotation marks ("), PHP will parse the following special characters

Sequence meaning
\ N Line feed (LF or 0x0A (10) in the ASCII character set ))
\ R Enter (CR or 0x0D (13) in the ASCII character set ))
\ T Horizontal Tab (HT or 0x09 (9) in the ASCII character set ))
\ V Vertical Tab (VT or 0x0B (11) in the ASCII character set) (from PHP 5.2.5)
\ E Escape (ESC or 0x1B (27) in the ASCII character set) (from PHP 5.4.0)
\ F Form Feed (FF or 0x0C (12) in the ASCII character set) (since PHP 5.2.5)
\\ Backslash
\ $ Dollar tag
\" Double quotation marks
\ [0-7] {1, 3} A character that matches the regular expression sequence in octal format
\ X [0-9A-Fa-f] {1, 2} A character in hexadecimal format that matches the regular expression sequence


Like a single quotation mark string, escaping any other character will cause the backslash to be displayed.

Curly brackets syntax

Because {cannot be escaped, it is recognized only when $ is next to {. it cannot contain spaces and must be enclosed by double quotation marks (it cannot be used outside double quotation marks)

$ X = 'hello'; $ y = 'world'; $ str = "Hi, {$ x}"; // $ str = 'Hi, {$ x }'; // invalid // $ str = "Hi, {$ x }". {$ y}; // invalid
Automatic type conversion to string

The TRUE value of a boolean value is converted to "1" of the string ". The FALSE value of Boolean is converted to "" (null string)

An integer or floating point float is converted to a numeric literal string (including the exponent part of float)

Array Array is always converted to the string "array". Therefore, echo and print cannot display the content of this Array.

The Object cannot be converted to a string, starting from PHP 5. You can use the _ toString method when appropriate.

String functions

Http://php.net/manual/zh/ref.strings.php

String Encoding

The string is encoded according to the same encoding method of the script file. The function that operates the text must assume how the string is encoded. Unfortunately, PHP has many variants of this function.

Some functions assume that strings are single-byte encoded, but do not need to be interpreted as specific characters. For example, substr (), strpos (), strlen (), and strcmp (). Another way to understand these functions is that they act on the memory buffer, that is, they are operated by byte and byte subscript.
Some functions are passed into the string encoding method, or this information may be assumed by default. For example, most functions in htmlentities () and mbstring extensions.

Other functions use the current region (see setlocale (), but operate by byte. For example, strcasecmp (), strtoupper (), and ucfirst (). This means that these functions can only be used for single-byte encoding, and the encoding must match the region. For example, strtoupper ("á") returns "á" when the region is set correctly and is a single-byte encoding ". If it is encoded with a UTF-8, the correct results are not returned, and the results may return corrupt values based on the current region.

The final functions would assume that the string is encoded in a specific way, typically UTF-8. Most functions of intl extension and PCRE extension (in the above example only when u modifier is used) are like this. Although this is for its special purpose, utf8_decode () assumes the UTF-8 encoding while utf8_encode () assumes the ISO-8859-1 encoding.

Programs that correctly use Unicode depend on functions that may damage data and, if necessary, use the multi-byte string function extended by mbstring.

Resource type

Resource is a special variable that stores a reference to an external resource, such as a special handle for opening a file, connecting to a database, and drawing a canvas area.

Because the PHP 4 Zend Engine introduces a reference counting system, it can automatically detect that a resource is no longer referenced (the same as Java ). In this case, all external resources used by the resource are released by the garbage collection system. Therefore, it is rare to manually release the memory.

Persistent database connections are special and will not be destroyed by the garbage collection system.

It is meaningless to convert other types of values to resources.

NULL

A special NULL value indicates that a variable has no value. The NULL type has only one value, that is, the case-insensitive constant NULL.

The variable is considered NULL in the following cases:

The value is NULL. Not assigned. Unset ().
Forced type conversion

Mandatory conversions allowed include

(Int), (integer)-converted to integer (bool), (boolean)-converted to boolean type boolean (float), (double), (real) -convert to float (string)-convert to string (array)-convert to array (object)-convert to object (unset)-convert to NULL (PHP 5)

Use the settype () function

// You can set the following type: "boolean" (or "bool", starting from PHP 4.2.0) "integer" (or "int", starting from PHP 4.2.0) "float" (only available after PHP 4.2.0, and "double" used in earlier versions is disabled now) "string" "array" "object" "null" (from PHP 4.2.0)

There are also some types of conversion functions boolval (), intval (), floatval (), strval ()

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.