Chapterبەش 8 · Lectureوانە 8.5

Including Files: include and requireهاوردەکردنی فایل: include و require

Write a header, a menu or a footer once and use it on every page. Learn include, include_once, require and require_once, what happens when a file is missing, and how die() and exit stop a script.

بەشی سەرەوە، یان بەشی خوارەوەی لاپەڕە یەکجار بنووسە و لە هەموو لاپەڕەکاندا بەکاری بهێنە. include، ⁦include_once⁩، require و ⁦require_once⁩ فێر دەبیت، دەزانیت کاتێک نەبێت چی ڕوودەدات و ⁦die()⁩ و exit چۆن ڕادەگرن.

20 min read20 خولەک خوێندنەوە · 17 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Explain what including a file does and why it saves work.
  • Use include, include_once, require and require_once correctly.
  • Predict what happens when an included file is missing: a warning or a fatal error.
  • Build a site whose pages share one header and one footer.
  • Stop a script with die() or exit, without showing secret error details.
  • Avoid the common mistakes: wrong names and paths, double includes, and file names chosen by the visitor.
  • ڕوونی بکەیتەوە چی دەکات و بۆچی کار کەم دەکاتەوە.
  • include، ⁦include_once⁩، require و ⁦require_once⁩ بە دروستی بەکاربهێنیت.
  • پێشبینی بکەیت کاتێک هێنراو نییە چی ڕوودەدات: یان .
  • دروست بکەیت کە لاپەڕەکانی یەک بەشی سەرەوە و یەک بەشی خوارەوەیان هاوبەش بێت.
  • بە ⁦die()⁩ یان exit ڕابگریت، بەبێ پیشاندانی وردەکاریی نهێنیی .
  • خۆت لە هەڵە باوەکان بپارێزیت: ناو و هەڵە، هێنانە ناوەوەی هەمان دوو جار و ناوی کە سەردانکەر هەڵیبژاردووە.

Why include files?بۆچی فایل دەهێنینە ناو یەکتر؟

Most websites repeat the same parts on every page. At the top there is a logo and a menu of links. At the bottom there is a copyright line. Imagine a site with 20 pages. If you copy the menu into all 20 files and later add one new link, you must change 20 files, and you will probably forget one.

زۆربەی هەمان بەش لە هەموو لاپەڕەیەکدا دووبارە دەکەنەوە. لە سەرەوە لۆگۆ و هەیە، لە خوارەوەش دێڕی . وای دابنێ ٢٠ لاپەڕەی هەیە. ئەگەر بۆ هەر ٢٠ کۆپی بکەیت و دواتر یەک نوێ زیاد بکەیت، دەبێت ٢٠ بگۆڕیت و بە ئەگەری زۆر یەکێکیان لەبیر دەکەیت.

PHP solves this with . The include (or require) statement takes all the text, code and markup that exist in another file and copies them into the file that uses the statement. You write a shared part once, keep it in its own file, and include it on every page that needs it. This is very useful when you want the same PHP, HTML or text on many pages of a website.

PHP ئەم کێشەیە بە چارەسەر دەکات. include (یان require) هەموو ئەو دەق و کۆد و نیشانەکردنانەی لە تردا هەن وەردەگرێت و کۆپییان دەکات بۆ ناو ئەو تێدا نووسراوە. بەم شێوەیە بەشێکی هاوبەش تەنها یەکجار دەنووسیت، لە جیادا هەڵیدەگریت و لە هەر لاپەڕەیەکدا کە پێویستی پێیەتی دەیهێنیتە ناوەوە. ئەمە زۆر بەسوودە کاتێک دەتەوێت هەمان PHP یان HTML یان دەق لە چەندین لاپەڕەی دابنێیت.

The school letterheadسەرپەڕەی نامەی قوتابخانە

At a school office, every official letter starts with the same letterhead: the school's name, its logo and its address. Nobody draws the letterhead by hand for each letter. It is printed once on a template, and every letter uses that template. When the school moves to a new street, only the template changes, and from that day every letter shows the new address.

لە نووسینگەی قوتابخانەدا، هەموو نامەیەکی فەرمی بە هەمان سەرپەڕە دەست پێدەکات: ناوی قوتابخانە، لۆگۆکەی و ناونیشانەکەی. کەس بۆ هەر نامەیەک سەرپەڕەکە بە دەست ناکێشێتەوە. یەکجار لەسەر نموونەیەک چاپ کراوە و هەموو نامەکان ئەو نموونەیە بەکار دەهێنن. کاتێک قوتابخانەکە دەگوازرێتەوە بۆ شەقامێکی نوێ، تەنها نموونەکە دەگۆڕدرێت و لەو ڕۆژەوە هەموو نامەکان ناونیشانە نوێیەکە پیشان دەدەن.

Where the analogy stops workingسنووری ئەم نموونەیە

A printed letterhead never changes by itself. An included PHP file is read again on every request, and the PHP code inside it really runs. So it can show something different each time, for example the current year.

سەرپەڕەی چاپکراو خۆی هەرگیز ناگۆڕێت. بەڵام PHP کە دەهێنرێتە ناوەوە لە هەموو دووبارە دەخوێنرێتەوە و کۆدی PHPـی ناوی بە ڕاستی جێبەجێ دەکرێت. بۆیە دەتوانێت هەر جارەی شتێکی جیاواز پیشان بدات، بۆ نموونە ساڵی ئێستا.

