w3resource

PHP require_once(), include_once()

PHP require_once()

Description

require_once() statement can be used to include a php file in another one, when you may need to include the called file more than once. If it is found that the file has already been included, calling script is going to ignore further inclusions.

If a.php is a php script calling b.php with require_once() statement, and does not find b.php, a.php stops executes causing a fatal error.

Syntax:

require_once('name of the calling file with path');

Example:

<?php
echo "today is:".date("Y-m-d");
?>

The above file is x.php

The above file x.php, is included twice with require_once() statement in the following file y.php. But from the output you will get that the second instance of inclusion is ignored,  since require_once() statement ignores all the similar inclusions after the first one.

<?php
require_once('x.php');
require_once('x.php');
?>

View this example in the browser

If a calling script does not find a called script with the require_once statement, it halts the execution of the calling script.

PHP include_once()

Description

The include_once() statement can be used to include a php file in another one, when you may need to include the called file more than once. If it is found that the file has already been included, calling script is going to ignore further inclusions.

If a.php is a php script calling b.php with include_once() statement, and does not find b.php, a.php executes with a warning, excluding the part of the code written within b.php.

Syntax:

include_once('name of the called file with path');

Example:

<?php
echo "today is:".date("Y-m-d");
?>

The above file is x.php

The above file x.php, is included twice with include_once() statement in the following file y.php. But from the output you will get that the second instance of inclusion is ignored,  since include_once() statement ignores all the similar inclusions after the first one.

<?php
include_once('x.php');
include_once('x.php');
?>

View the example in the browser

If a calling script does not find a called script with the include_once statement, it halts the execution of the calling script.

Previous: include and require
Next: PHP User Define Function



PHP: Tips of the Day

Convert HTML + CSS to PDF

Important: Please note that this answer was written in 2009 and it might not be the most cost-effective solution today in 2019. Online alternatives are better today at this than they were back then.

Here are some online services that you can use:

  • PDFShift
  • Restpack
  • PDF Layer
  • DocRaptor
  • HTMLPDFAPI
  • HTML to PDF Rocket

Have a look at PrinceXML.

It's definitely the best HTML/CSS to PDF converter out there, although it's not free (But hey, your programming might not be free either, so if it saves you 10 hours of work, you're home free (since youalso need to take into account that the alternative solutions will require you to setup a dedicated server with the right software).

Ref : https://bit.ly/384yytr