Types in PHP

Source: Internet
Author: User
Tags type null
Overview

PHP supports 8 types of raw data

Four types of scalar:

Boolean (Boolean) integer (integer) float (floating-point, also called Double) string (string)

Two kinds of composite types:

Array (Array) object (object)

Finally, there are two special types:

Resource (Resource) NULL (no type)

In fact double and float are the same, because of some historical reasons, both names exist at the same time.

The type of the variable is usually not set by the programmer, but rather is determined by the context in which PHP is used according to the variable.

Detecting variable types

Var_dump () printing information about variables

GetType () Gets the type of the variable

 
  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 for a type, because the string it returns may need to be changed in a future release. In addition, it is slower to run because it contains a comparison of strings. 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 ()//function exists method_exists ()///The method of an object exists
Boolean Boolean type

TRUE and FALSE are case insensitive

Automatic type conversion to a Boolean value

When an automatic type is converted to Boolean, the following values are considered FALSE

Boolean value FALSE itself integer value 0 (0) floating-point value 0.0 (0) empty string, and the string "0" does not include any element of an array that does not include any member variable objects (PHP 4.0 applies only) special type null (including variables not yet assigned) SimpleXML object generated from empty tag
Integer integer type

To use octal expressions, the number must be preceded by 0 (0)
To use hexadecimal representation, the number must be preceded by a 0x
To use binary representations, the number must be preceded by a 0b

Binary expression of integer from PHP 5.4.0 available

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

PHP does not support unsigned integers

The length of the Integer value can be expressed as a constant php_int_size, since PHP 4.4.0 and PHP 5.0.5, the maximum value can be expressed as a constant Php_int_max

Integer overflow

If a given number exceeds the range of integers (that is, an integer overflow occurs), it will be interpreted as float. Also returns a float if the result of the operation is outside the integer range

Automatic type conversion to integral type

converting from a Boolean value
FALSE will yield 0 (0), and TRUE will produce 1 (one)

convert from floating-point type
When you convert from a floating-point number to an integer, the rounding is rounded down.

Convert from String

If the string does not contain '. ', ' e ' or ' e ' and its numeric value is within the range of the integral type (as defined by Php_int_max), the string will be evaluated as an integer. All other cases are evaluated as float.

 
  
float Float Type

The word length of floating-point numbers and platform-related

