আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML, CSS PHP,MySQL,JavaScript, AJAX, JQUERY, Bootstrap and Live Project কোর্সের ৮৭ তম ব্যাচ এবং ২৬ ডিসেম্বর-২০১৭ তারিখ থেকে শুরু হচ্ছে চার মাস ব্যাপী Zend PHP-7 Certified PHP Engineering (Advance PHP) কোর্সের ৩৫ তম ব্যাচ। প্রত্যেকটি কোর্স এর ফী নির্ধারণ করা হয়েছে ৩০,০০০/= আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722 81 75 91

PHP Control Structure part-3: PHP for, while, do-while loops

What is loop in PHP?

php loops

The loops in PHP or any programming language is a specific task to be repeated or execute until a particular condition is met.

What kinds of loops in PHP?

PHP has 4 types of loop, those are

  • For
  • While
  • do-while
  • foreach

What is the for loop on PHP?

PHP for loop

For loop in PHP or any programming language, a specific task is to be repeated or executed until a particular condition is met. The value of variable initialization, condition and increment of the variable in for loop is done on the same line.

What is the for loop flowchart on PHP?

forloop

What is the for loop syntax in PHP?

For loop syntax in PHP, can be written in 2 ways

syntax 1:

for (initialization; condition; increment/decrement){
   code to be executed;
}

syntax 2:

for (initialization; condition; increment/decrement):
    statement
    ...
endfor;

Note the following examples, for all loops numbers 1 to 10 will appear.

Example 1:

<?php
for ($i = 1; $i <= 10; $i++) {
    echo $i;
}
?>

We can also write for loop in this way.

Example 2:

<?php
for ($i = 1; ; $i++) {
    if ($i > 10) {
        break;
    }
    echo $i;
}
?>

You can type for loop in this way also.

Example 3:

<?php
$i = 1;
for (; ; ) {
    if ($i > 10) {
        break;
    }
    echo $i;
    $i++;
}
?>

Note the following example, here we will print the even numbers between 1 to 20 through for loop.

Example 4:

<?php
for($i=2;$i<=20;$i+=2){
echo $i,"<br>";
}
?>

In the same way we can print odd numbers between 1 to 20 through for loop.

Example 5:

<?php
for($i=1;$i<=20;$i+=2){
echo $i,"<br>";
}
?>

The following example fibonacci series was make in PHP use for loop. Note that the fibonacci series is the sum of the first two and keep on the three number.

Example 6:

<?php
$f1=0;
$f2=1;
$f=1;
$f3=1;
echo $f1,"<br>",$f2;
for($i=0;$i<=6;$i++){
     
    $f3=$f1+$f2;
    $f1=$f2;
    $f2=$f3;
    echo $f3;
}

In the example below, we can print from 20 to 0, which means reverse numbers in for loop.

Example 7:

<?php
for ($i = 20; $i >0; $i--):
    echo $i,"<br>";
endfor;
?>

What is the while loop in PHP?

php-while-loop

While loop is the same as for loop in PHP or any programming language. A particular task is to be repeated or executed until a specific condition is met. The difference is that the value of variable initialization, condition and increment of variable in while loop is done in different lines.

What is the While loop flowchart in PHP?

PHP while loop flowchart

What is the loop syntax in PHP?

While loop syntax can be written in 2 ways in PHP.

syntax 1:

initialization
while (condition){
    statement
    increment
}

syntax 2:

initialization
while (condition):
    statement
    increment
endwhile;

Note the following examples, all the while loop show the number 1 to 10.

Example 1:

<?php
$i=1;
while ($i <= 10) {
    echo $i;
 $i++;
}
?>

Note the following example, here you can mark that while loop will print even number between 1 to 20.

Example 2:

<?php
$i=2;
while ($i <= 20) {
    echo $i;
 $i+=2;
}
?>

In the same way we can print odd numbers between 1 to 20 through the while loop.

Example 3:

<?php
$i=1;
while ($i <= 20) {
    echo $i;
 $i+=2;
}
?>

The following example fibonacci series was made by while loop in PHP. Note that the fibonacci series is the sum of the first two number keep on third number.

