PHP headers_sent()ฟังก์ชั่น
ตัวอย่าง
หากไม่มีการส่งส่วนหัว ให้ส่งอย่างใดอย่างหนึ่ง:
<?php
if (!headers_sent()) {
header("Location: https://www.w3schools.com/");
exit;
}
?>
<html>
<body>
...
...
ความหมายและการใช้งาน
ฟังก์ชัน headers_sent() ตรวจสอบว่า / ตำแหน่งใดถูกส่งไปแล้ว
ไวยากรณ์
headers_sent(file,line)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
file | Optional. If the file and line parameters are set, headers_sent() will put the PHP source file name and line number where output started in the file and line variables |
line | Optional. Specifies the line number where the output started |
รายละเอียดทางเทคนิค
คืนมูลค่า: | TRUE หากส่งส่วนหัว HTTP มิฉะนั้นจะเป็น FALSE |
---|---|
รุ่น PHP: | 4.0+ |
บันทึกการเปลี่ยนแปลง PHP: | PHP 4.3: เพิ่มไฟล์ เสริม และพารามิเตอร์บรรทัด |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
การใช้พารามิเตอร์ไฟล์และบรรทัดทางเลือก:
<?php
// $file and $line are passed in for later use
// Do not assign them values beforehand
if (!headers_sent($file, $line))
{
header("Location: https://www.w3schools.com/");
exit;
// Trigger an error here
}
else
{
echo "Headers sent in $file on line $line";
exit;
}
?>
<html>
<body>
...
...
❮ การอ้างอิงเครือข่าย PHP