Comments PHP is usually written within the block of PHP code to explain the functionality of the code. It will help you and others in the future to understand what you were trying to do with the PHP code. Comments are not displayed in the output, they are ignored by the PHP engine. A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is to be read by someone who is looking at the code.
Comments can be used to:
- Let others understand your code
- Remind yourself of what you did – Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code
- If you don’t work on the source code for some time, it’s easy to forget what the code does. Commenting on the source code helps remember what the code does.
- Commenting on 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 the best practice, you must have 3 lines of comments for every 10 lines of code
PHP supports ‘C’, ‘C++’ and Unix shell-style (Perl style) comments. For example:
<?php
echo 'This is a test'; // This is a one-line c++ style comment
/* This is a multi line comment
yet another line of comment */
echo 'This is yet another test';
echo 'One Final Test'; # This is a one-line shell-style comment
?>
The “one-line” comment styles only comment to the end of the line or the current block of PHP code, whichever comes first. This means that HTML code after // ... ?>
or # ... ?>
WILL be printed: ?> breaks out of PHP mode and returns to HTML mode, and //
or #
cannot influence that.
<h1>This is an <?php # echo 'simple';?> example</h1>
]';/*]<p>The header above will say 'This is an example'.</p>
‘C’ style comments end at the first */
encountered. Make sure you don’t nest ‘C’ style comments. It is easy to make this mistake if you are trying to comment out a large block of code.
<?php
/*
echo 'This is a test'; /* This comment will cause a problem */
*/
?>
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 in the same line.
- Multiple line comments start with a forward slash followed by the asterisk /* and end with the asterisk followed by the forward slash */.
The diagram below shows a PHP file with both multiple line and single line comments
PHP 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.
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 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’s see how this can be done.
- We will create 2 files names
- header.php, index.php
Below are the codes 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
PHP Require & PHP require_once
The require statement has two variations, require and require_once.
The require/require_once statement is used to include the file.
Require_once is ignored if the required file has already been added by any of the four included statements.
It has the following syntax
- <?PHP require ‘file_name’; ?>
<?php require_once 'file_name'; ?>
HERE,
- “require/require_once” is the statement that includes 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’s 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 / require
Include | Require |
---|---|
Issues a warning when an error occurs | Does not issue a warning |
Execution of the script continues when an error occurs | Execution of the script stops when an error occurs. |
Generally, it’s 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 required 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 codes where you want the code to appear.
Summary
- Single HTML code such as headers, footers, sidebars, etc. can be shared across many pages. This makes it easy to update the website by just updating a single file.
- PHP code such as database configuration settings, custom functions, etc. can be shared across many pages ensuring the website/application uses the same settings.
- Comments are used to help understand source code. They are for human understanding
- Single line comment statements start with double forward slashes //.
- Multi-line comment statements are enclosed between /* statements */.
- The “include, include_once, require and require_once” statements are used to include files.
- Include_once/require_once is ignored if the requested file has already been included using any of the four statements.
- The “include” statement issues a warning and continues with the execution of the requested file that has not been found.
- The required statement raises a fatal error and stops the script execution.
- The “include” statement should be in most cases except in situations where without the requested file to be included, the entire script cannot run.