Three columns. Left: two shared files, header.php with the doctype, title, site name and menu, and footer.php with the copyright line. Middle: about.php, which sets $pageTitle and contains only its own paragraph, plus the lines require 'header.php' and require 'footer.php'. Arrows join each shared file to its require line. Right: the single HTML page the browser receives, built from the header part, the about.php content and the footer part. A note says: change footer.php once and every page that includes it shows the new footer.سێ بەش. چەپ: دوو فایلی هاوبەش، header.php کە doctype و ناونیشان و ناوی تۆڕگە و مێنیوی بەستەرەکانی تێدایە و footer.php کە دێڕی مافی لەبەرگرتنەوەی تێدایە. ناوەڕاست: about.php کە $pageTitle دیاری دەکات و تەنها بڕگەی خۆی تێدایە، لەگەڵ دێڕەکانی require 'header.php' و require 'footer.php'. تیرەکان هەر فایلێکی هاوبەش بە دێڕی require ـەکەیەوە دەبەستنەوە. ڕاست: تاکە لاپەڕەیەکی HTML کە تۆڕگەڕ وەریدەگرێت، لە بەشی سەرەوە و ناوەڕۆکی about.php و بەشی خوارەوە پێکهاتووە. تێبینییەک دەڵێت: footer.php یەکجار بگۆڕە، هەموو ئەو لاپەڕانەی دەیهێننە ناوەوە بەشی خوارەوەی نوێ پیشان دەدەن.
A page built from parts: PHP replaces each require line with the whole content of the named file.لاپەڕەیەک کە لە چەند بەشێک دروست کراوە: PHP هەر دێڕێکی require بە هەموو ناوەڕۆکی ئەو ناوی هاتووە دەگۆڕێت.CêIT

In the picture, about.php holds only its own content. When a visitor asks for about.php, PHP runs it from top to bottom. At each require line, PHP reads the named file and puts its whole content at that place. The receives one complete HTML page. It never sees the separate files, and it cannot tell that the page was built from parts.

لە وێنەکەدا، about.php تەنها ناوەڕۆکی خۆی تێدایە. کاتێک سەردانکەرێک داوای about.php دەکات، PHP لە سەرەوە بۆ خوارەوە جێبەجێی دەکات. لە هەر دێڕێکی requireدا، PHP ئەو ناوی هاتووە دەخوێنێتەوە و هەموو ناوەڕۆکەکەی لەو شوێنەدا دادەنێت. یەک لاپەڕەی تەواوی HTML وەردەگرێت. هەرگیز جیاکان نابینێت و ناشزانێت کە لاپەڕەکە لە چەند بەشێک دروست کراوە.

Quick checkپشکنینی خێراWhat does include 'footer.php'; do?include 'footer.php'; چی دەکات؟

The four statementsچوار ڕستەکە

PHP has four statements for including a file. Each one takes the name of the file (its ) as a :

PHP چوار هەیە بۆ . هەر یەکێکیان ناوی () وەک وەردەگرێت:

The four ways to include a fileچوار ڕێگاکەی هاوردەکردنی فایلPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
include 'filename';
include_once 'filename';
require 'filename';
require_once 'filename';
include, include_once, require and require_onceinclude، ⁦include_once⁩، require و ⁦require_once⁩
StatementIf the file is missingئەگەر نەبێتIf the file was already includedئەگەر پێشتر هێنرابێت
includeShows a ; the script continues. پیشان دەدات؛ بەردەوام دەبێت.Includes it again.دووبارە دەیهێنێتەوە.
include_onceShows a ; the script continues. پیشان دەدات؛ بەردەوام دەبێت.Skips it: only the first time counts.پشتگوێی دەخات: تەنها جاری یەکەم هەژمار دەکرێت.
requireA ; the script stops.؛ دەوەستێت.Includes it again.دووبارە دەیهێنێتەوە.
require_onceA ; the script stops.؛ دەوەستێت.Skips it: only the first time counts.پشتگوێی دەخات: تەنها جاری یەکەم هەژمار دەکرێت.

In short: include and require do the same job and differ only when something goes wrong. The _once versions include a file only if it has not been included before.

بە کورتی: include و require هەمان کار دەکەن و تەنها کاتێک شتێک هەڵە بڕوات جیاوازن. وەشانەکانی _once تەنها ئەو کاتە دەهێننە ناوەوە کە پێشتر نەهێنرابێت.

Quick checkپشکنینی خێراWhich statement includes a file only once and stops the script if the file is missing?کام تەنها یەک جار دەهێنێتە ناوەوە و ئەگەر نەبێت ڕادەگرێت؟

The four statementsچوار ڕستەکە

Where PHP looks for the filePHP لە کوێ بە دوای فایلەکەدا دەگەڕێت

With a simple name such as 'footer.php', PHP looks in the folder of the page that the visitor opened. That is fine while all your files sit in one folder. When your parts live in another folder, build the path from __DIR__. This magic always holds the folder of the file in which it is written, so the path works no matter which page includes the file.

