Using include , include_once , require and require_once in PHP

Last Updated On Saturday 15th Jan 2022

Difference between include and include_once in php

Using include, you can tell PHP to fetch a particular file and load all its contents. It’s as if you pasted the included file into the current file at the insertion point.

include vs include_once

Including a PHP File

	<?php
 include "somewhat.php";
 // Your code goes here
?>
	

include_once in php

Using include_once

Each time you issue the include directive, it includes the requested file again, even if you’ve already inserted it.

Let YourFile.php contains a lot of useful functions, so you include it in your file, but you also include another library that includes YourFile.php.
Through nesting,You include twice.This will produce error messages, because you’re trying to define the same constant or function multiple times. So, you should use include_once instead.

	<?php
 include_once "yourFile.php";
 // Your code goes here
?>
	

include vs require in php

A potential problem with include and include_once is that PHP will only attempt to include the requested file. Program execution continues even if the file is not found.

require_once in php

When it is absolutely essential to include a file, require it. For the same reasons I gave for using include_once, I recommend that you generally stick with require_once whenever you need to require a file.

	<?php
 require_once "yourFile.php";
 // Your code goes here
?>