আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML, CSS PHP,MySQL,JavaScript, AJAX, JQUERY, Bootstrap and Live Project কোর্সের ৮৭ তম ব্যাচ এবং ২৬ ডিসেম্বর-২০১৭ তারিখ থেকে শুরু হচ্ছে চার মাস ব্যাপী Zend PHP-7 Certified PHP Engineering (Advance PHP) কোর্সের ৩৫ তম ব্যাচ। প্রত্যেকটি কোর্স এর ফী নির্ধারণ করা হয়েছে ৩০,০০০/= আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722 81 75 91

PHP Control Structure Part -5: PHP File Inclusion Functions

file inclusion

What is File Inclusion Functions in PHP?

In PHP, Include, require, include_once, and require_once functions is used for add file contents to the current file. However, if you want to include any remote PHP server file at your own server, then you must give permission to the allow_url_include on php.ini file. However, allow_url_include is harmful on the website’s security.

How to use include, include_once and require, require_once functions on PHP?

Include, include_once and require, require_once functions are as like echo and print language construct, so you can use these functions as well as with parentheses or without parentheses. Notice the following examples:

<?php
include("a.php"); //with parentheses 
//Or
include "a.php"; //without parentheses 
?>

In the line where you want to include the file, the file will work in the following lines. Now let us see how include function works in the following example:

<?php
//vars.php
 
$color = 'green';
$fruit = 'apple';
 
?>
 
 
<?php
//test.php
 
echo "$color $fruit"; // will show warning
 
include 'vars.php';
 
echo "$color $fruit"; // Result: green apple
 
?>

include function also can use in inside user defined function. In this case, the user defined function will be included only after the call. Notice the following examples:

<?php
//vars.php
 
$color = 'green';
$fruit = 'apple';
 
?>
<?php
//foo.php
function foo(){
    include 'vars.php';
    echo "A $color $fruit";
}
foo();                    // A green apple
?>

We also can include the file via http request in PHP. See the example below:

include 'http://www.example.com/file.php?foo=1&bar=2';

What is the difference between require and require_once, include and include_once function in PHP?

There are many differences between require and require_once, include and include_once function in PHP. Notice the following table:

requires and include VS require_once and include_once
include()It includes a specified file. It will produce a warning if it fail to find the file and execute the remaining scripts

require():It includes a specified file. It will produce a fatal error if it fail to find the file and stops the execution

include_once()It includes a specified file. a file has already been included, it will not be included again. It will produce a a warning if it fail to find the file and execute the remaining scripts.

require_once(): It includes a specified file. a file has already been included, it will not be included again. It will produce a a fatal error if it fail to find the file and stops the execution.

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.

Leave a Reply

Your email address will not be published. Required fields are marked *