کاتێک ناوێکی سادە دەنووسیت وەک 'footer.php'، PHP لە ئەو لاپەڕەیەدا دەگەڕێت کە سەردانکەر کردوویەتیەوە. تا هەموو لە یەک بن ئەمە باشە. کاتێک بەشەکانت لە تردان، لە __DIR__ دروست بکە. ئەم تایبەتە هەمیشە ئەو تێدایە کە تێیدا نووسراوە، بۆیە کار دەکات هەر لاپەڕەیەک بهێنێتە ناوەوە.

dir.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo __DIR__, "\n";
echo __DIR__ . '/footer.php', "\n";
PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
require __DIR__ . '/parts/header.php';   // works from any page
?>

A whole site with a shared header and footerتۆڕگەیەکی تەواو بە بەشی سەرەوە و خوارەوەی هاوبەش

Real sites usually share two parts: a header (the start of the HTML, the site name and the menu) and a footer (the copyright and the closing tags). Here is a small bookshop site with two pages. Each page sets its own title in a and then requires the parts.

ڕاستەقینەکان زۆرجار دوو بەش هاوبەش دەکەن: بەشی سەرەوە (سەرەتای HTML، ناوی و ) و بەشی خوارەوە ( و کۆتایی). ئەمە بچووکی کتێبفرۆشییە بە دوو لاپەڕەوە. هەر لاپەڕەیەک ناونیشانی خۆی لە دادەنێت و پاشان بەشەکان دەهێنێتە ناوەوە.

header.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<!DOCTYPE html>
<html>
<head>
  <title><?php echo $pageTitle; ?> - Aram's Bookshop</title>
</head>
<body>
<h1>Aram's Bookshop</h1>
<nav>
  <a href="index.php">Home</a> |
  <a href="about.php">About us</a>
</nav>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

$pageTitle is not created in this file. It comes from the page that includes header.php.

$pageTitle لەم دروست نەکراوە. لەو لاپەڕەیەوە دێت کە header.php دەهێنێتە ناوەوە.

footer.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<footer>
  <p>Copyright &copy; 2023-<?php echo date("Y"); ?> Aram's Bookshop</p>
</footer>
</body>
</html>
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$pageTitle = "Home";
require 'header.php';
?>
<p>Welcome! We sell books in Kurdish and English.</p>
<?php require 'footer.php'; ?>
about.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$pageTitle = "About us";
require 'header.php';
?>
<p>We opened our shop in Erbil in 2015.</p>
<?php require 'footer.php'; ?>

Now add a third page, or a new link in the menu: you change header.php once, and every page follows. This is the main reason to include files.

ئێستا لاپەڕەیەکی سێیەم یان نوێ لە زیاد بکە: تەنها یەکجار header.php دەگۆڕیت و هەموو لاپەڕەکان بەدوایدا دێن. ئەمە هۆکاری سەرەکیی .

Quick checkپشکنینی خێراIn the bookshop example, where does $pageTitle get its value?لە نموونەی کتێبفرۆشییەکەدا، $pageTitle لە کوێوە وەردەگرێت؟

PHP Full Course for free 🐘Bro Code · Englishئینگلیزی · 3M viewsبینین · 65K likesلایک · Partبەشی 2:52:33–2:59:23 (ofلە 4:00:00)

How to create a function, send values in with arguments, and get a result back with return, with a short practice example.

چۆن دروست بکەیت، بە بنێریتە ناوی و بە return ئەنجامێک لێی وەربگریتەوە، لەگەڵ ڕاهێنانێکی کورت.

Watch 2:52:33–2:59:23: the include() chapter of the full course.

لە ⁦2:52:33⁩ تا ⁦2:59:23⁩ سەیر بکە: بەشی include() لە کۆرسە تەواوەکەدا.

When the file is missing: include vs requireکاتێک فایلەکە نییە: include بەرامبەر require

It is possible to insert the content of one PHP file into another PHP file with the include or require statement. The two statements are identical, except upon failure, for example when the file does not exist or cannot be read:

دەتوانرێت ناوەڕۆکی PHP بە include یان require بخرێتە ناو تری PHP. ئەم دوو ڕێک وەک یەکن، تەنها لە کاتی شکستدا جیاوازن، بۆ نموونە کاتێک بوونی نییە یان ناخوێنرێتەوە:

  • include only produces a (E_WARNING), and the script continues.
  • require produces a and stops the script.
  • include تەنها (E_WARNING) دروست دەکات و بەردەوام دەبێت.
  • require دروست دەکات و ڕادەگرێت.
include-missing.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
include 'nonexistent.php';
echo "This line will still be executed.";
require-missing.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
require 'nonexistent.php';
echo "This line will not be executed.";
Quick checkپشکنینی خێراA page uses require 'menu.php';, but menu.php was deleted. What happens?لاپەڕەیەک require 'menu.php'; بەکار دەهێنێت، بەڵام menu.php سڕاوەتەوە. چی ڕوودەدات؟

11: Include and Require in PHP | Procedural PHP Tutorial For Beginners | PHP Tutorial | mmtutsDani Krossing · Englishئینگلیزی · 49K viewsبینین · 1.2K likesلایک · Partبەشی 0:44–4:27 (ofلە 12:04)

Shows include and require side by side, and what error each one gives when the file cannot be found.

