4

2.1PHP类型简介

Posted by 撒得一地 on 2015年9月3日 in PHP入门教程

PHP 支持 8 种原始数据类型。
四种标量类型: 
1. boolean (布尔型)  
2. integer (整型)  
3. float (浮点型,也称作 double )  
4. string (字符串)  

两种复合类型: 
1. array (数组)  
2. object (对象)  

最后是两种特殊类型: 
1. resource (资源)  
2. NULL (无类型) 

在后面的内容中,这8中类型将会详细被讨论。

为了确保代码的易读性,PHP中还包括一些伪类型:
1. mixed (混合类型)  
2. number (数字类型)  
3. callback (回调类型) 
在PHP的其它资料中,你可能还会读到一些关于“双精度(double)”类型的参考,实际上,在PHP中double和float是相同的,由于一些历史原因,这两个名字同时存在。

如果想查看某个表达式的值和类型,用 var_dump() 函数。  
如果只是想得到一个易读懂的类型的表达方式用于调试,用 gettype()  函数。
要查看某个类型,不要用 gettype() ,而用 is_type 函数。以下是一些范例: 

<?php
$a_bool  =  TRUE ;     // a boolean
$a_str   =  "foo" ;     // a string
$a_str2  =  'foo' ;     // a string
$an_int  =  12 ;      // an integer

echo  gettype ( $a_bool );  // prints out:  boolean
echo  gettype ( $a_str );   // prints out:  string

// If this is an integer, increment it by four
if ( is_int ( $an_int )) {
	    $an_int  +=  4 ;     //$an_int自增4
}

echo $an_int; //   print out:16;

// If $bool is a string, print it out
if ( is_string ( $a_bool )) {
	echo  "String:  $a_bool "; 
} else {
    echo "Not String: $a_bool";//print out:Not String 1
}
?> 

如果要将一个变量强制转换为某类型,可以对其使用强制转换或者 settype() 函数。 
注意变量根据其当时的类型在特定场合下会表现出不同的值。

标签:,

上一篇:

下一篇:

相关推荐

4 Comments

发表评论

电子邮件地址不会被公开。 必填项已用*标注

0 + 6 = ?

网站地图|XML地图

Copyright © 2015-2024 技术拉近你我! All rights reserved.
闽ICP备15015576号-1 版权所有©psz.