|
ถ้าต้องการเช็คดูว่า
ตัวแปรมีข้อมูลแบบใด เราสามารถใช้คำสั่ง gettype() ได้ ค่าที่ได้จากฟังก์ชันก็จะเป็น
"integer" "double" หรือ "string" เป็นต้น
| <?
echo gettype(0),"\n";
echo gettype(1.1),"\n";
echo gettype(""),"\n";
echo gettype((1==1)),"\n";
$var="abc";
if ( gettype($var)=="string"
) {
echo "this is a string\n";
}
?>
|
เราอาจจะไม่ใช้
gettype() ก็ได้ แต่เลือกใช้ฟังก์ชัน is_long() สำหรับเช็คค่าที่เป็นเลขจำนวนเต็ม,
is_string() สำหรับเช็คค่าที่เป็นสตริงค์, is_double() สำหรับค่าที่เป็นเลขทศนิยม,
is_array() สำหรับค่าที่เป็นอาร์เรย์ หรือ is_object() สำหรับค่าที่เป็นออปเจคจากคลาสแทน
ซึ่งจะให้ค่าเท่ากับ true (1) ถ้าตัวแปรมีแบบข้อมูล ตรงตามที่กำหนด
| <?
unset($a);
$a="hello";
if (is_string($a) == true)
{
echo "\$a is a string
<BR>\n";
}
unset($a);
$a[]="red";
$a[]="green";
$a[]="blue";
if (is_array($a) == true)
{
echo "\$a is an
array of size ",count($a),"<BR>\n";
}
?>
|
|