Three methods of data type conversion in PHP and three methods of data type conversion _ PHP Tutorial

Source: Internet
Author: User
Three methods of data type conversion in PHP and three methods of data type conversion. Three methods of data type conversion in PHP, three methods of data type conversion in PHP are forced conversion. The PHP Data types that can be converted include: 1. (int), (integer): three methods for converting data types to PHP, three methods for converting data types

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

1. (int), (integer): convert to integer
2. (float), (double), (real): convert to floating point type
3. (string): convert to a string
4. (bool), (boolean): convert to boolean type
5. (array): convert to an array
6. (object): convert to an object

There are three conversion methods for PHP Data types:

1. add the target type enclosed in parentheses before the variable to be converted
2. use three types of conversion functions: intval (), floatval (), strval ()
3. use the common type conversion function settype (mixed var, string type)

First conversion method: (int) (bool) (float) (string) (array) (object)
The code is as follows:
<? Php
$ Num1 = 3.14;
$ Num2 = (int) $ num1;
Var_dump ($ num1); // output float (3.14)
Var_dump ($ num2); // output int (3)
?>

Second conversion method: intval () floatval () strval ()
The code is as follows:
<? Php
$ Str = "123.9abc ";
$ Int = intval ($ str); // converted value: 123
$ Float = floatval ($ str); // converted value: 123.9
$ Str = strval ($ float); // converted string: "123.9"
?>

Third conversion method: settype ();
The code is as follows:
<? Php
$ Num4 = 12.8;
$ Flg = settype ($ num4, "int ");
Var_dump ($ flg); // output bool (true)
Var_dump ($ num4); // output int (12)
?>

A journey of a thousand miles begins with a single step. Change the future from now on. Changing the present is changing the future.

PHP Data type conversion is a forced conversion. The PHP Data types that can be converted include: 1. (int) and (integer...

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.