整型可以用十进制、八进制、十六进制表示;如果用八进制表示,前面以“0” 开头,如果用十六进制表示,在其前面加“0x”;在其前面加“+”或“-”表示正负。
$n1=656;
$n2=0;
$n3=-42;
$n4=0123;
$n5=0x1B;
echo "$n1\t"."$n2\t"."$n3\t"."$n4\t"."$n5\t";
?>
注意科学计数法的表示
$n1 = 101.1;
$n2 = 1.011e20;
$n3 = 1.01E-10;
echo "$n1\t"."$n2\t"."$n3";
?>
$name="张三";
echo "Hi,$name
";
echo "Hi,\$name
";
echo 'Hi,$name';
?>
$dos_dir='c:\\windows\\system';
$publisher='Tim O\'Reilly';
echo "$dos_dir $publisher";
?>
PHP 中为 false 值的情况,一般整型 0、浮点型 0.0;空白字符串“”和字符串“0”; 空数组;特殊类型 null
$x=5;
$y="";
if($x)echo '$x 有一个true值
';
if(!$x)echo '$x 有一个false值
';
if($y)echo '$y有一个true值
';
if(!$y)echo '$y值有一个false值
';
?>
数据类型检测函数
$x=5;
$y="";
if($x)echo '$x 有一个true值
';
if(!$x)echo '$x 有一个false值
';
if($y)echo '$y有一个true值
';
if(!$y)echo '$y值有一个false值
';
?>
$a="这是一个字符串!";
echo gettype($a),''
;
$b=100;
echo gettype($b),''
;
$c=15.12;
echo gettype($c),''
;
$d=false;
echo gettype($d),''
;
$e=array(10,20,30);
echo gettype($e),''
;
?>
数据类型强制转换的函数;转换的规则
$a = true;
echo '$a:' . (int) $a . "
";
$b = false;
echo '$b:' . (int) $b . "
";
echo 10 - $a . "
";
$c = 10.01;
echo '$c:' . (int) $c . "
";
$d = "3.45e5";
echo '$d:' . (int) $d . "
";
$e = 3.45e5;
echo '$d:' . (int) $d . "
";
$f = "string1000";
echo '$f:' . (int) $f . "
";
$g = "-15.3e11";
echo '$g:' . (int) $g;
//转换成浮点形用(float)或(double)或(real)
$a = true;
echo '$a:' . (float) $a . "
";
$b = false;
echo '$b:' . (float) $b . "
";
$c = 10;
echo '$c:' . (float) $c . "
";
$e = 3.45e5;
echo '$e:' . (float) $e . "
";
$f = 'string1000';
echo '$f:' . (float) $f . "
";
$g = '-15.3e5';
echo '$g:' . (float) $g . "
";
$h = "-15.3a11";
echo '$h:' . (float) $h . "
";
$i = "-153a11";
echo '$i:' . (float) $i;
$a = true;
echo '$a:' . (string) $a . "
";
$b = false;
echo '$b:' . (string) $b . "
";
$c = 1041;
echo '$c:' . (string) $c . "
";
$d = 3.45e5;
echo '$d:' . (string) $d . "
";
$e = "3.45e5";
echo '$e:' . (string) $e . "
";
$f = array(100, 200);
echo '$f:' . implode($f) . "
";
echo 'NULL:' . (string) null . "
";
$a = 0.0;
if ($a) {echo '$a:true.';} else {echo '$a:false.';}
$b = 0;
if ((bool)$b) {echo '$b:true.';} else {echo '$b:false.';}
$c = 10;
if ($c) {echo '$c:true.';} else {echo '$c:false.';}
$d = array();
if ((bool)$d) {echo '$d:true.';} else {echo '$d:false.';}
$e = "";
if ((bool)$e) {echo '$a:true.';} else {echo '$e:false.';}
$f = null;
if ((bool)$f) {echo '$a:true.';} else {echo '$f:false.';}
分别设置变量 a,b,c,d,e,f,g 的值为 256,-256,256(八进制),256(十六进制),-256(八进制),-256(十六进制),ABCD(十六进制),并输出变量的值。
$a = 256;
$b = -256;
$c = 400;
$d = 100;
$e = -400;
$f = -100;
$g = 'ABCD';
echo $a . "
" . $b . "
" . $c . "
" . $d . "
" . $f . "
" . $e . "
" . $f . "
" . $g;
设置变量 a,b,c,d 的值为 1.234,-1.234,1.234e-2,1.234e2
$a = 1.234;
$b = -1.234;
$c = 1.234e-2;
$d = 1.234e2;
echo $a . "
" . $b . "
" . $c . "
" . $d;
输出:小王说:“周末我们去郊游吧!”
echo "小王说:“周末我们去旅游吧!”";
?>
分别将数值 false、3.56、3.56e3、wellcome 转换成整形、将 3.56e3、“3.56e3”、3.56a3、wellcome 转换成浮点型、将 false、3.56e3、NULL 转换成字符串型、将 0、“”、NULL、wellcome、126.5 转换成布尔型,将结果输出,并查看结果。
echo "转化为整型" . (int) (false) . "
" . (int) (3.56) . "
" . (int) (3.56e3) . "
" . (int) ('wellcome') . "
";
echo "转化为浮点型" . (float) (3.56e3) . "
" . (float) ('3.56e3') . "
" . (float) (3.56e3) . "
" . (float) ('welcome');
echo "转化为字符串型" . (string) (false) . "
" . (string) (3.56e3) . "
" . (string) (null) . "
" . (string) ('wellcome') . "
" . (string) (126.5);
echo "转化为布尔型" . (bool) (false) . "
" . (bool) (3.56e3) . "
" . (bool) (null) . "
" . (bool) ('wellcome') . "
" . (bool) (126.5);