PHP数据类型之间的强制转换

时间:2022-02-08 16:46:06

1.实型数据强制转换为整型数据

$float1=2.7;
$int1=(int)$float1; echo var_dump($int1),"<br>";

输出:

int(2)

2.整型数据强制转换为字符串数据

$int2=3;
$float2=(float)$int2; echo "<hr>";
echo var_dump($float2),"<br>";

输出:

float(3)

3.整型数据强制转换为字符串数据

$int3=4;
$str1=(string)$int2; echo "<hr>";
echo var_dump($str1),"<br>";

输出:

string(1) "3"

4.整型数据强制转换为字符串数据

$float3=4.7;
$str2=(string)$float3; echo "<hr>";
echo var_dump($str2),"<br>";

输出:

string(3) "4.7"

5.布尔型数据强制转换为字符串数据

$bool1=TRUE;
$str3=(string)$bool1; echo "<hr>";
echo var_dump($str3),"<br>"; $bool1=FALSE;
$str3=(string)$bool1; echo var_dump($str3),"<br>";

输出:

string(1) "1" 
string(0) ""

6.布尔型数据强制转换为整型数据

$bool2=TRUE;
$str4=(string)$bool2; echo "<hr>";
echo var_dump($str4),"<br>";

输出:

string(1) "1"

7.整型数据强制转换为布尔型数据 (只有是0时,返回flase,其他都返回true)

$int4=2;
$bool3=(bool)$int4; echo "<hr>";
echo var_dump($bool3),"<br>"; $int4=-2;
$bool3=(bool)$int4; echo var_dump($bool3),"<br>"; $int4=0;
$bool3=(bool)$int4; echo var_dump($bool3),"<br>"; //返回false

输出:

bool(true) 
bool(true) 
bool(false)

8.字符串型数据强制转换为布尔型数据 (只有是"0"和空""时,返回flase,其他都返回true)

$str5="1";
$bool4=(bool)$str5; echo "<hr>";
echo var_dump($bool4),"<br>"; $str5="0";
$bool4=(bool)$str5; echo var_dump($bool4),"<br>"; //返回false $str5="00";
$bool4=(bool)$str5; echo var_dump($bool4),"<br>"; //返回true $str5="HELLO!";
$bool4=(bool)$str5; echo var_dump($bool4),"<br>"; $str5="0HELLO!";
$bool4=(bool)$str5; echo var_dump($bool4),"<br>"; $str5="";
$bool4=(bool)$str5; echo var_dump($bool4),"<br>"; //返回false $str5=" ";
$bool4=(bool)$str5; $str5="FLASE";
$bool4=(bool)$str5; echo var_dump($bool4),"<br>"; //返回true

输出:

bool(true) 
bool(false) 
bool(true) 
bool(true) 
bool(true) 
bool(false) 
bool(true)

最后是完整代码:

<?php
//1.实型数据强制转换为整型数据
$float1=2.7;
$int1=(int)$float1; echo var_dump($int1),"<br>"; //2.整型数据强制转换为实型数据
$int2=3;
$float2=(float)$int2; echo "<hr>";
echo var_dump($float2),"<br>"; //3.整型数据强制转换为字符串数据
$int3=4;
$str1=(string)$int2; echo "<hr>";
echo var_dump($str1),"<br>"; //4.整型数据强制转换为字符串数据
$float3=4.7;
$str2=(string)$float3; echo "<hr>";
echo var_dump($str2),"<br>"; //5.布尔型数据强制转换为字符串数据
$bool1=TRUE;
$str3=(string)$bool1; echo "<hr>";
echo var_dump($str3),"<br>"; $bool1=FALSE;
$str3=(string)$bool1; echo var_dump($str3),"<br>"; //6.布尔型数据强制转换为整型数据
$bool2=TRUE;
$str4=(string)$bool2; echo "<hr>";
echo var_dump($str4),"<br>"; //7.整型数据强制转换为布尔型数据 (只有是0时,返回flase,其他都返回true)
$int4=2;
$bool3=(bool)$int4; echo "<hr>";
echo var_dump($bool3),"<br>"; $int4=-2;
$bool3=(bool)$int4; echo var_dump($bool3),"<br>"; $int4=0;
$bool3=(bool)$int4; echo var_dump($bool3),"<br>"; //返回false //8.字符串型数据强制转换为布尔型数据 (只有是"0"和空""时,返回flase,其他都返回true)
$str5="1";
$bool4=(bool)$str5; echo "<hr>";
echo var_dump($bool4),"<br>"; $str5="0";
$bool4=(bool)$str5; echo var_dump($bool4),"<br>"; //返回false $str5="00";
$bool4=(bool)$str5; echo var_dump($bool4),"<br>"; //返回true $str5="HELLO!";
$bool4=(bool)$str5; echo var_dump($bool4),"<br>"; $str5="0HELLO!";
$bool4=(bool)$str5; echo var_dump($bool4),"<br>"; $str5="";
$bool4=(bool)$str5; echo var_dump($bool4),"<br>"; //返回false $str5=" ";
$bool4=(bool)$str5; $str5="FLASE";
$bool4=(bool)$str5; echo var_dump($bool4),"<br>"; //返回true
?>

(完.)