In today’s topic, we will discuss PHP Comments, Require, Include. These are very important concepts that are necessary to remember before writing a PHP program. Previously we discussed the operator in php. If you haven’t seen it, hurry up
What are Comments in PHP?
In PHP, comments are the most essential parts of the code and they help us to understand the code. Comments supply helpful information that will help you and other developers understand the meaning of the code.
A comment in PHP code is a line that will not execute as a part of the program. Its only aim is to be read by someone who is looking at the code.
Remember that comment in PHP code is a line in which it will not execute as a part of the program
In PHP, there are mostly two types of comments and they are
- Single line Comment
- Multi-line Comment
Single line Comment
Double forward slashes ‘//’ are used to start Single-line comments and they end in the same line.
<!DOCTYPE html> <html> <body> <?php // This is a single-line comment # This is also a single-line comment // GeektoCode.com ?> </body> </html>
A forward slash followed by the asterisk ‘ /*’ is used to start Single-line comments and they end in the same line.
<!DOCTYPE html> <html> <body> <?php /* This is a multiple-lines comment block GeektoCode.com */ ?> </body> </html>
Include variations in PHP
To include other files into a PHP file we need the ‘include’ statement. It means we can insert the content of one PHP file into another PHP file before the server executes it.
Syntax: include ‘Filename.php’;
<?php include 'header.php'; ?>
Lets we develop a website that contains the same navigation menu across all the pages. Then we create a common header then include it on every page using the include statement, then let’s see how it will be done.
We will create 2 files named as
First is header.php (It contains the navigation bar)
The Second is index.php
The code was written below:
Code for the header.php
<a href="index.php">Home</a> <a href="aboutus.php">About us</a> <a href="services.php">Services</a> <a href="contactus.php">Contact Us</a>
Code for the Index.php
Remember that include is used when the file is not required and the application should continue when a file is not found.
<?php include 'header.php'; ?>
Header will be the OUTPUT
Require variations in PHP
Suppose we develop a database-based application. We can create a Dbconfiguration file that we can include in all pages that connect to the database using the require statement. i.e config.php
Lets we create a file dbconfig.php, with codes given below
<?php $config['host'] = 'localhost'; $config['db'] = 'my_database'; $config['uid'] = 'root'; $config['password'] = ''; ?>
Then create another file where we connect dbconfig.php, filename- Index.php
Remember that require is used when the file is required by the application.
<?php require 'config.php'; //require the config file //other code for connecting to the database ?>
Difference Between Include and Require
|A warning is issued when an error occurs||Execution of the script does not stops when an error occurs|
|Does not issue a warning||When an error occurs the execution of the script stops|
You Might Like:
- Introduction To PHP
- XAMPP Installation
- Data Types in PHP
- Variable in PHP
- Operators in PHP
- Constant and String Function in PHP