PHP Data type conversions

Source: Internet
Author: User
Tags array object bool mysql php mysql string mysql database

This article mainly shares the knowledge of PHP data type conversion.

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

(int), (integer): Converting to plastic
(float), (double), (real): Converting to floating point type
(string): Converting to String
(bool), (Boolean): Convert to Boolean type
(array): Converting an array of
(object): Converting to Objects

There are three ways to convert PHP data types:

(1) Precede the variable to be converted with a target type enclosed in parentheses, for example:

(int)  (BOOL)  (float)  (string) (array) (object) The following example illustrates:

<?php
$num 1=3.14;
$num 2= (int) $num 1; Cast to int type
var_dump ($num 1);//output float (3.14)
Var_dump ($num 2);//output int (3)

(2) Use 3 specific types of conversion functions, intval (), Floatval (), Strval (), examples are as follows:

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

(3) Use the common type conversion function Settype (mixed var,string type), 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)

Articles that you may be interested in

    • Summary of methods for using Curl post submission data and get access to Web page data in PHP
    • MySQL various data table types reset the starting value of the method
    • How PHP converts a multidimensional array to a one-dimensional array
    • How PHP converts a BR line break in HTML into a newline character in text input
    • PHP to determine the most secure upload file type, the most realistic solution
    • PHP MySQL database operation class
    • Powerful PHP Image processing class (watermark, transparency, zoom, sharpen, rotate, flip, cut, invert color)
    • PHP uses Array_flip to implement array key exchange to remove duplicate value of array


Related Article

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.