How to Use Include , Include_once ,Require and Require_once in PHP

PHP

Include
The include() statement includes specified file.

Include Once
The include_once() statement includes specified file during the execution of the script. Once the file is already Included then it will not include same file again.

Require
require() and include() are same but they does not include the failure . Require() results in a Fatal Error. So you can use require() in the file is not available.

Require Once
The require_once() statement includes specified file during the execution of the script. Once the file is already Included then it will not include same file again.

Hence there is not much diffrent between Include and require.

Require is the best way to use other that Include . Because if the file will not be available then we can get the error and we can solve that error while in include we will not understand the error.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Include , Include Once ,Require and Require Once Examples in PHP</title>
</head>
<body>

<?php

//Include and Include Once Example
include "test.php"; // It will include the test.php

include_once "myfile.php"; // this will include myfile.php

include_once "myfile.php"; // Once the file is already included then it will not include again

include_once "nofileavailable.php"; //If file is not available on the perfect path then it will give error message

//Require and Require Once Example

require "test.php"; // It will include the test.php

require_once "myfile.php"; // this will include myfile.php

require_once "myfile.php"; // Once the file is already included then it will not include again

require_once "nofileavailable.php"; //If file is not available on the perfect path then it will give error message

?>

</body>
</html>
-->