PHP filter_input()ฟังก์ชั่น
ตัวอย่าง
ตรวจสอบว่าตัวแปรภายนอก "email" ถูกส่งไปยังหน้า PHP หรือไม่ โดยใช้วิธี "get" และตรวจสอบว่าเป็นที่อยู่อีเมลที่ถูกต้องหรือไม่:
<?php
if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL)) {
echo("Email is not valid");
} else {
echo("Email is valid");
}
?>
ความหมายและการใช้งาน
ฟังก์ชัน filter_input() รับตัวแปรภายนอก (เช่น จากอินพุตแบบฟอร์ม) และกรองตัวแปรได้
ฟังก์ชันนี้ใช้เพื่อตรวจสอบความถูกต้องของตัวแปรจากแหล่งที่ไม่ปลอดภัย เช่น การป้อนข้อมูลของผู้ใช้
ไวยากรณ์
filter_input(type, variable, filter, options)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
type | Required. The input type to check for. Can be one of the following:
|
variable | Required. The variable name to check |
filter | Optional. Specifies the ID or name of the filter to use. Default is FILTER_DEFAULT, which results in no filtering |
options | Optional. Specifies one or more flags/options to use. Check each filter for possible options and flags |
รายละเอียดทางเทคนิค
คืนมูลค่า: | ค่าของตัวแปรต่อความสำเร็จ FALSE เมื่อล้มเหลว หรือ NULL หากไม่ได้ตั้งค่าตัวแปร |
---|---|
รุ่น PHP: | 5.2+ |
❮ อ้างอิงตัวกรอง PHP ที่สมบูรณ์