Analysis on php Data type conversion

Source: Internet
Author: User
This article mainly introduces php Data type conversion. if you need it, you can refer to the explicit type definition that PHP does not need (or does not support) in variable definition; the variable type is determined by the context in which the variable is used. That is to say, if a string value is assigned to the variable var, var becomes a string. If another integer value is assigned to var, it becomes an integer.

An example of PHP's automatic type conversion is the plus sign "+ ". If any operand is a floating-point number, all the operands are treated as floating-point numbers, and the result is also a floating-point number. Otherwise, the operand is interpreted as an integer and the result is also an integer. Note that this does not change the type of these operands. it only changes how these operands are evaluated and the type of the expressions.

Type forced conversion
The following mandatory conversions are allowed:

• (Int), (integer)-convert to integer)
• (Bool), (boolean)-convert to boolean)
• (Float), (double), (real)-converted to float)
• (String)-convert to string (string)
• (Binary)-convert to a binary string (string) (PHP 6)
• (Array)-convert to an array (array)
• (Object)-convert to object)
• (Unset)-convert to NULL (PHP 5)
(Binary) the prefix 'B' is added before the result, and PHP 5.2.1 is added.

Note that empty spaces and tabs are allowed in parentheses.

Convert string text and variables to a binary string ):

The code is as follows:
$ Binary = (binary) $ string;
$ Binary = B "binary string ";
?>

To change the type of a variable, see settype ();

Settype-set the variable type

Bool settype (mixed $ var, string $ type)
Set the var variable type to type.

The possible value of type is:

• "Boolean" (or "bool", starting from PHP 4.2.0)
• "Integer" (or "int", starting from PHP 4.2.0)
• "Float" (available only after PHP 4.2.0, and "double" used in earlier versions is disabled now)
• "String"
• "Array"
• "Object"
• "Null" (from PHP 4.2.0)
Returns TRUE if the call succeeds, or FALSE if the call fails.

Intval (), floatval (), strval (), these three functions can also be converted

The following describes how to convert PHP Data types.

PHP Data type conversion is a forced conversion. The PHP Data types that can be converted include:

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

There are three conversion methods for PHP Data types:

(1) add the target type enclosed in parentheses before the variable to be converted, for example:

(Int) (bool) (float) (string) (array) (object) is described in the following example:

The code is as follows:
$ Num1 = 3.14;
$ Num2 = (int) $ num1; // forcibly converted to int type
Var_dump ($ num1); // output float (3.14)
Var_dump ($ num2); // output int (3)

(2) use three types of conversion functions: intval (), floatval (), and strval (). The example is as follows:

The code is as follows:
$ Str = "123.9abc ";
$ Int = intval ($ str); // converted value: 123
$ Float = floatval ($ str); // converted value: 123.9
$ Str = strval ($ float); // converted string: "123.9"

(3) use the common type conversion function settype (mixed var, string type). The example is as follows:

The code is as follows:
$ Num4 = 12.8;
$ Flg = settype ($ num4, "int ");
Var_dump ($ flg); // output bool (true)
Var_dump ($ num4); // output int (12)

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.