PHP Comments, Require, Include

We will talk about PHP Comments, Require, and Include them in today’s topic. Before building a PHP application, it is essential to understand some crucial ideas. We already spoke about PHP’s operator. Hurry if you haven’t seen it already.

What are Comments in PHP?

The most crucial components of PHP code are comments, which also aid in code comprehension. You and other developers will benefit from the comments’ useful information as it clarifies the meaning of the code.
A line of PHP code that is commented out will not be used by the program. Its sole purpose is to be read by someone who is reviewing the code.

Keep in mind that a PHP comment is a line of code that will not be executed as part of the programme.

There are primarily two types of comments in PHP, and they are

  • Single line Comment
  • Multi-line Comment

Single line Comment

Single-line comments begin with double forward slashes “//” and terminate on the same line.

Example

<!DOCTYPE html>
<html>
<body>
 <?php
// This is a single-line comment
# This is also a single-line comment
// oratadalafil.com
?>
</body>
</html>

Multi-line Comment

Single-line comments begin with double forward slashes “//” and terminate on the same line.

<!DOCTYPE html>
<html>
<body>
<?php
/*
This is a multiple-lines comment block
oraltadalafil.com
*/
?> 
</body>
</html>

Include variations in PHP

The “include” statement is required in order to include other files in a PHP file. It means that before the server executes a PHP file, we might add its content to another PHP file.

Syntax: include ‘Filename.php’;

 <?php

 include 'header.php';

 ?>

Example:

Let’s create a website with a single navigation menu that appears on all pages. Let’s examine how it will be done once we establish a standard header and use the include statement to include it on every page.

We’ll make two files with the names

First is header.php (It contains the navigation bar)

The Second is index.php

The code appeared as follows:

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

It’s important to keep in mind that include is used when a file is not necessary and that the application should continue if a file cannot be located.

<?php
include 'header.php';
?>
 Header will be the OUTPUT

Require variations in PHP

Let’s say we create a database-driven application. We can construct a Dbconfiguration file that we can include on each page that uses the required statement to connect to the database. such as config.php

Let’s create a file called dbconfig.php using the codes 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

Keep in mind that require is used when the application needs the file.

<?php
require 'config.php'; //require the config file
//other code for connecting to the database
?>

Difference Between Include and Require

IncludeRequire
An error results in the issuance of a warning.When an error occurs, the script’s execution does not stop.
does not sound the alarmThe script’s execution is interrupted by errors.

For more read with us, Oraltadalafil

Read our previous post

1 thought on “PHP Comments, Require, Include”

Leave a Comment

%d bloggers like this: