คำอธิบาย
ฟังก์ชัน ctype_digit() ตรวจสอบว่าตัวอักษรทุกตัวในสตริงเป็นตัวเลข (0-9) ทั้งหมดหรือไม่
รูปแบบการใช้งาน (Syntax)
ctype_digit(str)
พารามิเตอร์
- str — สตริงที่ต้องการตรวจสอบ
ค่าที่คืนกลับ
คืนค่า TRUE หากทุกตัวอักษรเป็นตัวเลข หรือ FALSE หากไม่ใช่
ตัวอย่างการใช้งาน
<?php
$arr = array('12345', '12.34', 'abc123', '007');
foreach ($arr as $a) {
if (ctype_digit($a)) {
echo "$a has all digits.\n";
} else {
echo "$a does not have all digits.\n";
}
}
?>
ผลลัพธ์
12345 has all digits. 12.34 does not have all digits. abc123 does not have all digits. 007 has all digits.
หมายเหตุ: จุดทศนิยม (.) ไม่ถือเป็นตัวเลข