What is include () in php?
PHP Include FilesThe Show Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website. PHP include and require StatementsIt is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. The include and require statements are identical, except upon failure:
So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing. Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file. Syntaxinclude 'filename'; or require 'filename'; PHP include ExamplesExample 1Assume we have a standard footer file called "footer.php", that looks like this: echo " Copyright © 1999-" . date("Y") . " W3Schools.com ";?> To include the footer file in a page, use the Example
Welcome to my home page!Some text. Some more text. Run example » Example 2Assume we have a standard menu file called "menu.php": echo 'Home - All pages in the Web site should use this menu file. Here is how it can be done (we are using a element so that the menu easily can be styled with CSS later):
Example
Welcome to my home page!Some text. Some more text. Run example » Example 3Assume we have a file called "vars.php", with some variables defined: $color='red'; Then, if we include the "vars.php" file, the variables can be used in the calling file: Example
Welcome to my home page!echo "I have a $color $car."; ?> Run example » The However, there is one big difference between include and require; when a file is included with the Example
Welcome to my home page!echo "I have a $color $car."; ?> Run example » If we do the same example using the Example
Welcome to my home page!echo "I have a $color $car."; ?> Run example » Use Use PHP Exercises(PHP 4, PHP 5, PHP 7, PHP 8) The The documentation below also applies to require. Files are included based on the file path given or, if none is given, the include_path specified.
If the file isn't found in the include_path, Note that both If a path is defined — whether absolute (starting with a drive letter or For more information on how PHP handles including files and the include path, see the documentation for include_path. When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope. Example #1 Basic
= 'green';
If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function. An exception to this rule are magic constants which are evaluated by the parser before the include occurs. Example #2 Including within functions
'vars.php'; When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags. If "URL include wrappers" are enabled in PHP, you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Supported Protocols and Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script. Example #3
include 'http://www.example.com/file.txt?foo=1&bar=2';// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the Warning Security warningRemote file may be processed at the remote server (depending on the file extension and the fact if the remote server runs PHP or not) but it still has to produce a valid PHP script because it will be processed at the local server. If the file from the remote server should be processed there and outputted only, readfile() is much better function to use. Otherwise, special care should be taken to secure the remote script to produce a valid and desired code. See also Remote files, fopen() and file() for related information. Handling Returns: Because Example #4 Comparing return value of include
Example #5
= 'PHP'; If there are functions defined in the included file, they can be used in the main file independent if they are before return or after. If the file is included twice, PHP will raise a fatal error because the functions were already declared. It is recommended to use include_once instead of checking if the file was already included and conditionally return inside the included file. Another way to "include" a PHP file into a variable is to capture the output by using the
Output Control Functions with Example #6 Using output buffering to include a PHP file into a string
get_include_contents($filename) { In order to automatically include files within scripts, see also the auto_prepend_file and auto_append_file configuration options in php.ini.
See also require, require_once, include_once, get_included_files(), readfile(), virtual(), and include_path. snowyurik at gmail dot com ¶ 13 years ago
Rash ¶ 7 years ago
?> John Carty ¶ 5 years ago
Anon ¶ 10 years ago
error17191 at gmail dot com ¶ 6 years ago
Wade. ¶ 13 years ago
Rick Garcia ¶ 14 years ago
jbezorg at gmail dot com ¶ 4 years ago
Ray.Paseur often uses Gmail ¶ 7 years ago
hyponiq at gmail dot com ¶ 12 years ago
$_SERVER['DOCUMENT_ROOT'])) { ayon at hyurl dot com ¶ 5 years ago
sPlayer ¶ 11 years ago
joe dot naylor at gmail dot com ¶ 11 years ago
Chris Bell ¶ 12 years ago
mbread at m-bread dot com ¶ 15 years ago
durkboek A_T hotmail D_O_T com ¶ 18 years ago
example at user dot com ¶ 14 years ago
('Content-type: image/jpeg'); abanarn at gmail dot com ¶ 8 years ago
uramihsayibok, gmail, com ¶ 14 years ago
Jero Minh ¶ 7 years ago
"Own value before: "; james at gogo dot co dot nz ¶ 18 years ago
$z2; How is include () different to require ()?include() Vs require()
The only difference is that the include() statement generates a PHP alert but allows script execution to proceed if the file to be included cannot be found. At the same time, the require() statement generates a fatal error and terminates the script.
What is difference between include and include_once in PHP?The include() function is used to include a PHP file into another irrespective of whether the file is included before or not. The include_once() will first check whether a file is already included or not and if it is already included then it will not include it again.
How do I include two PHP files?The include() statement is used to include a php file in another file. This way you can write a piece of code in a php file and can use it to multiple files through include() statement.
What is include path in PHP?The PHP Include Path is a set of locations that is used for finding resources referenced by include/require statements. Note: Adding libraries or external projects elements to your project's Include Path will also make elements defined in these resources available as content assist options.
|