Php Data type conversion

Source: Internet
Author: User
This article mainly shares the knowledge of PHP Data type conversion. PHP Data type conversion is forced conversion. The PHP Data types that can be converted include: (int), (integer): converted to integer & nbsp; (float), and (double), (real): convert to float type & nbsp; (string): convert to string & nbsp; (bool), (boolean): convert to boolean type & nbsp; (array ): convert to array & nbsp; (object): This article 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 brackets before the variable to be converted.

Target type, for example: (int) (bool) (float) (string) (array) (object)

The following is an example:

$ 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:

$ 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:

$ 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.