基本数据类型
四种标量类型
- boolean (布尔型)
- integer (整型)
- float (浮点型)
- string (字符串)
两种复合类型:
- array (数组)
- object (对象)
最后是两种特殊类型:
- resource (资源)
- NULL (NULL)
伪类型
为确保代码的易读性而提出的伪类型。以及伪变量 $....
“双精度”类型:double,其实际上和float是相同的,由于一些历史原因,这两个名称同时存在
伪类型有以下几种:
1.mixed
2.number
3.callback
变量打印
echo()函数 print_r()函数 和var_dump()函数
存在一个变量和一个数组和资源型变量,分别尝试去打印下面五个变量:
$a = '1';
$b = array(1,2,'3');
$fh = fopen('./02.php','r');
class Car {
var $color;
function __construct($color) {
$this->color = $color;
}
}
$obj = new Car('white');
$type = NULL;
输出结果:
echo "$a" // 1
echo "$b" // Notice: Array to string conversion,
// 意思是echo函数把`$b`当作一个字符串去输出,解析为字符串"Array"
echo "$fh"; // Resource id #3
echo "$obj"; // Catchable fatal erro: 实例化的对象不能转换成字符串输出
echo "$type"; // 什么也没有输出
print_r($a); // 1
print_r($b); // Array ( [0] => 1 [1] => 2 [2] => 3 )
print_r($fh); // Resource id #3
print_r($obj); // Car Object ( [color] => white )
print_r($type); // 什么也没有输出
var_dump($a); // string '1' (length=1)
var_dump($fh); // resource(3, stream)
var_dump($type); // null
var_dump($obj);
// 输出如下:
object(Car)[1]
public 'color' => string 'white' (length=5)
var_dump($b);
// 输出如下:
array (size=3)
0 => int 1
1 => int 2
2 => string '3' (length=1)
输出函数总结:
echo():可以输出标量类型和特殊类型resource的变量存储的值,但是不知道值的数据类型,特殊标识的类型除外。(如resource...)
print_r():可以输出除NULL特殊类型以外的所有数据类型,且知道值的数据类型,但是不知道数组、对象中key对应的value的数据类型。
var_dump():可以输出所有数据类型,且知道所有值的数据类型。