Three ways to convert data types in PHP, data types three ways _php tutorial

Source: Internet
Author: User

Three ways to convert data types in PHP, data types three ways


PHP's data type conversions are cast, and the PHP data types that are allowed to be converted are:

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

There are three ways to convert PHP data types:

1. Add the target type enclosed in parentheses before the variable you want to convert
2. Use 3 specific types of conversion functions, intval (), Floatval (), Strval ()
3. Using the common type conversion function Settype (mixed var,string type)

First conversion Mode: (int) (bool) (float) (string) (array) (object)
Copy the 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)
?>

Second mode of conversion: Intval () floatval () Strval ()
Copy the Code code as follows:
<?php
$str = "123.9ABC";
$int =intval ($STR); Post-conversion value: 123
$float =floatval ($STR); Post-conversion value: 123.9
$str =strval ($float); Converted string: "123.9"
?>

The third mode of conversion: Settype ();
Copy the 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)
?>

A journey begins with a journey. Change the future, starting from now. Changing the present is changing the future.

http://www.bkjia.com/PHPjc/978383.html www.bkjia.com true http://www.bkjia.com/PHPjc/978383.html techarticle PHP Data type conversion in three ways, data type three ways PHP data type conversion is cast, the allowable conversion of PHP data types are: 1. (int), (integer): Go ...

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