Include and Require

Used to insert the contents of the target PHP file at the place from where it was called in the calling PHP file.

 

Four variations:

  • include()  upon failure will generate a warning, but the script will continue executing the remaining code.
  • include_once() identical to above, but will check if the file has already been included, and therefore not include it again.
  • require()  upon failure will generate a fatal error and halt execution.
  • require_once()  identical to above, but will check if the file has already been included, and therefore not require it again.

 

<?php
	$myBike = "Ducati" ;
	$speed = 188 ;
?>

<?php
	echo "My $myBike has a top speed of $speed mph<br>";

	include(variables.php) ;

	echo "My $myBike has a top speed of $speed mph<br>";
?>

Displays:

My has a top speed of mph
My Ducati has a top speed of 188 mph

Leave a Reply