include و require بەراورد دەکات و کاتێک نەدۆزرێتەوە هەریەکەیان چ دەدات: یان .

Watch 0:44–4:27: include vs require and their error behaviour.

لە ⁦0:44⁩ تا ⁦4:27⁩ سەیر بکە: include بەراورد بە require و جۆری .

When the file is missing: include vs requireکاتێک فایلەکە نییە: include بەرامبەر require

Which one should you use?کامیان بەکاربهێنیت؟

Use require for files that the script cannot work without, such as configuration files, the database connection or a file of important functions. If such a file is missing, it is better to stop than to continue with unexpected behaviour. Use include for optional parts, such as an advertisement box or a "latest news" template, which can fail without breaking the whole page.

require بۆ ئەو بەکاربهێنە کە بەبێ ئەوان کار ناکات، وەک ، یان گرنگ. ئەگەر وا نەبێت، وەستان باشترە لە بەردەوامبوون بە ڕەفتارێکی چاوەڕواننەکراو. include بۆ بەشە ناپێویستەکان بەکاربهێنە، وەک سندوقێکی ڕیکلام یان «دوایین هەواڵەکان»، کە دەتوانن شکست بهێنن بەبێ ئەوەی هەموو لاپەڕەکە تێکبدەن.

PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
require 'functions.php';      // essential: the page cannot work without it
include 'latest-news.php';    // optional: the page still works without it
Quick checkپشکنینی خێراWhich file should you load with require rather than include?کام دەبێت بە require بهێنیتە ناوەوە نەک بە include؟

include_once and require_once: never twice⁦include_once⁩ و ⁦require_once⁩: هەرگیز دوو جار

Including the same file twice runs it twice. For a piece of HTML you would get two copies on the page. For a file that defines functions it is worse: PHP does not allow two functions with the same name, so the script stops.

ئەگەر هەمان دوو جار بهێنیتە ناوەوە، دوو جار جێبەجێ دەکرێت. بۆ پارچەیەک HTML دوو کۆپی لە لاپەڕەکەدا دەبینیت. بۆ کە دیاری دەکات خراپترە: PHP ڕێگە نادات دوو هەمان ناویان هەبێت، بۆیە دەوەستێت.

functions.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function greet($name) {
    return "Hello, $name!";
}
twice.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
include 'functions.php';
include 'functions.php';
echo greet("Shilan");

With require_once (or include_once), PHP remembers which files it has already included and skips them the second time:

بە require_once (یان include_once)، PHP لەبیری دەمێنێت کام پێشتر هێنراوەتە ناوەوە و جاری دووەم پشتگوێیان دەخات:

once.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
require_once 'functions.php';
require_once 'functions.php';   // skipped: already included
echo greet("Shilan");
counter.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo "counter.php ran<br>\n";
once2.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
include_once 'counter.php';
include_once 'counter.php';
var_dump(include_once 'counter.php');
include 'counter.php';
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-3

The first include_once runs the file; the second one skips it.

یەکەم include_once جێبەجێ دەکات؛ دووەمیان پشتگوێی دەخات.

Lineدێڕی 4

A skipped include_once returns true.

include_onceی پشتگوێخراو true دەگەڕێنێتەوە.

Lineدێڕی 5

A plain include does not check, so the file runs again.

includeی ئاسایی ناپشکنێت، بۆیە دووبارە جێبەجێ دەکرێت.

Quick checkپشکنینی خێراWhich statement should you use for a file that defines your functions?بۆ کە دیاری دەکات کام بەکاردەهێنیت؟

A real use: one database connection fileبەکارهێنانێکی ڕاستەقینە: یەک فایلی پەیوەندیی بنکەدراوە

In the database chapter you will write the connection code once, in a file such as dbcon.php, and include it at the top of every page that needs the . One line at the top of a page, and the connection $con is ready to use.

لە بەشی کۆدی پەیوەندییەکە یەکجار دەنووسیت، لە وەک dbcon.php و لە سەرەوەی هەموو ئەو لاپەڕانەی پێویستیان بە هەیە دەیهێنیتە ناوەوە. تەنها یەک دێڕ لە سەرەوەی لاپەڕەکە و پەیوەندییەکە، $con، ئامادەی بەکارهێنانە.

