Devdit
 

PHP การเรียกไฟล์ด้วยคำสั่ง include และ require ในรูปแบบต่างๆ

1.7K

บทความนี้จะสอนการเรียกใช้ไฟล์ด้วยคำสั่ง include และ require ของภาษา PHP ในลักษณะโครงสร้างการจัดเก็บรูปแบบต่างๆ ที่มีความซับซ้อน โดยตัวอย่างการจัดเก็บไฟล์ดังนี้

|- index.php (1)
|- template (Folder)
	- header.php
	- body.php
	- footer.php
|- function (Folder)
	- a.php
	- b.php
|- admin (Folder)
	- body (Folder)
		- index.php (2)

 

1. ตัวอย่าง ไฟล์ index.php (1) เรียกใช้ไฟล์ body.php ใน Folder template

require( "body.php" );
หรือ
include( "body.php" );

 

2. ตัวอย่าง ไฟล์ index.php (2) เรียกใช้ไฟล์ a.php ใน Folder function

require( "../../function/a.php" );
หรือ
include( "../../function/a.php" );

 

3. ตัวอย่าง ไฟล์ index.php (2) เรียกใช้ไฟล์ footer.php ใน Folder template

require( "../../template/footer.php" );
หรือ
include( "../../template/footer.php" );

 

4. ตัวอย่าง ไฟล์ a.php เรียกใช้ไฟล์ index.php (2) ใน Folder admin/body

require( "../admin/body/index.php" );
include( "../admin/body/index.php" );
เขียน 2 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