php

PHP require_once() Function

When a PHP script is used in multiple files for programming purposes, then it is better to write the script in one file and include the file in those files without writing where the script is required. PHP has many built-in functions to include any existing file in a script. These are require(), require_once(), include(), and include_once().

The task of require() and require_once() are the same, but the one difference is that the require() function does not check the file has been included before or not but the require_once() function check the file has been included before or not. If the file has been included in the script before, then the require_once() function will not include the same file again. How the require_once() function is used to include files in PHP has been shown in this tutorial.

Syntax

require_once( string $filepath);

or

require_once 'The path of the file';

This function has one argument that contains the path of the file that will be included. If the path does not exist, then this function will generate an error. If the path exists and the file has not been included before in the script then it will include the file and returns true, otherwise, it will just return true.

Different Uses of require_once() Function

Create a PHP file named arithmetic.php with the following script to check the use of the require_once() function. Four functions have been declared in the script to perform addition, subtraction, multiplication, and division operations. This file has been used in the examples shown in this part of the tutorial.

<?php

//Add function

function Addition($a, $b)

{

    return $a + $b;

}

//Subtract function

function Subtraction($a, $b)

{

    return $a - $b;

}

//Multiply function

function Multiplication($a, $b)

{

    return $a * $b;

}

//Division function

function Division($a, $b)

{

    return $a / $b;

}

?>

Example-1: Use of require_once() to Include an Existing File

Create a PHP file with the following script to show the way of including a file in the script. The arithmetic.php file that is created before has been included at the beginning of the script. Two integer variables have been declared and called the Addition() and Multiplication() functions that have been defined in the arithmetic.php file to add and multiply the values of the variables.

<?php


//Include the file

require_once('arithmetic.php');


//Assign two numeric values

$number1 = 50;

$number2 = 40;


//Call the Addition() function

echo "The sum of $number1 and $number2 is ".Addition($number1, $number2);

//Call the Multiplication() function

echo "<br/>The multiplication of $number1 and $number2 is ".Multiplication($number1, $number2);


?>

Output:

The following output will appear after executing the above script. The output shows the addition and the multiplication results.

Example-2: Use of Require_once() to Include an Existing File Multiple Times

Create a PHP file with the following script to check how the script works if the require_once() function is used multiple times to include the same file. The arithmetic.php file has been included two times in the script. Next, the Addition() and Multiplication() functions have been called to do the same task that has been shown in the previous example.

<?php


//Include the file
 
require_once('arithmetic.php');


//Assign two numeric values

$number1 = 50;

$number2 = 40;


//Include the file again

require_once('arithmetic.php');



//Call the Addition() function

echo "The sum of $number1 and $number2 is ".Addition($number1, $number2);

//Call the Multiplication() function

echo "<br/>The multiplication of $number1 and $number2 is ".Multiplication($number1, $number2);


?>

Output:

The following output will appear after executing the above script. The output shows the addition and the multiplication results without any error.

Example-3: Use of require_once() to Include a File that Does Not Exist

Create a PHP file with the following script to check how the require_once () function works if the file that is used in the function does not exist. Here, the concat.php file has been used in the require_once() function that does not exist.

<?php


//Include the file that does not exist

require_once('concat.php');


//Assign two string values

$string1 = "Hello";

$string2 = "World";


echo "$string1 $string2";



?>

Output:

The following output will appear after executing the above script. The output shows a warning message and a fatal error for the non-existing file.

Example-4: Use of require_once() After Checking the Existence of the File

The error shown in the previous example can be removed by checking the existence of the file before including the file. Create a PHP file with the following script that will check the existence of the file before using the require_once() function. The built-in PHP function, file_exists() has been used in the script to check the existence of the arithmetic.php file.

If this function returns true the arithmetic.php file will be included in the script by using the require_once() function and the Addition() function will be called to calculate the sum of two float numbers.

The file_exists() function has been used for the concat.php file also before including. If the file exists then two string values will be concatenated by using the combine_str() function. An error message will be displayed if the file_exists() function will return false.

<?php


$filename = 'arithmetic.php';

if(file_exists($filename))

{


    //Include the file

    require_once($filename);


    //Assign two numeric values

    $number1 = 5.67;

    $number2 = 4.23;


    //Call the Addition() function

    echo "The sum of $number1 and $number2 is ".Addition($number1, $number2);

}

else

    echo "The $filename does not exist.";



$filename = 'concat.php';

if(file_exists($filename))

{


    //Include the file

    require_once($filename);


    //Assign two string values

    $string1 = "Linux";

    $string2 = "Hint";


    //Call the combine_str() function

    echo "The concatenation value is ".combine_str($string1, $string2);

}

else

    echo "<br/>The $filename does not exist.";




?>

Output:

The following output will appear after executing the above script. The output shows that the arithmetic.php file exists and the concate.php file does not exist.

Conclusion

The require_once() is a very useful function to include any script inside another PHP script instead of writing the same script in multiple files. The way to use this function has shown in this tutorial by using various examples to help the new PHP user.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.