दुवै include र require ले अर्को PHP फाइलको सामग्री हाल भएको फाइलमा आयात (र निष्पादन) गर्छन्। एकमात्र भेद भनेको तिनीहरूले विफलता कसरी सम्हाल्छन्: require ले फाइल हराएमा घातक त्रुटि दिन्छ, जबकि include ले केवल चेतावनी दिन्छ र अगाडि बढ्छ।
<?php
include "config.php"; // if missing → WARNING, script CONTINUES
require "database.php"; // if missing → FATAL ERROR, script STOPS
include → file missing → emits a Warning, execution continues (file's code skipped)
require → file missing → throws a Fatal Error, execution HALTS
require प्रयोग गर्नुहोस् ऐसी फाइलहरूका लागि जसके बिना अनुप्रयोग चल्न सक्दैन (मुख्य वर्गहरू, गुरुत्वपूर्ण कन्फिग, डाटाबेस सेटअप) — तपाई यो चाहनुहुन्छ कि यो कठोरतापूर्वक रोक्का होस् यदि तिनीहरू हराएमा। include प्रयोग गर्नुहोस् वैकल्पिक फाइलहरूका लागि (उदाहरण गरी साइडबार टेम्प्लेट) जहाँ हराएको फाइले पृष्ठ क्र्यास गर्न सक्दैन।
require_once "functions.php"; // include the file ONLY if not already included
include_once "helper.php";
require_once/include_once ले फाइल आयात गर्छ यदि यो पहिले नै आयात भएको छैन भने मात्र — आवश्यक "function/class पुनः घोषणा गर्न सक्दैन" त्रुटिहरू रोक्न जब फाइल एकैचोटि समावेश गरिन सक्छ (उदाहरण गरी वर्ग परिभाषा धेरै फाइलहरूद्वारा खिचिन्छ)।
// included files share the SAME scope and can define functions, classes, variables
// config.php:
$dbHost = "localhost";
// main.php:
require "config.php";
echo $dbHost; // "localhost" — the variable is available
// ❌ old style — manually requiring every class file
require_once "User.php";
require_once "Order.php";
// ✅ modern — Composer AUTOLOADING loads classes on demand automatically
require "vendor/autoload.php"; // one line; classes load when used (PSR-4)
$user = new User(); // User.php loaded automatically
आधुनिक PHP मा, तपाई वर्गहरूका लागि म्यानुअल require विरलै प्रयोग गर्नुहुन्छ — Composer को autoloader तिनीहरूलाई मागमा लोड गर्छ। तपाई अझै पनि `require