PHP Comments
PHP Comments
If you don’t work on the source code for some time, it’s easy to forget what the code
does. Commenting the source code helps 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 the best practice, you must have 3 lines of comments for every 10 lines of code
PHP Comments
The diagram below shows a PHP file with both multiple line and single line comments
PHP Example
It has two variations, include and include_once. Include_once is ignored by the PHP
interpreter if the file to be included.
<?php
include 'file_name';
?>
<?php
include_once 'file_name';
?>
HERE,
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.
<a href="index.php">Home</a>
<a href="services.php">Services</a>
index.php
<?php
include 'header.php';
?>
Require_once is ignored if the required file has already been added by any of the four
include statements.
<?php
require 'file_name';
?>
<?php
require_once 'file_name';
?>
HERE,
Example : Require
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
?>
Include Require
Issues a warning when an error occurs Does not issue a warning
Execution of the script continues when an Execution of the script stops when an
error occurs error occurs.
Generally, it’s recommended using 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 codes where
you want the code to appear.