Php include and Php include_once : PHP में, include और include_once दो फ़ंक्शन्स हैं जिनका उपयोग एक फ़ाइल को किसी अन्य फ़ाइल में सम्मिलित (include) करने के लिए किया जाता है।
Php include function:
include PHP का एक फ़ंक्शन है जिसका उपयोग एक फ़ाइल के content को दूसरी फ़ाइल में सम्मिलित (include) करने के लिए किया जाता है।
Syntax Of Php include Function:
include 'filename.php';
Working Of Php include Function:
- File Address:
filename.phpके स्थान पर आप include करने वाली फ़ाइल का नाम या पूरा Path निर्दिष्ट कर सकते हैं। - File Inclusion: जब PHP स्क्रिप्ट चलती है, तो
includeफ़ंक्शन निर्दिष्ट फ़ाइल को पढ़ता है और उस फ़ाइल की सामग्री को वर्तमान स्क्रिप्ट में शामिल कर देता है। - Code Execution: Include की गई फ़ाइल का कोड, वर्तमान स्क्रिप्ट के कोड के साथ मिलकर Execute होता है।
Example of include Function:
मान लीजिए आपके पास एक header.php फ़ाइल है जिसमें वेबसाइट का हेडर HTML कोड है:
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
</header>PHPअब आप अपनी मुख्य फ़ाइल index.php में इस हेडर को शामिल कर सकते हैं:
<?php
include 'header.php';
?>
<p>This is the main content of the page.</p>
<?php
include 'footer.php'; // आप इसी तरह फुटर भी शामिल कर सकते हैं
?>PHPजब आप index.php को चलाएंगे, तो header.php का Content index.php में शामिल हो जायेगा और दोनों फ़ाइलों का कोड एक साथ Execute होगा।
When File Not Found:
अगर सम्मिलित फ़ाइल उपलब्ध नहीं है:
<?php
include 'missing.php';
echo "यह कोड अभी भी चलेगा।";
?>PHPआउटपुट:
Warning: include(missing.php): failed to open stream: No such file or directory
Warning: include(): Failed opening 'missing.php' for inclusion
यह कोड अभी भी चलेगा।PHPImportant Point of include function:
- यदि शामिल की जाने वाली फ़ाइल नहीं मिलती है, तो PHP एक चेतावनी (Warning) जारी करेगा लेकिन स्क्रिप्ट का Execution जारी रहेगा।
includeफ़ाइल को हर बार शामिल करेगा, चाहे वह पहले से शामिल हो चुकी हो या नहीं।
Php include_once function:
PHP में include_once फ़ंक्शन भी एक फ़ाइल को शामिल करने के लिए उपयोग किया जाता है, लेकिन इसके व्यवहार में एक महत्वपूर्ण अंतर है: यह सुनिश्चित करता है कि एक फ़ाइल केवल एक बार शामिल की जाए, भले ही आप इसे कई बार शामिल करने का प्रयास करें।
Syntax Of include_once function:
include_once 'filename.php';
Working Of include_once function:
- File Checking: जब
include_onceफ़ंक्शन को पहली बार Execute किया जाता है, तो यह निर्दिष्ट फ़ाइल को शामिल करता है। - Check Duplicate: यदि फ़ाइल पहले से ही शामिल हो चुकी है, तो
include_onceफ़ंक्शन इसे फिर से शामिल नहीं करेगा।
Example Of php include_once function:
मान लीजिए आपके पास एक functions.php फ़ाइल है जिसमें कुछ उपयोगी फ़ंक्शन define हैं:
<?php
function greet($name) {
echo "Hello, $name!";
}
?>PHPअब आप अपनी मुख्य फ़ाइल index.php में इस फ़ाइल को शामिल कर सकते हैं:
<?php
include_once 'functions.php';
greet('Alice');
greet('Bob');
?>PHPयहां, भले ही आप include_once को दो बार कॉल करें, functions.php फ़ाइल केवल एक बार शामिल होगी, क्योंकि include_once सुनिश्चित करता है कि डुप्लिकेट कोड शामिल न हो।
When we use include_once function:
- Multiple Inclusion: जब आप एक ही फ़ाइल को कई बार शामिल करने का प्रयास कर सकते हैं, लेकिन आप चाहते हैं कि वह केवल एक बार शामिल हो।
- Prevent Duplicate Code: जब आप डुप्लिकेट कोड को रोकना चाहते हैं, विशेषकर फ़ंक्शन या क्लास परिभाषाओं के मामले में।
Important point of php include_once function:
include_once फ़ंक्शन भी चेतावनी जारी करेगा यदि फ़ाइल नहीं मिलती है, लेकिन स्क्रिप्ट का निष्पादन जारी रहेगा।
| Function | Multiple Inclusion | Single Inclusion | File Note Found |
|---|---|---|---|
| include | Yes | No | Warning देता है, लेकिन स्क्रिप्ट चलती रहती है। |
| include_once | No | Yes | Warning देता है, लेकिन स्क्रिप्ट चलती रहती है। |

Welcome to HindiTutorials, your go-to platform for learning and mastering concepts in Hindi! Our mission is to make education accessible and simple for everyone by providing high-quality tutorials on programming, technology, and various other topics.