Home  >  Article  >  Backend Development  >  Detailed explanation of php data type conversion examples

Detailed explanation of php data type conversion examples

怪我咯
怪我咯Original
2017-04-27 17:09:246599browse

PHP is a weakly typed language, because when we declare a variable, we do not need to specify the data type it stores. However, although PHP is a weakly typed language, type conversion is still needed sometimes.

PHP allows conversion types as follows:

##integer, int Convert to integer(integer)$boo,(integer)$strfloat, doubleconvert to floating point type(float)$strarrayConvert to array(array)$strobjectConvert to object(object)$str##Type conversion in PHP is very simple,
Conversion operator Conversion type Example
boolean, bool Convert to Boolean type (boolean)$num,(boolean)$str
string Convert to string (string)$boo,(string)$flo
There are three Three conversion methods:

The first one:

Just need to add the type name enclosed in parentheses before the variable to be converted, like the following:

<?php
$num1=3.14;
$num2=(int)$num1;
var_dump($num1);
echo "<br/>";
var_dump($num2);
?>

Code running results:

##Second type: Detailed explanation of php data type conversion examples

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

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

Code running result:

Third type:Detailed explanation of php data type conversion examples

Use the settype() function, which can specify The variable is converted into the specified data type. The syntax is as follows:

settype(mixed var,string type)

The parameter var is the specified variable; the parameter type is the specified data type. The parameter type has 7 optional values, namely boolean, float, integer, array, null, object and string. If the conversion is successful, the setype() function returns true, otherwise it returns false.

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

Code running results:

Detailed explanation of php data type conversion examples#When the string is converted to an integer or floating point type, if the string starts with a number , the number part will be converted to an integer first, and then the following string will be discarded; if the number contains a decimal point, the first decimal place will be taken.

Detailed explanation of php data type conversion examplesPHP data type conversion example

This example will use the first and third methods to convert the specified string type and compare the two methods. The difference between them, the code is as follows:

<?php
header("content-type:text/html;charset=utf-8");        //设置编码
$num=&#39;3.1415926r*r&#39;;
echo &#39;使用(integer)操作符转换变量$num类型:&#39;;        //使用integer转换类型
echo (integer)$num .&#39;<br/>&#39;;
echo &#39;输出变量$num的值:&#39;.$num.&#39;<br/>&#39;;               //输出原始变量$num
echo &#39;使用settype函数转换变量$num类型:&#39;;
echo settype($num,&#39;integer&#39;).&#39;<br/>&#39;;                 //使用settype函数转换类型
echo &#39;输出变量$num的值:&#39;.$num;                       //输出原始变量$num
                       
?>

The code running result:

As you can see from the above example, using the integer operator can directly output The converted variable type, and the original variable does not change in any way. Instead, the settype() function returns 1, which is true, and the original variable is changed. In actual applications, you can choose the conversion method according to your own needs. Detailed explanation of php data type conversion examples

In the next section, we will explain "

How to detect data type

".

The above is the detailed content of Detailed explanation of php data type conversion examples. 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
Previous article:What are cookies in php?Next article:What are cookies in php?