Three ways to convert data types in PHP _php tips

Source: Internet
Author: User

PHP data type conversions are cast, and PHP data types that allow conversion are:

1. (int), (integer): Convert to Plastic
2. (float), (double), (real): Convert to floating point type
3. (string): Convert to String
4. (bool), (Boolean): Convert to Boolean type
5. (array): Converting an array of
6. (object): Convert to Object

There are three ways to convert PHP data types:

1. Precede the variable to be converted with a target type enclosed in parentheses
2. Use 3 specific types of conversion functions, intval (), Floatval (), Strval ()
3. Use common type Conversion functions Settype (mixed var,string type)

First mode of conversion: (int) (bool) (float) (string) (array) (object)

Copy Code code as follows:

<?php
$num 1=3.14;
$num 2= (int) $num 1;
Var_dump ($num 1); Output float (3.14)
Var_dump ($num 2); Output int (3)
?>

The second mode of conversion: Intval () floatval () Strval ()

Copy Code code as follows:

<?php
$str = "123.9ABC";
$int =intval ($STR); Value after conversion: 123
$float =floatval ($STR); Value after conversion: 123.9
$str =strval ($float); After conversion string: "123.9"
?>

The third mode of conversion: Settype ();
Copy Code code as follows:

<?php
$num 4=12.8;
$FLG =settype ($num 4, "int");
Var_dump ($FLG); output bool (TRUE)
Var_dump ($num 4); Output int (12)
?>

The journey of the journey begins with a. Change the future, start from now. To change the present is to change the future.

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.