PHP type conversion & amp; type forced conversion

Source: Internet
Author: User
Tags comparison table type null
Obtain the Data Type: 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, use the is_type () function instead of gettype. ■ String to numeric. When a string is evaluated as a number

Obtain the Data Type: 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, use the is_type () function instead of gettype. ■ String to numeric. When a string is evaluated as a number

Data Type:

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, use the is_type () function instead of gettype.

■ Convert a string to a numeric value

  1. When a string is evaluated as a number, the result type and value are determined according to the following rules.
  2. If the string contains any of the characters ".", "e", or "E", it is evaluated as a float. Otherwise, it is treated as an integer.
  3. This value is determined by the first part of the string. If a string starts with a valid number, use this number as its value. Otherwise, its value is 0 (0 ). Valid numeric data starts with an optional plus or minus sign, followed by one or more numbers (including decimal scores), followed by an optional index. An index is an "e" or "E" followed by one or more numbers.
Do not expect to get the encoding of a character when converting it into an integer (it may also be done in C ). Use the ord () and chr () functions if you want to convert between character encoding and characters.


■ Forced type conversion (refer to: PHP manual)

The forced type conversion in PHP is very similar to that in C: add the target type enclosed in parentheses before the variable to be converted.

The following mandatory conversions are allowed:

  • (Int), (integer)-convert to integer
  • (Bool), (boolean)-convert to boolean
  • (Float), (double), (real)-convert to floating point type
  • (String)-convert to string
  • (Array)-convert to an array
  • (Object)-convert to object

Note that empty spaces and tabs are allowed in parentheses.
You can also use settype (mixed var, string type) for forced conversion.

1. Forced conversion to boolean value (bool) | (boolean)

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.

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 (applicable only to PHP 4)
Special Type NULL (including unset variables)
All other values are considered TRUE (including any resources)

?phpvar_dump((bool) ""); // bool(false)var_dump((bool) 1); // bool(true)var_dump((bool) -2); // bool(true)var_dump((bool) "foo"); // bool(true)var_dump((bool) 2.3e5); // bool(true)var_dump((bool) array(12)); // bool(true)var_dump((bool) array()); // bool(false)var_dump((bool) "false"); // bool(true)?>

2. Forced conversion to integer (int) | (integer)
To explicitly convert a value to an integer, use (int) or (integer) to force the conversion. However, in most cases, no forced conversion is required, because the value is automatically converted when an integer parameter is required for the operator, function, or flow control. You can also convert a value to an integer using the intval () function.

A. Convert from bool
B. Convert from floating point

Integer. Beyond the range. The result is not fixed.
C. for conversion from a string, see converting a string to a numeric value.
D. Replace bool value with another type conversion before conversion.

Never forcibly convert an unknown score to an integer, which sometimes leads to unexpected results.
Echo (int) (0.1 + 0.7) * 10); // display 7
?>

$ Str = "123.456abc7"; // (int) 123
Echo (int) $ str;
$ Str = "abc123.456"; // (int) 0
$ Str = true; // (int) 1
$ Str = false; // (int) 0


3. Forced conversion to floating point (int) | (double) | (real) | doubleval_r () | floatval () | intval ()
Precision: 0.12345678901234 // double, real is the same
Data loss parameter string to Numerical Value


4. Force convert to string (string) | strval ()

You can use the (string) flag or the strval () function to convert a value to a string. When an expression requires a string, String Conversion is automatically completed within the expression range. For example, when the echo () or print () function is used, or a variable value is compared with a string.
  • The Boolean value TRUE is converted to the string "1", and the value FALSE is expressed as "" (an empty string ). In this way, you can freely compare a Boolean value with a string.
  • When an integer or floating-point number is converted to a string, the string is composed of numeric characters that represent these values (the floating-point number also contains an exponential part ).
  • The Array is converted to the string "Array", so the Array content cannot be output through the echo () or print () function. Refer to the following for more tips.
  • The Object is converted to the string "Object ". If you need to print out the member variables of the object for debugging, read the following section. If you want to get the name of the class to which the object is attached, use the get_class () function (). From PHP 5, use the _ toString () method if appropriate.
  • The Resource type is always converted to a string in the format of "Resource id #1", where 1 is the unique identifier that PHP assigns to the Resource at runtime. If you want to obtain the resource type, use the get_resource_type () function ().
  • NULL is converted to a NULL string.
As shown above, printing arrays, objects, or resources does not provide any useful information about these values. See the print_r () and var_dump () functions. These are better ways to print values for debugging.
PHP values can be converted to strings to store them permanently. This method is called serialization. You can use the serialize () function to complete this operation. If WDDX support is set up during PHP installation, the PHP value can also be serialized into an XML structure.


4. Forced conversion to array)
  • For any type: integer, floating point, String, Boolean, and resource, If you convert a value to an array, an array with only one element is obtained (its subscript is 0 ), this element is the value of this scalar.
  • If you convert an object into an array, the elements of the obtained array are the attributes (member variables) of the object, and the key name is the member variable name.
  • IfNULLConvert the value to an array to obtain an empty array.

