PHP FOR LOOP – PHP ARRAY

PHP & MySQL Course:PHP FOR LOOP –  PHP ARRAY

In the previous lecture# 4 Part A & B, we learnt about the operators and theirs uses and also conditional

statements. We learnt these all topics in detailed and also defined them in programs.

Lecture 5:PHP FOR LOOP –  PHP ARRAY

In lecture 5, we are going to learn few more important topics. And these topics are commonly

use in every object oriented programming languages. These topics made easy to do programming

in PHP as well as others too.

  1. Loops in PHP
  2. Array in PHP
  3. Comments in PHP

Loops in PHP:

These are also the same types of loops and their use is also same, like other programming languages.

As for loop, while loop, do while loop, and foreach loop. These loops are being used for different

purposes and they solved many problems. Sometimes we need to write a single name or word and

any sentence many times may be 10 to 50 times. So we use loops to write it,

and loop writes it in seconds.

Syntax:

  1. for(initial condition; final condition; increment/decrement) {statement}
  2. while(condition){statement}
  3. do{statement}(condition);
  4. foreach(condition){statement}

PHP For loop:

For loop, we know how much times the iteration will runs and we also know when it

will be break or stop the iteration of the loop. See the listing 1 for details.

Listing 1: Using PHP For Loop


<?

//using for loop in PHP

$var1 = 10;

$name = "TrustInGeeks";

for($var2 = 1; $var2<$var1; $var2++)

{

echo $name." ";

}

?>

While loop:

While loop, in this loop we use a condition. If the condition is true then it

will stop the running iterations. See the listing 2 for details.

Listing 2: Using While Loop in PHP


<?

//using while loop in PHP

$var1 = 1;

$name = "TrustInGeeks";

while ($var1 <10)

{

echo $name." ";

$var1++;

}

?>

Do while loop:

Do while loop, in this loop we also use a condition. But the difference is that it

will run once at any cost. It means that we can use this loop where we need it.

See the listing 3 for details.

Listing 3: Using Do While Loop in PHP


<?

//using do while loop in PHP

$var1 = 1;

$name = "TrustInGeeks";

do

{

$var1++;

echo $name." ";

}while($var1 < 10);

?>

Foreach loop:

Foreach loop, we use this loop commonly in arrays. And we know working

with arrays is complicated, so we need to be very care full. See the listing 4 for details.

Listing 4: Using Foreach Loop in PHP


<?

//using foreach loop in PHP

$name = array("a" => "C++","b" => "Java","c" => "PHP & MySQL","d"
=> "JavaScript",);

foreach($name as $ArrayData)

{

echo $ArrayData;  //now we will print the data of array using $ArrayData

}

?>

Using   PHP Array:

As we know that the array is used to define list of similar data in a single variable.

And there three types of Arrays are being used in PHP programming for web pages design.

  1. Numeric Arrays in PHP
  2. Associative Arrays in PHP
  3. Multidimensional Arrays in PHP

Numeric Arrays in PHP:

The numeric arrays are used to store numbers, strings, letters, and any type of other object.

But we use a keyword array() to declare the arrays. Its name is numeric arrays due to its

counting of items in the array. As it starts from the 0 to the last item. It mean that its

first item will be at 0[zero]. Check the listing 5 for details

Listing 5: Using Numeric Arrays in PHP


<?

//using numeric arrays in PHP

$number = array(1,2,3,4,5,6,7,8,9,10);

for($i = 0; $i <= 9; $i++)

{

echo "<br>".$number[$i];

}

//different way to define numeric arrays in PHP using item number

$name[0] = "C++";

$name[1] = "Java";

$name[2] = "PHP";

$name[3] = "MySQL";

$name[4] = "JavaScript";

//display them using their item numbers

echo "<br> $name[0]";

echo "<br> $name[1]";

echo "<br> $name[2]";

echo "<br> $name[3]";

echo "<br> $name[4]";

//displaying them with loops

foreach ($name as $i)

{

echo "$i<br>";

}

Associative Arrays in PHP:

The associative arrays are used by their index. Because these are called associative arrays

due to the connection of data with index. And it is not necessary to use any number for the index.

We also use any string or letter for the index. And then we assign it an item or data. We never say it,

it is being start from the 0. It just starts from where it is displaying the data. We will also use the

array() method to declare the associative arrays in PHP . Check the listing 6 for details about

associative arrays.

Listing 6: Using Associative Arrays in PHP:


<?

//using associative arrays in PHP

$grade = array ("C++" => "A", "Java" => "A+", "PHP" => "A");

foreach($grade as $index => $value)

{

echo "$value is on $index<br>";

}

/*there is an other simple way to declare the

associative arrasy in PHP as we declared the numeric arrays in PHP*/

$grade["C++"] = "A";

$grade["Java"] = "A+";

$grade["PHP"] = "A";

$grade["C#"] = "A";

//we can also print them without any using loop

echo $grade["C++"]." ".$grade["Java"]." ".$grade["PHP"]." ".$grade["C#"];

?>

Multidimensional Arrays in PHP:

From its name you may understand, we will work in rows and columns now. We can set that

data in rows and columns. And we call one row as a record. So now we will use the multidimensional

arrays of PHP in our programs. It is difficult job to work with multidimensional arrays and also it is

not being used commonly in websites development. We use commonly the numeric and associative

arrays during the development of websites. Read the listing 7 for details about the multidimensional arrays.

Listing 7: Use of Multidimensional Arrays in PHP:


<?

//using multidimensional arrays in PHP

$records = array(

"John" => array("age" => 40,"city" => "New York", "work" => "unemployee"),

"Toney" => array("age" => 20,"city" => "Bejing", "work" => "Computer Operator"),

"Tina" => array("age" => 30,"city" => "Washington", "work" => "Model")

);

//using with foreach loop

foreach($records as $i => $value)

{

echo "<h5>Data for $i "."</h5>";

foreach($value as $j)

{

echo $j."<br>";

}

}

?>

Comments in PHP:

Using of comments and its purpose is same in PHP. We use comments to remind us when

we want to check the source code.And it is very good habit to use comments. There are two

types of comments single line comments and multi-line comments. For single line comments

we use double slash as “//” and for multi-line comments we use “/**/”. And the

comments never execute. These are dummy lines in any source code of any   programming

languages. As we used comments in all listing which you can understand the source code easily.

Summary:

In this lecture 5, we discussed the three topics. And these three topics are very important to understand them

for future. Because these topics are being used commonly in website development. We discussed the loops

and their types. Then we used them in programs to execute and checked their results. After that we used the

most important topic array and then we also use them in programs using PHP. And then we discuss the

simplest topic comments as you are familiar with it.

admin
 

student of computer Science , having interest and experience in C++,Java,Php,Html,Css,JavaScript, wordpress and android development