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: <?php To include the footer file in a page, use the Example<html> <h2>Welcome to my home
page!</h2> </body> Run example » Example 2Assume we have a standard menu file called "menu.php": <?php 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> <div class="menu"> <h2>Welcome to my home page!</h2> </body> Run example » Example 3Assume we have a file called "vars.php", with some variables defined: <?php Then, if we include the "vars.php" file, the variables can be used in the calling file: Example<html> <h2>Welcome to my home page!</h2> </body> Run example » The However, there is one big difference between include and require; when a file is included with the Example<html> <h2>Welcome to my home page!</h2> </body> Run example » If we do the same example using the Example<html> <h2>Welcome to my home page!</h2> </body> Run example » Use Use PHP ExercisesIf we want to use PHP in the Html document, then we have to follow the steps which are given below. Using these simple steps, we can easily add the PHP code. Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to use the PHP. Step 2: Now, we have to place the cursor in any tag of the <body> tag where we want to add the code of PHP. And, then we have to type the start and end tag of PHP. Step 3: After then, we have to type the code of PHP between the tags of PHP. Step 4: When we successfully add the PHP code, then we have to save the Html file and then run the file in the browser. For Videos Join Our Youtube Channel: Join Now Feedback
Help Others, Please Share |