dbcon.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
// dbcon.php - connect to MySQL once; every page that needs the database includes this file
try {
    $con = mysqli_connect("localhost", "root", "", "mydb");
} catch (mysqli_sql_exception $e) {
    error_log($e->getMessage());          // the real reason goes to the server's error log
    die("Sorry, the database is not available right now.");
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3-4

Try to connect. Since PHP 8.1 a failed connection throws an exception.

هەوڵی پەیوەندیکردن بدە. لە ⁦PHP 8.1⁩ـەوە پەیوەندیی شکستخواردوو فڕێ دەدات.

Lineدێڕی 6-7

Write the real reason to the server's log, show visitors only a short message, and stop.

هۆکاری ڕاستەقینە لە لۆگی بنووسە، تەنها پەیامێکی کورت بە سەردانکەران پیشان بدە و بوەستە.

books.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
require_once 'dbcon.php';   // after this line, $con is ready to use

$result = mysqli_query($con, "SELECT title FROM books");
while ($row = mysqli_fetch_assoc($result)) {
    echo htmlspecialchars($row["title"]) . "<br>";
}

These two files need a running MySQL server and a books table, so their output is not shown here. You will build and test them in Connecting PHP to MySQL. The die() in dbcon.php is explained below.

ئەم دوو پێویستیان بە MySQLـی کارا و بە ناوی books هەیە، بۆیە لێرەدا پیشان نادرێت. لە پەیوەستکردنی PHP بە MySQLدا دروستیان دەکەیت و تاقییان دەکەیتەوە. die()ی ناو dbcon.php لە خوارەوە ڕوون کراوەتەوە.

Quick checkپشکنینی خێراWhat does include "dbcon.php"; do?include "dbcon.php"; چی دەکات؟

Stopping a script: die() and exitڕاگرتنی دەسنووس: ⁦die()⁩ و exit

Sometimes a script meets an error that it cannot fix (cannot "handle"), for example a missing settings file. Continuing would only cause more errors or damage. Then the script should stop at once. The die() does exactly this: it ends the script and can print a message to the browser (or to the command line).

هەندێک جار تووشی دەبێت کە ناتوانێت چارەسەری بکات (ناتوانێت «Handle»ی بکات)، بۆ نموونە ونبوو. بەردەوامبوون تەنها و زیانی زیاتر دروست دەکات. لەو کاتەدا دەبێت دەستبەجێ بوەستێت. die() ڕێک ئەمە دەکات: کۆتایی پێدەهێنێت و دەتوانێت پەیامێک بۆ (یان بۆ هێڵی فەرمان) چاپ بکات.

die.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
// stop the script and show a message
die("An error has occurred.");

// this code will not be executed
echo "This code will not be executed.";
?>

What you should know about die():

ئەوەی دەبێت دەربارەی die() بیزانیت:

  • The script stops at once. No code after die() runs.
  • Output that was already produced before die() is still sent to the browser.
  • The message is optional. It does not have to be an error message: any string is printed.
  • die() is the same as exit(). You can also write exit; or die; without brackets.
  • If you pass an instead of a string, nothing is printed. The number becomes the script's exit status, which matters when PHP runs from the command line.
  • دەستبەجێ دەوەستێت. هیچ کۆدێک دوای die() جێبەجێ ناکرێت.
  • ئەو پێش die() دروست بووە هەر بۆ دەنێردرێت.
  • پەیامەکە ئارەزوومەندانەیە. پێویست ناکات بێت: هەر بێت چاپ دەکرێت.
  • die() هەمان exit()ـە. دەتوانیت exit; یان die;یش بەبێ کەوانە بنووسیت.
  • ئەگەر لە جیاتی بنێریت، هیچ شتێک چاپ ناکرێت. ژمارەکە دەبێتە دۆخی دەرچوونی ، کە کاتێک PHP لە هێڵی فەرمانەوە کار دەکات گرنگە.

Stopping a script: die() and exitڕاگرتنی دەسنووس: ⁦die()⁩ و exit

exit, and die as a functionexit و die وەک کردار

exit does the same job as die(). Here it stops the page after the first line, so the second echo never runs:

exit هەمان کاری die() دەکات. لێرەدا لاپەڕەکە دوای یەکەم دێڕ ڕادەگرێت، بۆیە echoی دووەم هەرگیز جێبەجێ ناکرێت:

exit.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo "Start<br>";
exit;
echo "Never shown";
exit-status.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo "Checking...";
exit(3);
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

The 3 is not printed; it is only the exit status.

ژمارە 3 چاپ ناکرێت؛ تەنها دۆخی دەرچوونە.

Quick checkپشکنینی خێراWhat does exit(3); print?exit(3); چی چاپ دەکات؟

Stopping a script: die() and exitڕاگرتنی دەسنووس: ⁦die()⁩ و exit

die() in practice⁦die()⁩ لە کرداردا

A practical use: stop with a polite message when a file that the page needs is missing.

نموونەیەکی ڕاستەقینە: کاتێک کە لاپەڕەکە پێویستی پێیەتی نییە، بە پەیامێکی ڕێزدارانە بوەستە.

start.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$settingsFile = 'settings.php';
if (!file_exists($settingsFile)) {
    die("Sorry, the site is not available right now.");
}
require $settingsFile;
echo "Welcome!";
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

file_exists() returns true if the file is there.

file_exists() ئەگەر هەبێت true دەگەڕێنێتەوە.

Lineدێڕی 4

Stop with a short, friendly message.

بە پەیامێکی کورت و دۆستانە بوەستە.

You will meet exit again right after header() in Redirects, where it stops the rest of the page from running after the visitor is sent away.

دووبارە exit دەبینیتەوە ڕاستەوخۆ دوای header() لە ئاڕاستەکردنەوە، کە لەوێ ڕێگە دەگرێت لە جێبەجێبوونی باقی لاپەڕەکە دوای ئەوەی سەردانکەر نێردرا بۆ شوێنێکی تر.

Never include a file that the visitor choosesهەرگیز فایلێک مەهێنە کە سەردانکەر هەڵیبژاردووە

Some sites have one page that shows different parts, for example show.php?page=contact. It is tempting to write include $_GET['page'] . '.php';. Never do this. The visitor controls the , so they can change the value to reach other files on the server, for example with ../ to climb out of the folder.

هەندێک یەک لاپەڕەیان هەیە کە بەشی جیاواز پیشان دەدات، بۆ نموونە show.php?page=contact. ئارەزوو دەکەیت بنووسیت include $_GET['page'] . '.php';. هەرگیز ئەمە مەکە. سەردانکەر کۆنترۆڵ دەکات، بۆیە دەتوانێت بگۆڕێت بۆ ئەوەی بگاتە تری سەر ، بۆ نموونە بە ../ بۆ دەرچوون لە .

Instead, keep an : a fixed list of the only names you accept. Anything else gets a "not found" message.

لە جیاتی ئەوە، هەڵبگرە: لیستێکی جێگیر لەو ناوانەی تەنها ئەوانە قبووڵ دەکەیت. هەر شتێکی تر پەیامی «نەدۆزرایەوە» وەردەگرێت.

show.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$allowed = ['home', 'contact'];          // the only pages we allow
$page = $_GET['page'] ?? 'home';

if (in_array($page, $allowed, true)) {
    include __DIR__ . '/pages/' . $page . '.php';
} else {
    echo "<p>Page not found.</p>";
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

The allow-list: only these two names are accepted.

: تەنها ئەم دوو ناوە قبووڵ دەکرێن.

Lineدێڕی 3

?? gives 'home' when there is no page in the URL.

?? کاتێک page لە URLـەکەدا نەبێت 'home' دەدات.

Lineدێڕی 5

in_array(..., true) checks that the name is exactly one of the allowed names.

in_array(..., true) دەپشکنێت کە ناوەکە ڕێک یەکێک لە ناوە ڕێگەپێدراوەکانە.

pages/home.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<p>Welcome to the home page.</p>
pages/contact.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<p>Call us: 0750 000 0000</p>
What the server sent for three requests (tested)ئەوەی بۆ سێ ناردی (تاقیکراوەتەوە)
RequestResultئەنجام
show.php?page=contact<p>Call us: 0750 000 0000</p>
show.php?page=../../../etc/passwd<p>Page not found.</p>
show.php<p>Welcome to the home page.</p>
Quick checkپشکنینی خێراWhy is include $_GET['page'] . '.php'; dangerous?بۆچی include $_GET['page'] . '.php'; مەترسیدارە؟

Common mistakesهەڵە باوەکان

  • Wrong file name. The file is saved as Footer.php or index.php, but the code includes footer.php. You get two warnings and no footer. Check the name and the folder.
  • Wrong folder. The part is in another folder. Build the path with __DIR__ . '/parts/footer.php'.
  • A functions file included twice. You get "Cannot redeclare function". Use require_once.
  • include for an essential file. The page goes on without its settings and shows strange errors further down. Use require (or require_once).
  • Text printed by an included file, even one blank line after ?>, comes before your HTML and can break header() redirects. In files that contain only PHP, leave out the final ?>.
  • Including a file chosen by the visitor. Use an allow-list, as shown above.
  • Expecting include to work in a .html file. PHP code runs only in files that the server sends to PHP, normally .php files.
  • ناوی هەڵەی . بە ناوی Footer.php یان index.php خەزن کراوە، بەڵام کۆدەکە footer.php دەهێنێتە ناوەوە. دوو وەردەگریت و هیچ بەشی خوارەوەیەک نییە. ناو و بپشکنە.
  • هەڵە. بەشەکە لە تردایە. بە __DIR__ . '/parts/footer.php' دروست بکە.
  • دوو جار هێنراوە. «⁦Cannot redeclare function⁩» وەردەگریت. require_once بەکاربهێنە.
  • include بۆ پێویست. لاپەڕەکە بەبێ بەردەوام دەبێت و لە خوارەوەتر سەیر پیشان دەدات. require (یان require_once) بەکاربهێنە.
  • دەقێک کە هێنراو چاپی دەکات، تەنانەت یەک دێڕی بەتاڵ دوای ?>، پێش HTMLـەکەت دێت و دەتوانێت header() تێکبدات. لەو تەنها PHPیان تێدایە، ?>ی کۆتایی مەنووسە.
  • هێنانی کە سەردانکەر هەڵیبژاردووە. وەک لە سەرەوە پیشان درا، بەکاربهێنە.
  • چاوەڕوانیی ئەوەی include لە .htmlدا کار بکات. کۆدی PHP تەنها لەو کار دەکات کە دەیاننێرێت بۆ PHP، کە بە شێوەی ئاسایی .phpن.

Exercises and lab tasksڕاهێنان و ئەرکەکانی تاقیگە

ExerciseڕاهێنانLab 6, task 1: a shared footer in NetBeansتاقیگەی ٦، ئەرکی ١: بەشی خوارەوەی هاوبەش لە NetBeans

Open a new PHP project in NetBeans and name it file_include. (1) Create a file footer.php that prints a copyright line with the current year. (2) Create a second page, second.php, with a heading and two paragraphs, and include footer.php at the bottom. (3) Run second.php and look at the result and at the page source.

پرۆژەیەکی نوێی PHP لە NetBeans بکەرەوە و ناوی بنێ file_include. (١) بە ناوی footer.php دروست بکە کە دێڕێکی لەگەڵ ساڵی ئێستا چاپ بکات. (٢) لاپەڕەیەکی دووەم، second.php، بە سەردێڕێک و دوو بڕگەوە دروست بکە و footer.php لە خوارەوەی بهێنە ناوەوە. (٣) second.php جێبەجێ بکە و سەیری ئەنجامەکە و لاپەڕەکە بکە.

Show answerوەڵامەکە پیشان بدە
footer.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php echo "<p>Copyright &copy; 2023-" . date("Y") . " example.com</p>"; ?>
second.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<html>
<body>
 <h1>Welcome to my home page!</h1>
 <p>Some text.</p>
 <p>Some more text.</p>
 <?php include 'footer.php';?>
   </body>
 </html>
ExerciseڕاهێنانBuild a three-page siteتۆڕگەیەکی سێ لاپەڕەیی دروست بکە

Make header.php and footer.php like the bookshop example, and three pages: index.php, about.php and contact.php. Each page sets $pageTitle before it requires the header. Add the contact page to the menu by changing only one file.

header.php و footer.php وەک نموونەی کتێبفرۆشییەکە دروست بکە و سێ لاپەڕە: index.php، about.php و contact.php. هەر لاپەڕەیەک پێش ئەوەی بەشی سەرەوە بهێنێتە ناوەوە، $pageTitle دیاری دەکات. لاپەڕەی پەیوەندی بە گۆڕینی تەنها یەک بخە ناو .

Hintئاماژە

Which file contains the menu?

کام تێدایە؟

Show answerوەڵامەکە پیشان بدە

Add one line to the <nav> in header.php; all three pages show the new link.

یەک دێڕ بۆ <nav>ی ناو header.php زیاد بکە؛ هەر سێ لاپەڕەکە پیشان دەدەن.

header.php (menu part)PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<nav>
  <a href="index.php">Home</a> |
  <a href="about.php">About us</a> |
  <a href="contact.php">Contact</a>
</nav>
contact.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$pageTitle = "Contact";
require 'header.php';
?>
<p>Phone: 0750 000 0000</p>
<?php require 'footer.php'; ?>
Exerciseڕاهێنانinclude or require?include یان require؟

What is the difference between include and require? Predict the output of both scripts below, then run them.

جیاوازیی نێوان include و require چییە؟ پێشبینیی هەردوو خوارەوە بکە، پاشان جێبەجێیان بکە.

PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
include 'nonexistent.php';
echo "This line will still be executed.";
PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
require 'nonexistent.php';
echo "This line will not be executed.";
Show answerوەڵامەکە پیشان بدە

Both bring another file into the script and differ only when the file cannot be found or read. The first script shows two warnings and then prints "This line will still be executed.". The second shows a warning and a fatal error (an uncaught Error in PHP 8), and the echo never runs.

هەردووکیان تر دەهێننە ناو و تەنها کاتێک جیاوازن کە نەدۆزرێتەوە یان نەخوێنرێتەوە. یەکەم دوو پیشان دەدات و پاشان «⁦This line will still be executed⁩.» چاپ دەکات. دووەمیان و پیشان دەدات (Errorێکی نەگیراو لە ⁦PHP 8⁩دا) و echo ـەکە هەرگیز جێبەجێ ناکرێت.

ExerciseڕاهێنانFix the double includeکێشەی دوو جار هاوردەکردنی فایل چاک بکەرەوە

twice.php stops with "Cannot redeclare function greet()". Change one word in each include line so that it works.

twice.php بە «⁦Cannot redeclare function greet()⁩» دەوەستێت. لە هەر دێڕێکی هێنانەکەدا یەک وشە بگۆڕە بۆ ئەوەی کار بکات.

PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
include 'functions.php';
include 'functions.php';
echo greet("Shilan");
Show answerوەڵامەکە پیشان بدە
PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
require_once 'functions.php';
require_once 'functions.php';   // skipped: already included
echo greet("Shilan");
ExerciseڕاهێنانWhat is die()?⁦die()⁩ چییە؟

Explain in your own words what die() does. Then write a script that stops with the message "Sorry, the site is not available right now." when the file settings.php does not exist, and otherwise requires it and prints "Welcome!".

بە زمانی خۆت ڕوونی بکەرەوە die() چی دەکات. پاشان بنووسە کە ئەگەر settings.php بوونی نەبوو بە پەیامی «⁦Sorry, the site is not available right now⁩.» بوەستێت، دەنا بیهێنێتە ناوەوە و «⁦Welcome!⁩» چاپ بکات.

Show answerوەڵامەکە پیشان بدە

die() (the same as exit) stops the script at once and can print a message; nothing after it runs. It is used when an error cannot be handled and going on would only cause more problems.

die() (هەمان exit) دەستبەجێ ڕادەگرێت و دەتوانێت پەیامێک چاپ بکات؛ هیچ شتێک دوای ئەو جێبەجێ ناکرێت. کاتێک بەکار دێت کە چارە ناکرێت و بەردەوامبوون تەنها کێشەی زیاتر دروست دەکات.

PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$settingsFile = 'settings.php';
if (!file_exists($settingsFile)) {
    die("Sorry, the site is not available right now.");
}
require $settingsFile;
echo "Welcome!";

Summary and reviewپوختە و پێداچوونەوە

  • include/require copy another file's content into the current file at that line; the code in it really runs.
  • Write shared parts (header, footer, menu, settings, connection) once, and include them everywhere.
  • Missing file: include gives a warning and continues; require stops with a fatal error (an uncaught Error in PHP 8).
  • include_once/require_once never include the same file twice.
  • die() = exit(): stop the script now, optionally with a message. Functions since PHP 8.4.
  • Never include a file name that comes from the visitor without an allow-list.
  • include/require ناوەڕۆکی تر لەو دێڕەدا کۆپی دەکەن بۆ ناو ئێستا؛ کۆدی ناوی بە ڕاستی جێبەجێ دەکرێت.
  • بەشە هاوبەشەکان (سەرەوە، خوارەوە، ، ، پەیوەندی) یەکجار بنووسە و لە هەموو شوێنێک بیانهێنە ناوەوە.
  • نەبوو: include دەدات و بەردەوام دەبێت؛ require بە دەوەستێت (Errorێکی نەگیراو لە ⁦PHP 8⁩دا).
  • include_once/require_once هەرگیز هەمان دوو جار ناهێننە ناوەوە.
  • die() = exit(): ئێستا ڕابگرە، ئەگەر ویستت بە پەیامێکەوە. لە ⁦PHP 8.4⁩ـەوە .
  • هەرگیز ناوی کە لە سەردانکەرەوە دێت بەبێ مەهێنە ناوەوە.
Review questionsپرسیارەکانی پێداچوونەوە
What does the include (or require) statement do?ڕستەی include (یان require) چی دەکات؟

It takes all the text, code and markup in the named file and copies it into the file that uses the statement, at that line, while the script runs.

هەموو دەق و کۆد و نیشانەکردنی ناو ناوبراوەکە وەردەگرێت و لەو دێڕەدا، لە کاتی کارکردنی ، کۆپیی دەکات بۆ ناو ئەو تێدایە.

Why is including files useful?بۆچی هاوردەکردنی فایل بەسوودە؟

You can put the same PHP, HTML or text on many pages but keep it in one file, so a change is made only once.

دەتوانیت هەمان PHP یان HTML یان دەق لە چەندین لاپەڕەدا دابنێیت بەڵام لە یەک بیهێڵیتەوە، بۆیە گۆڕانکاری تەنها یەکجار دەکرێت.

What is the difference between include and require?جیاوازیی نێوان include و require چییە؟

They are identical except upon failure: include produces a warning (E_WARNING) and the script continues; require produces a fatal error and the script stops.

ڕێک وەک یەکن جگە لە کاتی شکست: include ⁦(E_WARNING)⁩ دروست دەکات و بەردەوام دەبێت؛ require دروست دەکات و دەوەستێت.

When should you use require instead of include?کەی require لە جیاتی include بەکاردەهێنیت؟

For essential files such as configuration files, the database connection or libraries of functions; use include for optional parts such as templates.

بۆ پێویستەکان وەک ، یان ؛ include بۆ بەشە ناپێویستەکانی وەک بەکاربهێنە.

What do include_once and require_once add?⁦include_once⁩ و ⁦require_once⁩ چی زیاد دەکەن؟

They include the file only if it has not been included before, which prevents errors like "Cannot redeclare function".

تەنها ئەو کاتە دەهێننە ناوەوە کە پێشتر نەهێنرابێت، ئەمەش ڕێگە لە وەک «⁦Cannot redeclare function⁩» دەگرێت.

What is the DIE (die) function?کرداری ⁦DIE (die)⁩ چییە؟

It stops the script immediately when an error cannot be handled, optionally printing a message. It is the same as exit(). Since PHP 8.4 both are functions.

کاتێک چارە ناکرێت دەستبەجێ ڕادەگرێت و ئەگەر ویستت پەیامێک چاپ دەکات. هەمان ⁦exit()⁩ـە. لە ⁦PHP 8.4⁩ـەوە هەردووکیان .

What does include "dbcon.php"; do?⁦include "dbcon.php";⁩ چی دەکات؟

It brings the connection code into this page, so $con is ready.

کۆدی پەیوەندییەکە دەهێنێتە ناو ئەم لاپەڕەیە، بۆیە ⁦$con⁩ ئامادەیە.

Videoڤیدیۆ

Watch a videoسەیری ڤیدیۆیەک بکە

A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

How To Include Files In PHP - Include and Require - Full PHP 8 TutorialProgram With Gio · Englishئینگلیزی · 66K viewsبینین · 2.3K likesلایک

Shows include, require, include_once and require_once, how PHP finds the file, and how to reuse HTML parts on many pages.

include، require، include_once و require_once نیشان دەدات، چۆن دەدۆزێتەوە و چۆن پارچەکانی لە زۆر دووبارە بەکاربهێنیتەوە.

Whole video: all four include statements, PHP 8.

هەموو ڤیدیۆکە: هەر چوار جۆرەکەی ، بە 8.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Redirects: Sending the Browser Somewhere Elseئاڕاستەکردنەوە: ناردنی تۆڕگەڕ بۆ شوێنێکی تر

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.