- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP require() function
The PHP require() function is used when we need to require a file to execute first, before the execution of PHP scripts after the require(). For example:
<?php require("myfile.php"); echo "PHP is Fun!<BR>"; echo "Is not it?"; ?>
The script for the file myfile.php is:
<?php echo "This is text inside \"myfile.txt\" file<HR>"; ?>
Now the output of the above PHP example on the require() function is shown in the snapshot given below:
PHP require() Syntax
The syntax of the require() function in PHP is:
require 'file';
Or
require('file');
Unlike include(), upon failure, the include() function produce a fatal error (E_COMPILE_ERROR), that halts the current PHP script to execute further. For example:
<?php echo "Before require()<BR>"; require("unknown.php"); echo "After require()<BR>"; ?>
The output produced by this PHP example on require() is:
See the output; the script before the require() function has been executed, whereas the script after the function has not been executed, as the specified file does not exist.
Why use require() in PHP?
I do not know about your purpose for using the require() function. But this function plays an important role when your application needs to execute some script (from an external file) before executing the current PHP script. For example:
<?php require 'dbConnection.php'; $sql = "SELECT * FROM customer"; $result = $conn -> query($sql); if($result) { while($row = $result -> fetch_row()) { echo "Name: ", $row[1]; echo "<BR>"; echo "Age: ", $row[2]; echo "<HR>"; } $result -> free_result(); } else { echo "Something went wrong!<BR>"; echo "Error Description: ", $conn -> error; } $conn -> close(); ?>
And here is the script of the file named dbConnection.php:
<?php $server = "localhost"; $user = "root"; $pass = ""; $db = "fresherearth"; $conn = new mysqli($server, $user, $pass, $db); if($conn -> connect_errno) { echo "Connection to the database failed!<BR>"; echo "Reason: ", $conn -> connect_error; exit(); } ?>
Now the output produced by the above example should be:
That is, before a successful connection to the database, the script does not execute.
Note: The mysqli() function is used to open a connection to the MySQL database server, in object-oriented style.
Note: The new keyword is used to create a new object.
Note: The connect_errno is used to get or return the error code (if any) from the last connect call in object-oriented style.
Note: The connect_error is used to get the error description (if any) from the last connection in object-oriented style.
Note: The exit() function is used to terminate the execution of the current PHP script.
Note: The query() function is used to perform queries on the MySQL database in object-oriented style.
Note: The fetch_row() function is used to fetch and return the result as an enumerated array in object-oriented style.
Note: The free_result() function is used to free the stored result in object-oriented style.
Note: The error is used to return the description of the error (if any) by the most recent function call in object-oriented style.
Note: The close() function is used to close an opened connection in object-oriented style.
Note: The require() function is used most of the time to include key file(s) to avoid compromising the security of web applications written in PHP. That is, if the key file is missing, then the entire script does not get executed.
« Previous Tutorial Next Tutorial »