Example 4:

<?php
$f1=0;
$f2=1;
$f=1;
$f3=1;
echo $f1,"<br>",$f2;
$i=1;
while($i<=6){
     
    $f3=$f1+$f2;
    $f1=$f2;
    $f2=$f3;
    echo $f3;
$i++;
}

In the example below, we can print from 20 to 1, ie, the reverse number with the while loop.

Example 5:

<?php
$i = 20; 
while ($i >0):
    echo $i,"<br>";
 $i--;
endwhile;
?>

What is the do-while loop in PHP?

do-while loop in php or any programming language is similar to the while loop, ie, a particular task is to repeat or execute until a particular condition is met. The value of variable initialization, condition and increment of variable in while loop is done in different lines. The difference is that when the condition of the while loop is not true, the code can not execute the block. And do not show any results. But if do-while this condition is not true, it can execute the code block 1 times, and show results.

What is the do-while loop flowchart in PHP?

dowhile

What is the do while loop syntax in PHP?

do {
  // This code is run at least once
} while ( condition )
  
// This code is run after the loop finishes

Note the following example

<?php
 
$theNumber = rand ( 1, 10 );
 
do {
$myGuess = rand ( 1, 10 );
echo "I'm guessing: $myGuess</br />";
} while ( $myGuess != $theNumber );
 
echo "I got it! It's $myGuess!<br />";
 
?>

Result

I'm guessing: 3
I'm guessing: 5
I'm guessing: 7
I'm guessing: 2
I got it! It's 2!

What is foreach in PHP?

The foreach is typically used to work as a loop for arrays and each element of the object.

What is the foreach loop flowchart in PHP?

PHP foreach loop flowchart

What is foreach syntax in PHP?

Foreach loop syntax can be written in 3 ways in PHP

syntax 1:

foreach ($array_or_object_handler as $value){
    statement
}

syntax 2:

foreach ($array_or_object_handler as $key=>$value){
    statement
}

syntax 3:

foreach ($array_or_object_handler as $value):
    statement
endforeach;

syntax 4:

foreach ($array_or_object_handler as $key=>$value):
    statement
endforeach;

Note the following examples, how you can iterate elements of array by foreach.

Example 1:

$numbers=["one"=>1,"two"=>2,"three"=3,"four">4,"five"=>5];
foreach($numbers as $val){
echo $val,"<br>";
}

Result

1
2
3
4
5

Similarly, we can also print the keys of array variable. See the following example:

Example 2:

$numbers=["one"=>1,"two"=>2,"three"=3,"four">4,"five"=>5];
foreach($numbers as $key=>$val){
echo $key,"<br>";
}

Result

one
two
three
four
five

Can it possible to change the array element by foreach in PHP?

Yes it is possible to change the array elements using the reference See the example below:

<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
// $arr is now array(2, 4, 6, 8)
}
print_r($arr); 
unset($value); // break the reference with the last element
?>

Result

2
4
6
8

Explanation: Because of & reference operator use before $value veritable which is carry array veritable value and change by foreach condition.

Can you print all the properties values of class by foreach in PHP?

Yes, but you can print public property only. Notice the following examples:

<?php
class A{
    public $name="Plabon Mozumder";
    public $email="[email protected]";
    public $mobile="01788339922";
    public $price;
    private $user;
    public function setPrice($amount){
        $this->price=$amount;
    }
     
}
$obj=new A;
$obj->setPrice(100);
 
foreach($obj as $property=>$val){
    print $property."=>".$val."\n";
}

Result

name=>Plabon Mozumder
email=>[email protected]
mobile=>01788339922
price=>100

Is there any other way to print key and value of array without foreach in PHP?

Yes, in addition foreach, you can print the key and value of the array by while loop in PHP.

<?php
$arr = array("one"=>1, "two"=>2, "three"=>3);
reset($arr);
while (list($key, $value) = each($arr)) {
    echo "$key=> $value\n";
}

Result

one=> 1
two=> 2
three=> 3

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.

Leave a Reply

Your email address will not be published. Required fields are marked *