Type conversions in PHP

Source: Internet
Author: User
Tags scalar
We are PHPThe process of development will definitely encounter the type of data received is different from the type of data we want, then we can use PHPType conversion, and today we're going to be speaking of PHPIn the type of conversion, nonsense not much to say, we look together!

Convert to integral and floating-point numbers

Types that can be converted to integers include only floating-point numbers, Boolean values, strings, and resources. Arrays, objects cannot be converted to numeric types.

When converting to floating-point numbers, the string converts to floating-point numbers with its own set of rules, while the other values are converted to integers first (with the ability to convert to integers) and then to floating-point numbers.

When you convert from a floating-point number to an integer, the rounding is rounded down (the fractional part is discarded). If the floating-point number is outside the integer range, the result is undefined because there is not enough precision to give an exact integer result. In this case there is no warning, not even any notice!

PHP 7.0.0, when NaN and Infinity are converted to integers, they are no longer undefined or dependent on the platform, but all become 0.

When a string (decimal only) is converted to an integer, a valid integer character is searched from the first character until the end of the string or a non-integer number is encountered, and the found characters are converted to integers, and if there is no legal character, the result is 0.

When a string is converted to a floating-point number, the first character is searched for a valid floating-point digit character until the end of the string, or a non-floating-point number is encountered, and the found characters are converted to floating-point numbers, and the result is 0 if there is no legal character.

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.

The starting part of the string determines its value. If the string starts with a valid numeric value, the value is used. Otherwise its value is 0 (0). The legal value is represented by an optional sign, followed by one or more digits (which may have decimal points), followed by an optional exponential portion. The exponential part is composed of one or more digits followed by ' e ' or ' e '.

echo 0+ "0xFF", "\ n";   0  Echo 0+ "2.33", "\ n";   2.33  Echo 0+ "033", "\ n";  echo 0+ "2e3", "\ n";    2000
0, 1false, 0true, NULL

When you convert resource to integer, the result is the unique resource number assigned to resource by the PHP runtime.

Convert to String

True, "1" false, "" null--""

Converts an integer or floating-point number to a string, directly representing a numeric value as a string (Decimal, decimal)

When an array is converted to a string, the result is "array"

When an object is converted to a string, the result is "object"

When a resource converts a string, a string like "Resource id#1"

Convert to a Boolean value

When other values are converted to a Boolean value, only the following values are converted to false, and all others are converted to true:

Integer 0

Floating point 0.0

String "0"

String ""

Empty array (no element)

Empty object (no member)

NULL (including variables that have not been set)

SimpleXML object generated from an empty tag

Converting to arrays

When integers, floats, booleans, and strings are converted to arrays, a new array is obtained, and the array has only one element, which is the corresponding integer, floating-point number, Boolean value, and string.

When a null is converted to an array, the result is an empty arrays:

echo var_dump ((array) null);  Array (0) {}

Show transformations

1. You can add a coercion type conversion operator before a variable:

(int) (integer) (float) (double) (real) (BOOL) (Boolean) (string) (array) (object)

This method does not change the data type of the variable, but only changes the data type of the expression.

2. Use the Settype (var, type) function

$var = true;  Settype ($var, "integer");  Var_dump ($var);  Int (1)

Visible, the type and value of the variable have been modified

3. Use the Intval ()/floatval ()/boolval ()/strval () series functions

Note that these functions do not modify the value of the variable

$var = "0xFF";  $num = Intval ($var, +);  Var_dump ($var);  String (4) "0xFF"  var_dump ($num);  Int (255)

If the first argument of the Intval () function is a string, you can then provide an optional second argument to specify the binary of the numeric value in the string. If this parameter is not provided, the default is decimal processing and the parameter is specified as 0, which is automatically judged as a string (starting with 0x for 16). )

Intval () cannot be used with object types

The function returns a converted integer value, if the conversion fails, returns 0, if the argument is an array, the non-empty array returns 1, and the empty array returns 0

Boolval () Gets the Boolean value of the parameter.

The arguments to the floatval () function can only be scalar types, such as integers, floating-point numbers, Boolean values, and strings. cannot be used for arrays or objects.

The arguments to the Strval () function can only be scalar types, such as integers, floating-point numbers, Boolean values, and strings. cannot be used for arrays or objects.

What happens when implicit conversions are not performed

The two operands of an arithmetic operator are converted to numeric types.

The two operands of a string connector are converted to a string type.

In an expression that requires a string, it is automatically converted to string. For example, this conversion occurs when you use the function echo or print, or when a variable is compared to a 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.