PHP cast type

Source: Internet
Author: User
Tags comparison table scalar type null

Get data type:

1. If you want to see the value and type of an expression, use Var_dump ().
2. If you just want an easy-to-read type of expression for debugging, use GetType ().
3. To view a type, do not use GetType () and use the Is_type () function. convert string to numeric
    1. When a string is evaluated as a number, the type and value of the result are determined according to the following rules.
    2. If any one of the characters is included in ".", "E" or "E", the string is evaluated as a float. Otherwise, it is treated as an integer.
    3. The value is determined by the first part of the string. If the string starts with a valid numeric data, the number is used as its value, otherwise its value is 0 (0). Valid numeric data starts with an optional sign, followed by one or more digits (optionally including a decimal fraction) followed by an optional exponent. An exponent is an "E" or "E" followed by one or more numbers.
Note: Do not expect to be able to encode a character when it is converted to an integer type (this may also be done in C). If you want to convert between character encodings and characters, use the Ord () and Chr () functions. forcing type casts

The type casts in PHP are very similar to those in C: precede the variables to be converted with the target type enclosed in parentheses.

The allowable casts are:

    • (int), (integer)-Converted to integral type
    • (bool), (Boolean)-converted to Boolean
    • (float), (double), (real)-converts to floating-point type
    • (string)-converts to a string
    • (array)-Convert an array
    • (object)-Convert to Object

Note that spaces and tabs are allowed inside the parentheses
You can also use Settype (mixed var, string type) to cast.

1. Cast to a Boolean value (BOOL) | (Boolean)

To explicitly convert a value to a Boolean, use either (BOOL) or (Boolean) to cast. However, in many cases it is not necessary to cast, because when an operator, function, or process control requires a Boolean parameter, the value is automatically converted.

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"
An array with no member variables
object with no cell (PHP 4 only)
Special type NULL (including variables that have not been set)
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. Cast to reshape (int) | (integer)
To explicitly convert a value to an integer, cast with (int) or (integer). However, in most cases there is no need to cast, because when an operator, function, or process control requires an integer parameter, the value is automatically converted. You can also convert a value to an integral type by using the function intval ().

A. Converting from BOOL
B. Transfer from floating point to whole, out of range, with uncertain results
C. Converting from a string to a numeric value
D. Convert from another type to a bool value, then convert

Never cast an unknown fraction to an integer, which can sometimes result in unexpected results.
<?php
echo (int) ((0.1+0.7) * 10); Showing 7
?>

$str = "123.456ABC7"; (int) 123
echo (int) $str;
$str = "abc123.456"; (int) 0
$str = true; (int) 1
$str = false; (int) 0


3. Cast to floating-point (int) | (double) | (real) |doubleval () |floatval () |intval ()
Accuracy: 0.12345678901234//Double,real all the same
Data loss parameter string converted to numeric value


4. Force swap to String (string) |strval ()

You can use the (string) tag or the Strval () function to convert a value to a string. When a string is required for an expression, the conversion of the string is done automatically within the scope of the expression. For example, when you use the Echo () or print () function, or when you compare a variable value to a string.

    • A Boolean value of TRUE will be converted to the string "1", and the value FALSE will be represented as "" (that is, an empty string). This allows you to randomly compare a Boolean value to a string.
    • When a numeric value of an integer or floating-point number is converted to a string, the string consists of numeric characters representing the numbers (the floating-point number also contains the exponential portion).
    • The array is converted to the string "array", so the contents of the array cannot be output through the echo () or print () function. Please refer to below for more tips.
    • The object is converted to the string "Object". If you need to print the object's member variables for debugging purposes, please read the following. If you want to get the name of the class to which the object is attached, use the function Get_class (). From PHP 5, if appropriate, you can use the __tostring () method.
    • The resource type is always converted to a string in the format "Resource ID #1", where 1 is the unique identity that PHP assigns to the resource at run time. If you want to get the type of the resource, use the function Get_resource_type ().
    • Null will be converted to an empty string.

As shown above, printing arrays, objects, or resources does not provide any useful information about the values themselves. See Functions Print_r () and Var_dump (), which are better ways to print values for debugging purposes.
You can convert the values of PHP to strings to store them permanently. This method is called serialization and can be done with the function serialize (). If you set up WDDX support when you install PHP, you can also serialize the value of PHP into an XML structure.


4. Casting an array (array)

    • For any type: integer, floating Point, String, Boolean, and resource, if you convert a value to an array, you will get a list of only one element (its subscript is 0), which is the value of this scalar.
    • If an object is converted to an array, the resulting array of elements is the property (member variable) of the object whose key name is the member variable name.
    • If you convert a value to an NULL array, you will get an empty set.


5. Convert to Objects (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 empty. Converting an array into an object causes the key name to be the property name and has a corresponding value. For any other value, a member variable named scalar will contain the value


6. Convert to resource (cannot convert)
Because resource type variables hold special handles for open files, database connections, graphics canvas areas, and so on, you cannot convert other types of values to resources.

PHP Type Comparison Table
The table below shows the role of PHP types and comparison operators when they are loosely and rigorously compared. The supplemental material is also relevant to the relevant section of the type-juggling.

    • Attention
    • HTML forms do not pass integers, floating-point numbers, or Boolean values, they only pass strings. To detect if a string is not a number, you can use the Is_numeric () function.
    • When a variable $x is not defined, the use of an if ($x) results in a e_notice level error. Therefore, you can consider using empty
    • Go to https://www.cnblogs.com/supermeimei/p/5164613.html

PHP cast type

Related Article

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.