A rational number that can be accurately represented in decimal, such as 0.1 or 0.7, cannot be converted to a binary format without losing a little bit of precision, regardless of how many of the mantissa can be accurately represented by the binary used internally. This can result in confusion: for example, floor ((0.1+0.7) *10 typically returns 7 instead of 8 as expected because the internal representation of the result is actually similar to 7.9999999999999991118 ...
So never believe that the floating-point number is accurate to the last one, and never compare two floating-point numbers for equality

If you do need a higher precision, you should use any mathematical or GMP function of any precision.

NAN

Some mathematical operations produce a result that is represented by a constant NAN. This result represents a value that is undefined or not expressed in a floating-point operation

NAN should not be compared with other values, including itself, and should be checked with Is_nan ().

String strings

A string can be expressed in 4 different ways

Single quote double quotes HEREDOC grammatical structure NOWDOC syntax structure (since PHP 5.3.0)

The characters in string can be accessed and modified using a 0-based subscript that contains the corresponding number in brackets similar to the array structure

Heredoc, Nowdoc

The HEREDOC structure is like a double-quote string without using double quotes

The identifier referenced at the end must be in the first column of the row, and the name of the identifier follows the rules of PHP like any other label: it can only contain letters, numbers, and underscores, and must begin with a letter and an underscore.

$str = <<
    
  

The NOWDOC structure is like a single quote string without using single quotes

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

Escape

If the string is enclosed in single quotation marks ('), only single quotation marks (') and backslashes (\) can be escaped

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

sequence meaning
\ n wrapping (LF or 0x0A in ASCII character set)
\ r carriage return (CR or 0x0D in 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) (since PHP 5.2.5)
\e Escape (ESC or 0x1B (27) in the ASCII character set) (since PHP 5.4.0)
\f page Break (FF or 0x0C (12) in the ASCII character set) (since PHP 5.2.5)
\ \ backslash
\$ USD flag
\ " double quotes
\[0-7]{1,3} conforms to the regular expression sequence, which is a eight-binary representation of the character
\x[0-9a-fa-f]{1,2} conforms to the regular expression sequence is a character expressed in 16 binary


As with single-quote strings, escaping any other character will cause backslashes to be displayed.

Curly brace Syntax

{cannot be escaped, only $ is recognized next to {, cannot have spaces, and must be enclosed in double quotation marks (not enclosed in double quotes)

$x = ' Hello '; $y = ' world '; $str = "Hi, {$x}";//$str = ' Hi, {$x} '; Invalid//$str = "Hi, {$x}". {$y}; Invalid

Automatic type conversion to string

A Boolean value of Boolean TRUE is converted to string "1". Boolean FALSE is converted to "" (empty string)

A string that is the literal style of an integer or floating-point float that is converted to a number (including the exponential portion of float)

Array arrays are always converted to the string "array", so echo and print cannot display the contents of the array

Object cannot be converted to a string, since PHP 5, you can use the __tostring method when appropriate

String functions

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

Encoding of strings

The string is encoded in the same way that the script file is encoded. Functions that manipulate the text must assume that the string is encoded, and unfortunately, PHP has many variants for this function

Some functions assume that a string is encoded in a single byte, but do not need to interpret the byte as a specific character. Examples include substr (), Strpos (), strlen (), and strcmp (). Another way to understand these functions is that they work in memory buffers, that is, by byte and byte subscripts.
Some functions are passed into the encoding of the string, and it may be assumed that there is no such information by default. For example, most functions in the htmlentities () and mbstring extensions.

Other functions use the current region (see setlocale ()), but are byte-wise. Examples include 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 locale is set correctly and á is a single-byte encoding. If you encode with UTF-8, the correct result is not returned, and the result may return a corrupted value based on the current region.

Finally, some functions assume that a string is a specific encoding, usually a UTF-8. This is true for most of the functions in the Intl extension and PCRE (only when you have used the U modifier in the previous example). Although this is due to its special purpose, Utf8_decode () assumes that UTF-8 encoding and Utf8_encode () assumes iso-8859-1 encoding.

To write a program that correctly uses Unicode relies on careful avoidance of functions that might corrupt the data and, if necessary, multibyte string functions that use the mbstring extension.

Resource Resource types

Resource resource is a special variable that holds a reference to an external resource, such as a special handle to open a file, a database connection, a graphics canvas area, and so on

Since the PHP 4 Zend engine introduces a reference counting system, it can automatically detect that a resource is no longer referenced (as in Java). In this case, all external resources used by this resource will be released by the garbage collection system. Therefore, it is seldom necessary to manually free up memory.

Persistent database connections are special, and they are not destroyed by the garbage collection system.

It does not make sense to convert other types of values to resources.

Null

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

A variable is considered NULL in the following cases:

is assigned a value of NULL. has not been assigned a value. Be unset ().
Forcing type conversions

The allowable casts are

(int)-Converted to an integer (bool), (Boolean)-Converted to Boolean type Boolean (float), (double), (real)-converted to floating-point float (string)-converted to Word String string (array)-Convert array Array (object)-Convert to Object (unset)-Convert to NULL (PHP 5)

Use the Settype () function

The type types you can set are "Boolean" (or "bool", starting with PHP 4.2.0) "integer" (or "int", from PHP 4.2.0) "Float" (only available after PHP 4.2.0, for use in older versions of " Double "Now deactivated" "string" "Array" "Object" "null" (from PHP 4.2.0)

There are also some types of conversion functions Boolval (), Intval (), Floatval (), Strval ()

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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