Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

What is Include, Require, Include once, and Require once in PHP?


Include and Include once:-  It is used to include the content of one PHP file to another PHP file, if the file not found then it will provide a warning and show the remaining data.

If we include the same file multiple times then include will display multiple results but include_once will show single result.

for example, if we create a.php file and echo "hello world" then if we include this file into b.php three times then it will display "hello world" three times but include_once will display "hello world" only once.

Example of include PHP:-

a.php code


<?php
echo "hello world";

?>

b.php 

<?php
rinclude("a.php");
include("a.php");

?>

output helloworld helloworld

Example of includeonce PHP:-

a.php code


<?php
echo "hello world";

?>

b.php 

<?php
rinclude_once("a.php");
include_once("a.php");

?>

output helloworld



Require and Require once:-  It is used to include the content of one PHP file to another PHP file, if a file not found then it will provide fatal error and not show remaining data.

If we include the same file multiple times then require will display multiple results but require_once will show single result.

for example, if we create a.php file and echo "hello world" then if we require this file into b.php three times then it will display "hello world" three times but require_once will display "hello world" only once.

Example of require in  PHP:-

a.php code


<?php
echo "hello world";

?>

b.php 

<?php
require("a.php");
require("a.php");

?>


output helloword helloworld

Example of requireonce PHP:-

a.php code


<?php
echo "hello world";

?>

b.php 

<?php
require_once("a.php");
require_once("a.php");

?>

output helloworld


........................................................................................................................................

What is the difference between require and include?

require to provide a fatal error if the file not found but include provide warning message hence if we need urgent requirement of a file then uses require otherwise use include.


What is the difference between include and include-once?

include will display result multiple times for the same file and include once display result only once.




Where we use to include in the project:-


When we create database connectivity then we should always create a connection file separately into the application that will be included on multiple files so that we can easily customize it.










Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post