PHP data type conversion (character to number, numeric to character)

Source: Internet
Author: User
This article mainly introduces PHP data type conversion (character to number, number to character), has a certain reference value, now share to everyone, the need for friends can refer to

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

    • (int), (integer): Convert to Reshape

    • (float), (double), (real): Convert to floating-point type

    • (string): converted to a string

    • (bool), (Boolean): Converted to Boolean type

    • (array): Convert an array

    • (object): Convert to Object

There are three ways to convert PHP data types:

    • Precede the variable you want to convert with a target type enclosed in parentheses

    • Use 3 specific types of conversion functions, intval (), Floatval (), Strval () "Memory: Purpose type to convert +val ()"

    • Use the common type conversion function Settype (mixed var,string type)

First conversion Mode: (int) (bool) (float) (string) (array) (object)

    1. <?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 ()

    1. <?php   $str = "123.9ABC";   $int =intval ($STR);     Converted value: 123   $float =floatval ($STR);//Converted value: 123.9   $str =strval ($float);   Converted string: "123.9"    ?>

The third mode of conversion: Settype ();

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

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.