5. Convert to object)

If you convert an object to an object, it will not change. If any other type of value is converted to an object, an instance of the built-in class stdClass will be created. If the value is NULL, the new instance is NULL. Converting an array to an object makes the key name a property name and has a corresponding value. For any other value, the member variable named scalar will contain this value.


6. convert to a resource (cannot be converted)
Because resource type variables are saved as special handles for opening files, database connections, and graphic canvas areas, other types of values cannot be converted to resources.

■ PHP type comparison table (see Appendix P of the Manual)
The following table shows the role of PHP types and comparison operators in loose and strict comparison. This supplement also relates to the content of the relevant chapter for the Type trick.

  • Note:

  • HTML forms do not pass integers, floating-point numbers, or boolean values. They only pass strings. To check whether a string is a number, use the is_numeric () function.
  • When no variable $ x is defined, such as if ($ x) usage may cause an E_NOTICE-level error. Therefore, you can use empty () or isset () functions to initialize variables.

Implementation of PHP forced type conversion

Conversion format

Returns

Implementation Method

(Int), (integer)

Convert other data types to integer type

$ A = "3"; $ B = (int) $ a; $ c = (integer) $;

(Bool), (boolean)

Forcibly convert other data types to boolean

$ A = "3"; $ B = (bool) $ a; $ c = (boolean) $;

(Float), (double), (real)

Forcibly convert other data types to floating point type

$ A = "3"; $ B = (float) $ a; $ c = (double) $ a; $ d = (real) $;

(String)

Forcibly convert other data types to strings

$ A = 3; $ B = (string) $;

(Array)

Forcibly convert other data types to Arrays

$ A = "3"; $ B = (array) $;

(Object)

Forcibly convert other data types to objects

$ A = "3"; $ B = (object) $;

Convert other data types to integer types

Original Type

Category

Change rules

Floating Point Type

Integer

Round down, that is, the part after the decimal point is directly removed instead of rounded down. Only the integer part is retained.

Boolean

Integer

TRUE to integer 1, FALSE to integer 0

String

Integer

The string is a pure integer number and is converted to the corresponding integer number.

The string contains digits with decimal points. after the decimal point is removed during conversion, the integer part is retained.

The string starts with an integer. The part after the integer is removed during conversion and then processed according to Rule 1.

The string starts with a decimal point. after the decimal point is removed during conversion, the string is processed according to Rule 2.

The string content starts with a non-number and is directly converted to 0.

Convert other data types to floating point

Original Type

Category

Change rules

Integer

Floating Point Type

Convert the integer data directly to the floating point type, and keep the value unchanged.

Boolean

Floating Point Type

TRUE to float-type number 1, FALSE to float-type number 0

String

Floating Point Type

The string is an integer and is directly converted to a floating point number.

The string starts with a number. The part after the number is removed during conversion and then processed according to Rule 1.

The string starts with a decimal point. After the conversion, only the digits are retained.

The string starts with a non-numeric content and is directly converted to 0.

Convert other data types to boolean ones

Original Type

Category

Change rules

Integer

Boolean

0 to FALSE, non-zero integer to TRUE

Floating Point Type

Boolean

Convert 0 to FALSE, and convert non-zero floating point numbers to TRUE

String

Boolean

Convert null string or string content to FALSE, and convert other strings to TRUE

NULL

Boolean

Convert directly to FALSE

Array

Boolean

Convert an empty array to FALSE, and convert a non-empty array to TRUE

Convert other data types to Arrays

Original Type

Category

Change rules

Integer, floating point, and Boolean

String, Resource

Array

When these data types are forcibly converted to arrays, the obtained array contains only one data element, which is the data before conversion, and the data type is the same as before the conversion.

Object

Array

During conversion, the name of the member variable of the object is used as the key of each array element. After conversion, the value of each key in the array is empty. If the member variable is private ), the converted key name is "class name + member variable name". If the member variable is public, the converted key name is the member variable name, if the member variable is protected, the converted key name is "* + member variable name"

NULL

Array

Directly convert to an empty array

Convert other data types to objects

Original Type

Target type

Conversion rules

Integer and floating point type

Boolean, string

Object

When you convert other types of variables to an object, a new attribute named "scalar" will be created, and the value of the original variable will be stored in this attribute.

Array

Object

When an array is converted to an object, the key of the array is used as the name of the object member variable, and the value of each key is used as the saved value of the object member variable.

NULL

Object

Directly convert to an empty object

Convert other data types to strings

Original Type

Category

Change rules

Integer

String

During conversion, double quotation marks are directly added to both sides of the integer data as the conversion result.

Floating Point Type

String

During conversion, double quotation marks are directly added to both sides of the floating point data as the conversion result.

Boolean

String

TRUE to string "1", FALSE to string "0"

Array

String

Directly convert to the string "Array"

Object

String

Directly convert to the string "Object"

NULL

String

Directly convert to an empty string

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.