Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

คำสั่ง include ใน PHP

บทนำ

โค้ด PHP ในไฟล์หนึ่งสามารถนำไปใช้ในสคริปต์ PHP อีกไฟล์หนึ่งได้ด้วยคำสั่ง include คุณสมบัตินี้ทำให้พัฒนาโปรแกรมแบบโมดูลาร์ได้ โดยเขียนฟังก์ชันและคลาสครั้งเดียว เรียกใช้ได้ทุกที่ที่ต้องการ

คำสั่ง include ตรวจสอบไฟล์ในโฟลเดอร์ปัจจุบันโดยค่าเริ่มต้น และต่อไปในไดเรกทอรีที่ระบุในการตั้งค่า include_path ของ php.ini หากไฟล์ที่ขอไม่มีในโฟลเดอร์ปัจจุบันและโฟลเดอร์ include_path PHP parser จะปล่อย E_WARNING และการดำเนินการของโมดูลที่เรียกจะดำเนินต่อไป

ไฟล์ที่ include เข้ามาสามารถเข้าถึงตัวแปรที่กำหนดในโมดูลที่เรียก ก่อนการ include ได้ ตัวแปรเหล่านี้จะมีขอบเขต global (global scope)

ตัวอย่าง include

ในตัวอย่างต่อไปนี้ main php script include test.php:

ตัวอย่าง

<?php echo "inside main script\n"; $var1=100; echo "now calling test.php script\n"; include "test.php"; echo "returns from test.php"; ?> //test.php <?php $var2=200; //เข้าถึง $var1 จาก main script echo $var1+$var2 . "\n"; ?>

ผลลัพธ์

inside main script<br />now calling test.php script<br /><br />300<br />returns from test.php

คำเตือนเมื่อ include ล้มเหลว

ในตัวอย่างต่อไปนี้ พยายาม include ไฟล์ที่ไม่มีอยู่ ทำให้เกิด warning:

ตัวอย่าง

<?php echo "inside main script\n"; $var1=100; echo "now calling nosuchfile.php script\n"; include "nosuchfile.php"; echo "returns from nosuchfile.php"; ?>

ผลลัพธ์

inside main script now calling nosuchfile.php script PHP Warning: include(nosuchfile.php): failed to open stream: No such file or directory in line 5 PHP Warning: include(): Failed opening 'nosuchfile.php' for inclusion (include_path='C:\xampp\php\PEAR') in line 5 returns from nosuchfile.php

include จากภายในฟังก์ชัน

ในตัวอย่างต่อไปนี้ test.php ถูก include ภายในฟังก์ชัน โค้ดภายในไฟล์ที่ include จะถูกจัดการเป็นส่วนของฟังก์ชัน ดังนั้นตัวแปรในไฟล์ include จะไม่เข้าถึงได้นอกฟังก์ชัน

ตัวอย่าง

//main script <?php function myfunction(){     $var1=100;     include "test.php";     echo $var1+$var2."\n"; } myfunction(); echo "variable from included file outside function: $var2"; ?> //test.php included <?php echo "test.php called from inside function\n"; $var2=200; return; ?>

ผลลัพธ์

100 test.php called from inside function PHP Notice: Undefined variable: var2 in C:\xampp\php\testscript.php on line 9 Notice: Undefined variable: var2 in C:\xampp\php\testscript.php on line 9 variable from included file outside function

การคืนค่าจากไฟล์ include (return from include file)

คำสั่ง include คืนค่า TRUE เมื่อสำเร็จ และ FALSE เมื่อล้มเหลว หากไฟล์ include คืนค่านิพจน์อย่างชัดเจน สามารถใช้โดยโมดูลที่เรียกสำหรับการประมวลผลต่อไปได้

ตัวอย่าง

//main script <?php $result=include "test.php"; // returns value echo "retun value : $result\n"; $y=include "test1.php"; //with return only echo "return value : $y\n"; $x=include "test2.php"; //no return statement echo "return value : $x\n"; ?> //test.php <?php $var=100; return $var; ?> //test1.php <?php $var=100; return; ?> //test2.php <?php $var=100; ?>

ผลลัพธ์

retun value : 100 return value : return value : 1