PHP require Statement

PHP require Statement

Use the PHP require statement to include a file into your PHP code. The script execution stops, if the file to be included isn’t found. Let’s say you need to add a section on the website, which is repeated on every page, for example, footer or sidebar. For that, add it using the PHP require() statement and eliminate the need of adding separate Lines of Code again and again.

Here’s the syntax of require() statement,

Now, we will learn with an example. We have two files, credits.php and index.php.

Here’s the code for index.php. Here, we are using require to include file credits.php,

Here’s the code for credits.php. This file gets added to the index.php file, since we included it using require previously in index.php,

Here’s the output,

PHP require function

Now, we will see another example, where a file powered.php isn’t part of our website is included. Here, you can see, nothing gets displayed in output, only an error is visible, since we’re using require(),

Here’s the code for new.php. This file includes a file powered.php using require,

As shown above, we do not have a file, “powered.php”, so the text, “This won’t get printed, since we’re trying to access a file that does not exist” won’t get printed. Only an error can be seen as shown below,

PHP Error

PHP Sessions
PHP include Statement
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create free tutorials for all.

No Comments

Post A Comment