PHP Learning (type conversion)

Source: Internet
Author: User
Tags type null

PHP does not need (or does not support) explicit type definitions in variable definitions; The variable type is determined by the context in which the variable is used. In other words, if you assign a string value to a variable $var , $var it becomes a string. If you assign an integer to $var it, it becomes an integer.

An example of automatic type conversion for PHP is the addition operator "+". if any one of the operands is float, all the operands are treated as float, and the result is float . Otherwise the operand is interpreted as an integer and the result is an integer. Note that this does not change the type of the operands themselves; it only changes how the operands are evaluated and the type of the expression itself.

A type cast is required if you want to force a variable to be evaluated as a type.

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.

Convert to Boolean type

To explicitly convert a value to a Boolean, use either (bool) or (Boolean) to cast. However, there are many cases where casting is not required because the value is automatically converted when an operator, function, or process control structure requires a boolean parameter. when converted to Boolean, the following FALSE values are considered:

    • The Boolean FALSE value itself
    • Integer value 0 (0)
    • Floating-point value 0.0 (0)
    • An empty string, and the string "0"
    • An array that does not include any elements
    • Objects that do not include any member variables (PHP 4.0 only applies)
    • Special type NULL (including variables that have not been assigned)
    • SimpleXML object generated from an empty tag

Convert to integral type

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 ().

See also: discriminant of type conversions.

Converting from a Boolean value

will produce a 0(0), TRUE will produce 1 (one). FALSE

Convert from floating-point type

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

Convert string to Numeric

When a string is taken as a numeric value, the result and type are as follows:

If the string does not contain '. ', ' e ' or ' e ' and its numeric value is within the range of the integral PHP_INT_MAX type (defined by), 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 '.

PHP Learning (type conversion)

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.