PHP Tutorial
- PHP Tutorial
- Install PHP
- PHP Code
- PHP Echo and print
- PHP Variable
- PHP Variable Scope
- PHP $ and $$
- PHP Constants
- PHP Data Types
- PHP Operators
- PHP Comments
Control Statement
- PHP If else
- PHP Switch
- PHP For Loop
- PHP foreach loop
- PHP While Loop
- PHP Do While Loop
- PHP Break
- PHP Continue
PHP Functions
- PHP Functions
- Parameterized Function
- PHP Call By Value and reference
- PHP Default Arguments
- PHP Variable Arguments
- PHP Recursive Function
PHP Arrays
PHP Strings
PHP Math
PHP Form
PHP Include
State Management
PHP File
Upload Download
PHP OOPs Concepts
- OOPs Concepts
- OOPs Abstract Class
- OOPs Abstraction
- OOPs Access Specifiers
- OOPs Const Keyword
- OOPs Constructor and destructor
- Encapsulation
- Final Keyword
- OOPs Functions
- OOPs Inheritance
- OOPs Interface
- OOPs Overloading
- OOPs Type Hinting
PHP MySQLi
- MySQLi CONNECT
- MySQLi CREATE DB
- MySQLi CREATE Table
- MySQLi INSERT
- MySQLi UPDATE
- MySQLi DELETE
- MySQLi SELECT
- MySQLi Order by
PHP Topics
- Compound Types
- is_null() Function
- Special Types
- Inheritance Task
- Special Types
- MVC Architecture
- PHP vs. JavaScript
- PHP vs. HTML
- PHP vs. Node.js
- PHP vs Python
- PHP PDO
- Top 10 PHP frameworks
- phpMyAdmin
- Count All Array Elements
- Create Newline
- Get Current Page URL
PHP Mail
PHP allows us to create various elements and functions, which are used several times in many pages. It takes much time to script these functions in multiple pages. Therefore, use the concept of file inclusion that helps to include files in various programs and saves the effort of writing code multiple times.
“PHP allows you to include file so that a page content can be reused many times. It is very helpful to include files when you want to apply the same HTML or PHP code to multiple pages of a website.” There are two ways to include file in PHP.
- include
- require
Both include and require are identical to each other, except failure.
- include only generates a warning, i.e., E_WARNING, and continue the execution of the script.
- require generates a fatal error, i.e., E_COMPILE_ERROR, and stop the execution of the script.
Advantage
Code Reusability: By the help of include and require construct, we can reuse HTML code or PHP script in many PHP scripts.
Easy editable: If we want to change anything in webpages, edit the source file included in all webpage rather than editing in all the files separately.
PHP include
PHP include is used to include a file on the basis of given path. You may use a relative or absolute path of the file.
Syntax
There are two syntaxes available for include:
- include ‘filename ‘;
- Or
- include (‘filename’);
Examples
Let’s see a simple PHP include example.
- <a href=”http://www.protutorials.com”>Home</a> |
- <a href=”http://www.protutorials.com/php-tutorial”>PHP</a> |
- <a href=”http://www.protutorials.com/java-tutorial”>Java</a> |
- <a href=”http://www.protutorials.com/html-tutorial”>HTML</a>
- <?php include(“menu.html”); ?>
- <h1>This is Main Page</h1>
Output:
Home | PHP | Java | HTML This is Main Page
PHP require
PHP require is similar to include, which is also used to include files. The only difference is that it stops the execution of script if the file is not found whereas include doesn’t.
Syntax
There are two syntaxes available for require:
- require ‘filename’;
- Or
- require (‘filename’);
Examples
Let’s see a simple PHP require example.
File: menu.html
- <a href=”http://www.javaprotutorials.com”>Home</a> |
- <a href=”http://www.protutorials.com/php-tutorial”>PHP</a> |
- <a href=”http://www.protutorials.com/java-tutorial”>Java</a> |
- <a href=”http://www.protutorials.com/html-tutorial”>HTML</a>
File: require1.php
- <?php require(“menu.html”); ?>
- <h1>This is Main Page</h1>
Output:
Home | PHP | Java | HTML This is Main Page
PHP include vs PHP require
Both include and require are same. But if the file is missing or inclusion fails, include allows the script to continue but require halts the script producing a fatal E_COMPILE_ERROR level error.
Let’s understand the difference with the help of example:
Example
- <?php
- //include welcome.php file
- include(“welcome.php”);
- echo “The welcome file is included.”;
- ?>
Output:
The welcome.php file is not available in the same directory, which we have included. So, it will produce a warning about that missing file but also display the output.
Warning: include(welcome.php): failed to open stream: No such file or directory in C:\xampp\htdocs\program\include.php on line 3 Warning: include(): Failed opening 'welcome.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\program\include.php on line 3 The welcome file is included.
- <?php
- echo “HELLO”;
- //require welcome.php file
- require(“welcome.php”);
- echo “The welcome file is required.”;
- ?>
Output:
In case of require() if the file (welcome.php) is not found in the same directory. The require() will generate a fatal error and stop the execution of the script, as you can see in the below output.
HELLO Warning: require(Welcome.php): failed to open stream: No such file or directory in C:\xampp\htdocs\program\include.php on line 3 Fatal error: require(): Failed opening required 'Welcome.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\program\include.php on line 3