PHP Comments, Include/Include_once, Require/Require_once
Today We are going to see Comments in PHP & Including other PHP files in PHP.
PHP comments are used to give more idea to code reader about code. This will help another coder about the code done by previous user. This is very common in medium scale or big scale project where multiple users are working on the same project code, So it is recommended to follow the good coding standards , where Commenting code will help another user to understand the code. Comments not get executed, those are dead lines in respect to coding.
One more important point is that , if your profession is coding then you may do many codes , for may projects. Then you may needs to reuse , or re check your old code in future, so proper commenting will help you to minimize your efforts to understand your old code with less stress to you.
Next Use of Commenting is required , if you or your organization wants sell your code, then keep in mind the purchaser will hire another freelancer or coder or company to edit that code. So it’s good practice to have well commented code, that will help those coders. It also impact your product sell, when your code script is your product.
So hope with above three paragraphs you better understood the importance of commenting, right now you may thought why you not start the PHP learning fast n finish this PHP course, be calm purpose of this series is to teach you every thing as good as possible.
Types of comments
- Single line comments start with double forward slashes // and they end in the same line.
- // this is commented line, only for this single line.
- Multiple line comments start with a forward slash followed by the asterisk /* and end with the asterisk followed by the forward slash */.
- /* I am
- a multiline comment.
- programmer hates to write me, but some really needs me
So that’s all about I wanted to tell you about comments.
PHP Include & PHP Include_once
Repeating code ?, yes while coding project you needs to use same functions , code of lines , data base connections across different pages of PHP, so every time you need to write same code again n again????????
NO , it was story with plain html, here in PHP you can include code written in one file in to other file with include & require functions, isn’t it amazing??
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 is already included.
The include statement has the following syntax
The include_once statement has the following syntax
- “Include/include_once” is the statement that includes file
- “‘file_name’” is the name of the file to be included (With extension).
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>
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 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
- “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
<?phprequire 'config.php'; //require the config file//other code for connecting to the database?>
Php include vs require
The difference between include / require
IncludeRequireIssues a warning when an error occursDoes not issue a warningExecution of the script continues when an error occursExecution of the script stops when an 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.
- Single HTML code such as headers, footers, side bars 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 if the requested file has not been found.
- The require 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 include, the entire script cannot run.