Verification: a143cc29221c9be0

Php code for adding comments

Why use 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

In this tutorial, you will learn-

  • PHP Comments
  • PHP Include & PHP Include_once
  • Example : Include / Include_once
  • PHP Require & PHP require_once
  • Example : Require
  • PHP include vs require

PHP Comments

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

The include_once statement has the following syntax

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

Home

About us

Services

Contact Us

index.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 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

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

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

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 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.

What is Form?

When you login into a website or into your mail box, you are interacting with a form.

Forms are used to get input from the user and submit it to the web server for processing.

 The diagram below illustrates the form handling process.

A form is an HTML tag that contains graphical user interface items such as input box, check boxes radio buttons etc.

The form is defined using the

...
tags and GUI items are defined using form elements such as input.

In this tutorial, you will learn-

  • When and why we are using forms?
  • Create a form
  • POST method
  • GET method
  • GET vs POST Methods
  • Processing the registration form data
  • More examples

When and why we are using forms?

  • Forms come in handy when developing flexible and dynamic applications that accept user input.
  • Forms can be used to edit already existing data from the database

Create a form

We will use HTML tags to create a form. Below is the minimal list of things you need to create a form.

  • Opening and closing form tags
  • Form submission type POST or GET
  • Submission URL that will process the submitted data
  • Input fields such as input boxes, text areas, buttons,checkboxes etc.

The code below creates a simple registration form



	Registration Form

Registration Form

First name:
Last name:

Viewing the above code in a web browser displays the following form.

HERE,

  • are the opening and closing form tags
  • action="registration_form.php" method="POST"> specifies the destination URL and the submission type.
  • First/Last name: are labels for the input boxes
  • are input box tags

  • is the new line tag
  • is a hidden value that is used to check whether the form has been submitted or not
  • is the button that when clicked submits the form to the server for processing

Submitting the form data to the server

The action attribute of the form specifies the submission URL that processes the data. The method attribute specifies the submission type.

PHP POST method

  • This is the built in PHP super global array variable that is used to get values submitted via HTTP POST method.
  • The array variable can be accessed from any script in the program; it has a global scope.
  • This method is ideal when you do not want to display the form post values in the URL.
  • A good example of using post method is when submitting login details to the server.

It has the following syntax.

  HERE,

  • “$_POST[…]” is the PHP array
  • “'variable_name'” is the URL variable name.

PHP GET method

  • This is the built in PHP super global array variable that is used to get values submitted via HTTP GET method.
  • The array variable can be accessed from any script in the program; it has a global scope.
  • This method displays the form values in the URL.
  • It’s ideal for search engine forms as it allows the users to book mark the results.

It has the following syntax.

  HERE,

  • “$_GET[…]” is the PHP array
  • “'variable_name'” is the URL variable name.

GET vs POST Methods

POST GET
Values not visible in the URL Values visible in the URL
Has not limitation of the length of the values since they are submitted via the body of HTTP Has limitation on the length of the values usually 255 characters. This is because the values are displayed in the URL. Note the upper limit of the characters is dependent on the browser.
Has lower performance compared to Php_GET method due to time spent encapsulation the Php_POST values in the HTTP body Has high performance compared to POST method dues to the simple nature of appending the values in the URL.
Supports many different data types such as string, numeric, binary etc. Supports only string data types because the values are displayed in the URL
Results cannot be book marked Results can be book marked due to the visibility of the values in the URL

The below diagram shows the difference between get and post

 

Processing the registration form data

The registration form submits data to itself as specified in the action attribute of the form.

 When a form has been submitted, the values are populated in the $_POST super global array.

We will use the PHP isset function to check if the form values have been filled in the $_POST array and process the data.

We will modify the registration form to include the PHP code that processes the data. Below is the modified code



	Registration Form //this code is executed when the form is submitted

        

Thank You

You have been registered as

Go back to the form

Registration Form

First name:
Last name:

  HERE,

  • checks if the form_submitted hidden field has been filled in the $_POST[] array and display a thank you and first name message.

    If the form_fobmitted field hasn’t been filled in the $_POST[] array, the form is displayed.

More examples

Simple search engine

We will design a simple search engine that uses the PHP_GET method as the form submission type.

For simplicity’s sake, we will use a PHP If statement to determine the output.

We will use the same HTML code for the registration form above and make minimal modifications to it.



	Simple Search Engine

Search Results For

The GET method displays its values in the URL

Sorry, no matches found for your search term

Go back to the form

Simple Search Engine - Type in GET

Search Term:

View the above page in a web browser

The following form will be shown

Type GET in upper case letter then click on submit button.

The following will be shown

The diagram below shows the URL for the above results

Note the URL has displayed the value of search_term and form_submitted. Try to enter anything different from GET then click on submit button and see what results you will get.

Working with check boxes, radio buttons

If the user does not select a check box or radio button, no value is submitted, if the user selects a check box or radio button, the value one (1) or true is submitted.

We will modify the registration form code and include a check button that allows the user to agree to the terms of service.



	Registration Form

You have not accepted our terms of service

Thank You

You have been registered as

Go back to the form

Registration Form

First name:
Last name:
Agree to Terms of Service:

View the above form in a browser

Fill in the first and last names

Note the Agree to Terms of Service checkbox has not been selected.

Click on submit button

You will get the following results

Click on back to the form link and then select the checkbox

Click on submit button

You will get the following results