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 ?>View Code
(完.)