PHP Comments, Include/Include_once, Require/Require_once

โšก Smart Summary

Comments, include, include_once, require, and require_once are core PHP tools for documenting code and reusing files. This walkthrough explains how single and multi-line comments work and how the four file-inclusion statements share headers, menus, and configuration across many pages.

  • ๐Ÿ“ Why Comment: Comments record what code does so you and other developers can understand it later; aim for roughly three comment lines per ten lines of code.
  • โœ๏ธ Comment Syntax: Single-line comments start with // and end at the line break, while multi-line comments sit between /* and */.
  • ๐Ÿ“„ Include Statement: include and include_once pull another file into the current script, ideal for shared headers and navigation menus.
  • ๐Ÿ”’ Require Statement: require and require_once work like include but stop the script on failure, which suits essential files such as database config.
  • ๐Ÿ” The _once Variants: include_once and require_once skip a file that was already loaded, preventing duplicate declarations and redefinition errors.
  • โš–๏ธ Include vs Require: include only warns and continues on a missing file, while require throws a fatal error and halts execution.
  • ๐Ÿค– AI Assist: AI tools can generate PHPDoc comments, and advise when include or require is the safer choice for a file.

PHP Comments, Include and Require

Why use Comments?

  • If you do not work on the source code for some time, it is easy to forget what the code does. Commenting the source code helps you remember what the code does.
  • Commenting source code is also very important when multiple developers have to work on the same project. The changes made by one developer can be easily understood by other developers by simply reading the comments.
  • As a best practice, you should have 3 lines of comments for every 10 lines of code.

PHP Comments

  • Comments help us to understand the code
  • Comments are explanations that we include in our source code. These comments are for human understanding.
  • Single line comments start with double forward slashes // and they end on the same line.
  • PHP Comments
  • Multiple line comments start with a forward slash followed by the asterisk /* and end with the asterisk followed by the forward slash */.
  • PHP Comments

The diagram below shows a PHP file with both multiple line and single line comments.

PHP Example

PHP Comments Example

PHP Include & PHP Include_once

The “include” PHP statement is used to include other files into a PHP file.

It has two variations, include and include_once. include_once is ignored by the PHP interpreter if the file to be included has already been included.

The include statement has the following syntax

<?php
include 'file_name';
?>

The include_once statement has the following syntax

<?php
include_once 'file_name';
?>

HERE,

  • “include/include_once” is the statement that includes the file
  • “‘file_name'” is the name of the file to be included.

Example: Include / Include_once

Suppose you are developing a website that contains the same navigation menu across all the pages.

You can create a common header, then include it in every page using the include statement. Let us see how this can be done.

  • We will create 2 files named
  • header.php, index.php

Below is the code for 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>

index.php

<?php

include 'header.php';

?>

The header page above will output the navigation links.

PHP Require & PHP require_once

The require statement has two variations, require and require_once.

The require/require_once statement is used to include a file.

require_once is ignored if the required file has already been added by any of the four include statements.

It has the following syntax

<?php
require 'file_name';
?>
<?php
require_once 'file_name';
?>

HERE,

  • “require/require_once” is the statement that includes the file
  • “‘file_name'” is the name of the file to be included.

Example: Require

Suppose we are developing a database-powered application.

We can create a configuration file that we can include in all pages that connect to the database using the require statement. config.php

<?php

$config['host'] = 'localhost';

$config['db'] = 'my_database';

$config['uid'] = 'root';

$config['password'] = '';

?>

Let us now look at the sample code that requires the config file. Pages_model.php

<?php

require 'config.php'; //require the config file

//other code for connecting to the database

?>

PHP include vs require

The difference between include and require is shown below.

Include Require
Issues a warning when an error occurs Issues a fatal error
Execution of the script continues when an error occurs Execution of the script stops when an error occurs.

Generally, it is recommended to use the include statement so that when an error occurs, execution of the script continues to display the webmaster email address or the contact us page.

The require statement should be used if the entire script cannot run without the requested file.

The “include” and “require” statements can be used at any line in the source code where you want the code to appear.

PHP Documentation Comments (PHPDoc)

Beyond single and multi-line comments, PHP developers use PHPDoc block comments to document functions and classes. A PHPDoc block starts with /** and uses tags such as @param and @return that IDEs read to show hints and autocomplete.

<?php
/**
 * Adds two numbers together.
 *
 * @param int $a The first number
 * @param int $b The second number
 * @return int The sum of the two numbers
 */
function add($a, $b) {
    return $a + $b;
}
?>

PHPDoc does not change how the code runs, but it makes the intent of each function clear and lets tools generate API documentation automatically. Adding it to shared files, such as the config and helper files above, keeps a growing codebase maintainable.

FAQs

Both load a file only once, even if called again. The difference is failure handling: include_once emits a warning and lets the script continue, while require_once throws a fatal error and stops execution if the file is missing.

Only if allow_url_include is enabled in php.ini, which is off by default for security. Including remote files is risky because an attacker could inject code, so local file paths are strongly preferred.

No. PHP comments are removed by the interpreter on the server, so they never reach the browser and are not visible in page source. Their effect on performance is effectively zero.

Yes. Given a function, AI can produce a PHPDoc block with a summary plus @param and @return tags. Review the descriptions so they match your intent, since AI infers meaning from names and code.

Yes. AI can review each inclusion and recommend require for essential files, such as configuration, and include for optional ones, such as an extra sidebar, explaining the failure behavior of each.

Summarize this post with: