PHP Include And Require - W3Schools
Maybe your like
PHP include and require Statements
The include and require statements take all the text/code/markup that exists in a file, and inserts it into the file that uses the include/require statement.
Including files is very useful for re-using code/text on multiple webpages!
The include and require statements are identical, except upon failure:
- include will produce a warning (E_WARNING) but the script will continue
- require will produce a fatal error (E_ERROR) and the script will stop
If you want the execution to go on and show users the output, even if the include file is missing, use the include statement. This could be wise for files like headers, footers, or navigation menus.
If you want the execution to stop on failure, use the require statement. This is wise for configuration files or database connections.
Syntax
include 'filename'; or require 'filename';PHP Include Footer
Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your webpages. Then, when the header needs to be updated, you can only update the header include file.
Assume we have a standard footer file called "footer.php", that looks like this:
<?phpecho "<p>Copyright © 1999-" . date("Y") . " W3Schools.com</p>";?>To include the footer file in a page, use the include statement:
Example
<html> <body> <h1>Welcome to my home page!</h1> <p>Some text.</p><p>Some more text.</p><?php include 'footer.php';?> </body> </html> Run example »PHP Include Navigation Menu
Assume we have a standard menu file called "menu.php":
<?phpecho '<a href="/default.asp">Home</a> -<a href="/html/default.asp">HTML Tutorial</a> -<a href="/css/default.asp">CSS Tutorial</a> -<a href="/js/default.asp">JavaScript Tutorial</a> -<a href="default.asp">PHP Tutorial</a>';?>All pages in the Web site should use this menu file. Here is how it can be done (we are using a <div> element so that the menu easily can be styled with CSS later):
Example
<html> <body> <div class="menu"><?php include 'menu.php';?></div> <h1>Welcome to my home page!</h1><p>Some text.</p><p>Some more text.</p> </body> </html> Run example »PHP Include Variables
Assume we have a file called "vars.php", with some variables defined:
<?php $color='red'; $car='BMW'; ?>Then, if we include the "vars.php" file, the variables can be used in the calling file:
Example
<html> <body> <h1>Welcome to my home page!</h1> <?php include 'vars.php'; echo "I have a $color $car.";?> </body> </html> Run example »PHP include vs. require
If a file is included with the include statement, and PHP cannot find it, the script will continue to execute:
Example
<html> <body> <h1>Welcome to my home page!</h1> <?php include 'noFileExists.php'; echo "I have a $color $car.";?> </body> </html> Run example »If a file is included with the require statement, and PHP cannot find it, the echo statement will not be executed because the script execution dies after require returns a fatal error:
Example
<html> <body> <h1>Welcome to my home page!</h1> <?php require 'noFileExists.php'; echo "I have a $color $car.";?> </body> </html> Run example »Use require if the include file is required by the application.
Use include if the include file is not required, and the code can continue, even if the include file is not found.
❮ Previous Next ❯ ★ +1 Sign in to track progressTag » Codm Php Not Showing
-
PHP Does Not Display Error Messages - Stack Overflow
-
PHP Logging Basics - The Ultimate Guide To Logging - Loggly
-
MacOS 12.0 -where Oh Where Is PHP | Apple Developer Forums
-
10 Most Common Mistakes That PHP Developers Make - Toptal
-
PHP Programming With Visual Studio Code
-
Troubleshooting Common PHP Debugging Issues | PhpStorm
-
Com - Manual - PHP
-
Tracing PHP Applications - Datadog Docs
-
Tutorial: Install A LAMP Web Server On Amazon Linux 2
-
PHP Error_reporting() Function - W3Schools
-
PHP FastCGI Example - NGINX
-
How To Configure The PHP Environment With PHP Ini - Bluehost
-
Deploying PHP Apps On Heroku