您的位置:首页 > 编程语言 > PHP开发

Php数据类型简介

2016-02-18 10:58 549 查看
Php支持8种原始数据类型。

四种标量类型:boolean(布尔型)、integer(整型)、float(浮点型,也称作double)、string(字符串)。

两种符合类型:array(数组)、object(对象)

两种特殊类型:resource(资源)、NULL(无类型)

变量的类型通常不是由程序员设定的,确切地说,是由php根据该变量使用的上下文在运行时决定的。

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

<?php
header("Content-type:text/html;charset=utf-8");
$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(is_int($an_int)){
$an_int += 4;
echo $an_int; //print out : 16
}
if(is_string($a_bool)){
echo "String:$a_bool";
}
?>

如果要将一个变量强制转化为某类型,可以对其使用强制转换或者settype()函数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: