Skip to main content

PHP include() Vs require()

Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website. For example, if we want to create an instance of a class defined in the separate class file, then it has to be included before creating an instance of it. PHP provides various functions to include external files.
The following list of functions is used to include external file into a PHP program.n this tutorial, we are going to compare these functions with suitable examples. Also, we are going to see the purpose of the _once usage and the difference between include and include_once/require and require_once.
  • include()
  • require()
  • include_once()
  • require_once()
include():
PHP include() function includes external file into a PHP program. It accepts the external file path and checks if the file exists or not. If the file does not exist in the specified path, then the include() will return PHP warning.
Warning: failed to open stream: No such file or directory...
Warning: Failed opening ... for inclusion...
By including an external file by using the PHP include() function, the variable, functions, and classes of the included file can be used in the program where it is included. The following code shows an example for including an external file using PHP include().
<?php
include("../file_name.php"); // relative path
//OR
include("/xampp/hddocs/file_name.php"); // absolute path
?>
If we include the same file multiple time by using this function, then it will cause a PHP error.
require():
PHP require()  function is as similar as include() function. But, the difference is, the require() function will return a fatal error and stop executing the program at the time of failure where the include() function returns warning and continue execution.
Warning: failed to open stream: No such file or directory...
Fatal Error: Failed opening required...
The code to include file using require() function is,
<?php
require("../file_name.php"); // relative path
//OR
require("/xampp/hddocs/file_name.php"); // absolute path
include_once() and require_once():
The include_once() and require_once() functions are similar to the include() and require() functions, respectively. But, using this function will create difference at the time of including the same file for multiple time. By using include_once() and require_once() functions, will include the specified file if it is not already included, otherwise, PHP will ignore this statement. The code is,
<?php
include_once("../file_name.php"); // relative path
<?php
require_once("../file_name.php"); // relative path
Hope you understand it.If any queries please comment below.Please subscribe to other solutions.Thank you :)

Comments

Popular posts from this blog

MYSQL CONNECTION USING PDO WITH PHP

Hello guys, In the previous tutorial we have seen how to connect to MySQL database with PHP using MySQLi. Today we will learn how to connect with PDO. As a PHP programmer we have learned how to access database by using either MySQL and MySQLi. As of PHP 5.1, there's a better way. PHP Data Objects(PDO) provide methods for prepared statements and working with objects that will make you far more productive! PDO Introduction :        PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. Database Support:       The extension can support any database that a PDO driver has been written for. The following drivers currently implement the PDO interface: PDO_CUBRID Cubrid PDO_DBLIB FreeTDS / Microsoft SQL Server / Sybase PDO_FIREBIRD Firebird PDO_I...

Convert JSON to ARRAY in PHP

 To convert an Array to JSON in PHP, we use json_encode() function. The function is used to encode a value to JSON format $arr = array ( 'Millie' => 'Eleven' ,             'Mike' => 'Finn' ,             'Gaten' => 'Dustin' ,             'Noah' => 'Will' ); echo json_encode ( $arr ). " \n " ; Output : { "Millie" : "Eleven" , "Mike" : "Finn" , "Gaten" : "Dustin" , "Noah" : "Will" }

MYSQL DATABASE CONNECTION WITH PHP

Hello friends, today we will learn how to connect to  MySQL  database with PHP. This tutorial will require a MySQL database and a web development environment using mysql, apache and php and a simple text editor. The tutorial takes you through establishing a  MySQL  connection using php on a web page, connecting to a  MySQL  table and retrieving the results and displaying them back on the web page. Earlier versions of PHP used the  MySQL  extension.However, this extension was depreciated in 2012. Later can work with  MySQL  database using MySQLi extension (the "i" stands for improved) PDO (PHP Data Objects)        The main difference between these two are if you want to switch your project to another database PDO makes it easy(since it can connect 12 different databases). You only have to change the connection string and a few queries. With MySQLi, you will need to rewrite the entire code - queries included.In this t...