PHP Include

6 min read

The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.

include (эсвэл require) мэдэгдэл нь заасан файлд байгаа бүх текст / код / ​​тэмдэглэгээг авч, оруулгыг ашигласан файлд хуулна.

Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

Вэбсайтын олон хуудсанд ижил PHP, HTML эсвэл текст оруулахыг хүссэн тохиолдолд файл оруулах нь маш их хэрэгтэй байдаг.


PHP include and require Statements #

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.

Нэг PHP файлын агуулгыг өөр PHP файлд оруулах боломжтой (сервер ажиллуулахаас өмнө), оруулах эсвэл шаардах мэдэгдлийн хамт оруулах боломжтой.

The include and require statements are identical, except upon failure:

Оруулах ба шаардах мэдэгдэл нь бүтэлгүйтсэнээс бусад тохиолдолд ижил байна:

  • require will produce a fatal error (E_COMPILE_ERROR) and stop the script

    require нь ноцтой алдаа (E_COMPILE_ERROR) үүсгэж, скриптийг зогсоох болно.

  • include will only produce a warning (E_WARNING) and the script will continue

    include нь зөвхөн анхааруулга (E_WARNING) үүсгэх бөгөөд скрипт үргэлжлэх болно

So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution. This will help avoid compromising your application’s security and integrity, just in-case one key file is accidentally missing.

Тиймээс, хэрэв та файл нь ороогүй байсан ч гэсэн гүйцэтгэлийг үргэлжлүүлж, хэрэглэгчдэд үр дүнг нь харуулахыг хүсч байвал include мэдэгдлийг ашиглаарай. Үгүй бол FrameWork, CMS эсвэл PHP програмын нарийн төвөгтэй кодчилолын хувьд үргэлж гүйцэтгэлийн урсгалд түлхүүр файл оруулахын тулд Require-ийг ашиглаарай. Энэ нь таны програмын аюулгүй байдал, бүрэн бүтэн байдлыг алдагдуулахаас зайлсхийхэд тусална, учир нь нэг түлхүүр файл санамсаргүй байдлаар алга болсон тохиолдолд л болно.

Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.

Файлуудыг оруулах нь маш их ажлыг хэмнэж өгдөг. Энэ нь та бүх вэб хуудсандаа стандарт толгой, хөл, цэс файл үүсгэх боломжтой гэсэн үг юм. Дараа нь толгойг шинэчлэх шаардлагатай үед та зөвхөн толгой файлыг шинэчлэх боломжтой.

Syntax  #

include ‘filename‘;

or

require ‘filename‘;


PHP include Examples #

Example 1 #

Assume we have a standard footer file called “footer.php”, that looks like this:

Бидэнд “footer.php” нэртэй стандарт footer файл байгаа гэж үзье:

echo “

Copyright © 1999-” . date(“Y”) . ” Apprentice.mn

“;
?>

To include the footer file in a page, use the include statement:

Хуудасны доод хэсгийн файлыг оруулахын тулд include мэдэгдлийг ашиглана уу.

Example #

Жишээ #

<html> <body> <h1>Welcome to my home page!</h1> <p>Some text.</p> <p>Some more text.</p> <?php include 'footer.php';?> </body> </html>

Example 2 #

Assume we have a standard menu file called “menu.php”:

Бидэнд “menu.php” нэртэй стандарт цэс файл байгаа гэж үзье:

echo ‘Home –
HTML Tutorial –
CSS Tutorial –
JavaScript Tutorial –
PHP Tutorial’;
?>

All pages in the Web site should use this menu file. Here is how it can be done (we are using a element so that the menu easily can be styled with CSS later):

Вэбсайт дахь бүх хуудсууд энэ цэсийн файлыг ашиглах ёстой. Үүнийг хэрхэн яаж хийхийг энд харуулав (цэсийг дараа нь CSS-ээр хялбархан засахын тулд бид элемент ашиглаж байна):

Example #

Жишээ #

<html> <body> <div class="menu"> <?php include 'menu.php';?> </div> <h1>Welcome to my home page!</h1> <p>Some text.</p> <p>Some more text.</p> </body> </html>

Example 3 #

Assume we have a file called “vars.php”, with some variables defined:

Бидэнд “vars.php” нэртэй, зарим хувьсагчийг тодорхойлсон файл байгаа гэж үзье:

$color=’red’;
$car=’BMW’;
?>

Then, if we include the “vars.php” file, the variables can be used in the calling file:

Дараа нь, хэрэв бид “vars.php” файлыг оруулсан бол хувьсагчдыг дуудагч файлд ашиглаж болно.

Example #

Жишээ #

<html> <body> <h1>Welcome to my home page!</h1> <?php include 'vars.php'; echo "I have a $color $car."; ?> </body> </html>

PHP include vs. require #

The require statement is also used to include a file into the PHP code.

require мэдэгдлийг PHP кодонд файл оруулахад ашигладаг.

However, there is one big difference between include and require; when a file is included with the include statement and PHP cannot find it, the script will continue to execute:

Гэсэн хэдий ч оруулах, оруулах хоёрын хооронд нэг том ялгаа бий; файлыг include хэллэгт оруулаад PHP олж чадахгүй тохиолдолд скрипт үргэлжлүүлэн ажиллана.

Example #

Жишээ #

<html> <body> <h1>Welcome to my home page!</h1> <?php include 'noFileExists.php'; echo "I have a $color $car."; ?> </body> </html>

If we do the same example using the require statement, the echo statement will not be executed because the script execution dies after the require statement returned a fatal error:

Хэрэв бид require мэдэгдлийг ашиглан ижил жишээг хийвэл, echo хэллэгийг гүйцэтгэхгүй, учир нь require мэдэгдэл нь ноцтой алдаа гаргасны дараа скриптийн гүйцэтгэл үхэх болно.

Example #

Жишээ #

<html> <body> <h1>Welcome to my home page!</h1> <?php require 'noFileExists.php'; echo "I have a $color $car."; ?> </body> </html>

Use require when the file is required by the application.

Файлыг аппликешн require үед ашиглахыг шаардана.

Use include when the file is not required and application should continue when file is not found.

Файл шаардлагагүй тохиолдолд include оруулна уу, файл олдохгүй тохиолдолд програм үргэлжлүүлэн ажиллана.

Powered by BetterDocs

Leave a Reply