Home  >  Article  >  Backend Development  >  PHP data type conversion conversion

PHP data type conversion conversion

小云云
小云云Original
2018-03-14 13:09:021912browse

This article mainly shares with you the conversion of PHP data type conversion. This article mainly shares it with you in the form of code. I hope it can help you.

PHP data type conversion is a forced conversion. The PHP data types that are allowed to be converted are:

  • (int), (integer): converted to integer

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

  • (string): Convert to string

  • (bool), (boolean): Convert to Boolean type

  • (array): Convert to array

  • (object): Convert to object

There are three conversion methods for PHP data types:

  • Add parentheses before the variable to be converted. The target type

  • uses three specific types of conversion functions, intval(), floatval(), strval()

  • uses general Type conversion function settype(mixed var,string type)

The first conversion method: (int) (bool) (float) (string) (array) (object)

<?php   
$num1=3.14;   
$num2=(int)$num1;   
var_dump($num1); //输出float(3.14)   
var_dump($num2); //输出int(3)   
?>

The second conversion method: intval() floatval() strval()

<?php   
$str="123.9abc";   
$int=intval($str);     //转换后数值:123   
$float=floatval($str); //转换后数值:123.9   
$str=strval($float);   //转换后字符串:"123.9"    
?>

The third conversion method: settype();

<?php   
$num4=12.8;   
$flg=settype($num4,"int");   
var_dump($flg);  //输出bool(true)   
var_dump($num4); //输出int(12)   
?>

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

Related recommendations:

Detailed explanation of PHP data type conversion examples

PHP data type conversion

php data type conversion_PHP tutorial

The above is the detailed content of PHP data type conversion conversion. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn