Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Working with PHP Functions and Loops

This article will discuss about how to work with php functions and loops along with their usage.

Today, we are going to learn about various php functions and loops along with their usage.

Introduction

There are number of functions in php which can be used to make your life easy. Functions and loop both helps you reduce the coding work and also make the code clean. Moreover function like “include and require”(will be discussed) make the code reuse possible to maximum level in php

Today we will be mainly discussing three main php function which are:

  1. include(really important)
  2. require(really important)
  3. Date function

After discussing functions we will discuss the various types of loops:

  1. For loop
  2. While loop
  3. do-while loop
  4. for each loop

First we start with discussion of some important php functions.

include function

Include functions let you include a file in your webpage. This is really helpful when you want to include a particular header or footer on your webpage. Just adding an include function will save you from writing too much code.

Listing 1: PHP Include Function

<html>
<head>
	<title>MrBool Tutorial</title>
</head>
<body>
<?php include("header.php");
//The above includes the header.php
 ?>
<h1>This is my new website</h1>
<p>Some text</p>
</body>
</html>

We made a simple html page. We added the include function here to include the header file within this php file. So now everything which resided inside header.php will be displayed under this file too. Similarly you may add statement to include the footer or any other important file too.

There is one more function which does the same thing -> require

Listing 2: PHP Require Function

<html>
<head>
	<title>MrBool Tutorial</title>
</head>
<body>
<?php
//We write the require function to include the file “myheaderfile.php” in our file
require("myheaderfile.php");
echo "Hello World!";
?>
</body>
</html>

It is also helpful in adding a webpage on existing page

Difference

Include function executes the whole script even if there is any error in your script. An example would be :

Listing 3: PHP Sample Include example

<html>
<head>
	<title>MrBool Tutorial</title>
</head>
<body>
<?php
include("header.php");
//Here even if header.php is missing then too below echo will work
echo "Hello World!";
?>
</body>
</html>

Here suppose that header.php is missing then also the script will execute to the last.That is why in the output you will get the string “Hello world”

Requirefunction will stop executing the script when it founds any error in the script. A simple example is :

Listing 4: PHP Sample require example

<html>
<head>
	<title>MrBool Tutorial</title>
</head>
<body>
<?php
require("wrongFile.php");
//Here if above file is not found then below echo don’t work
echo "Hello World!";
?>
</body>
</html>

Here in output you will be reported the error and also the script will stop executing so that the string “Hello World” don’t get printed on the screen

Date

This is a very important function. You may use this to display date on your webpage dynamically. Let us discuss its usage with a simple program

Listing 5: PHP Date Function

<html>
<head>
	<title>MrBool Tutorial</title>
</head>
<body>
<?php
echo date("Y/m/d");
?>
</body>
</html>

You will get the current date as an output.

Here the date functions will retrieve the date and display in y/m/d format since in this case it was passed to date function

Loops

Loops can be used to store large amount of values in a single variable. The main advantage of arrays is that it make storing and retrieving values too simple.

There are total 4 kinds of loops in PHP

  1. for loop
  2. while loop
  3. do-while loop
  4. for each loop

For loop

This is most easy to use.The syntax for this loop is easier than others. Let’s understand with simple example

Listing 6: PHP for loop

<html>
<head>
	<title>MrBool Tutorial</title>
</head>
<body>
<?php
//Starting the for loop, initializing variable i and looping until i<=5

	for ($i=1; $i<=5; $i++){
		echo "This is a loop!<br />";
	}

?>
</body>
</html>
  1. This is going to print the statement “This is a loop!” five times.
  2. We defined 3 arguments in for loop. The first one initialize the variable. Second one checks for condition. Third one is for increment.
While Loop

The syntax for while loop is also simple and is explained below:

Listing 7: PHP while loop

<html>
<head>
	<title>MrBool Tutorial</title>
</head>
<body>
<?php

	$x=1;
	//First we initialized then we are checking the condition
	while($x<=10){
		echo "The number is " . $x . " ";
		$x++;
	}

?>
</body>
</html>

We first initialized the variable. In while loop we test the condition and within the loop we do the increments

do while

This loop is different from others because here the statements within loop gets executed at least once and it’s not dependent whether the condition is true or false

Listing 8: PHP do-while loop

<html>
<head>
	<title>MrBool Tutorial</title>
</head>
<body>
<?php
	$x=0;
	//The below loop works no matter what is condition since condition is tested at last

	Do{
		$x++;
		echo "The number is " . $x . " ";
	}

	while ($x<10);

?>
</body>
</html>

For each loop

It can be used to print the array or traverse through an array in a easy manner.

Listing 9: PHP for each syntax

foreach (array1 as $val)
    statement

Listing 10: PHP for each usage

<html>
<head>
	<title>MrBool Tutorial</title>
</head>
<body>
<?php
	$x=array("one","two");
	//Here we traverse through each array value
		foreach ($x as $value) {
  			echo $value . "<br />";
  		}
?>
</body>
</html>

We use the array1 variable and traverse through its value and store each in variable val.

This is all for today’s article. See you next time



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login