Loops and conditional statements in PERL in comparison with PHP

Published:

Add / read comments

Work with loops like if - else, do - while, for, foreach in PERL is very flexible. On the other hand, this flexibility can result into the complexity. In PHP is situation with loops more simplified. Conditional statements are from my perspective very the same. Let's find out what are the differences.

The best, is to start with if - else conditional statement. Open your favorite code editor, write down the following code and save file as "pl" extension.

#!C:\xampp\perl\bin\perl.exe
 
# header for showing via browser  
print "Content-Type: text/html; charset=utf-8\n\n";
 
print "Work with loops";
 
# declaration and initialization of variable of INTEGER type
$a = 6;
$b = 7;
 
$sBreak = '<br>';
 
# if A equals to B
if ( $a == $b ) {
  print $sBreak . 'Equal!';
}
else {
  print $sBreak . 'Not equal!';
}

Now, if you open the PERL file via web browser, you should see something like this:

Work with loops
Not equal!

Sometimes it is necessary to have more than two conditional executions. The syntax of an if...elsif...else statement in Perl programming language is:

# declaration and initialization of variable of INTEGER type
$a = 6;
$b = 7;
 
$sBreak = '<br>';
 
# if A equals to B
if ( $a == $b ) {
  print $sBreak . 'Equal!';
}
elsif ( $a < $b ) {
  print $sBreak . 'A is less than B';
}
else {
  print $sBreak . 'Not equal!';
}

As you can see from the code above, conditional statements in PERL are piece of cake :-) In the PHP, conditional statements are very the same, except the key word elsif is written as else if. Just see next code example:

<?php
 
# declaration and initialization of variable of INTEGER type
$a = 6;
$b = 7;
 
# variable of STRING type
$sBreak = '<br>';
 
# if A equals to B
if ( $a == $b ) {
  print $sBreak . 'Equal!';
}
else if ( $a < $b ) {
  print $sBreak . 'A is less than B';
}
else {
  print $sBreak . 'Not equal!';
}
 
/**
output: A is less than B  
*/
 

A FOR loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times. During the execution, you can for example check the result of conditional statement. Just see following code:

# definition of INDEXED ARRAY from 1 to 10
@count10 = (1 .. 10);
 
# printing
print "<br /><br />Prints number starting from 1 to 10:<br />";
print "@count10";
 
# printing with loop FOR (increasing)
print '<br /><br />Increasing FOR loop:<br>';    
 
for ($index = 0; $index < 10; $index++) {    
 	print '<br />Array index:' . $index. ' is number: ' . @count10[$index];
 
   if ( $index == 4 ) {
      print '<br />In the middle of the loop!';
   }  
}

Now, if you open the PERL file via web browser, you should see something like this:

Prints number starting from 1 to 10:
1 2 3 4 5 6 7 8 9 10

Increasing FOR loop:

Array index:0 is number: 1
Array index:1 is number: 2
Array index:2 is number: 3
Array index:3 is number: 4
Array index:4 is number: 5
In the middle of the loop!
Array index:5 is number: 6
Array index:6 is number: 7
Array index:7 is number: 8
Array index:8 is number: 9
Array index:9 is number: 10

In the PHP, loop FOR structure is the same. There are differences with PERL in how to set the indexed array. See next PHP code example:

# definition of INDEXED ARRAY from 1 to 10
$aNumbers = array(1, 2, 3, 4, 5);
 
# printing
echo "<br /><br />Prints number starting from 1 to 5:<br />";
print_r( $aNumbers );
 
# printing with loop FOR (increasing)
print '<br /><br />Increasing FOR loop:<br>';    
 
for ($index = 0; $index < 6; $index++) {    
 	echo '<br />Array index:' . $index. ' is number: ' . $aNumbers[$index];
 
   if ( $index == 2 ) {
      echo '<br />In the middle of the loop!';
   }  
}

Result of execution of the PHP code should be something like this:

Prints number starting from 1 to 5:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) 

Increasing FOR loop:

Array index:0 is number: 1
Array index:1 is number: 2
Array index:2 is number: 3
In the middle of the loop!
Array index:3 is number: 4
Array index:4 is number: 5

An UNTIL loop statement in Perl programming language repeatedly executes a target statement as long as a given condition is false. In PHP there is not language construction UNTIL

$a = 1;
 
# until the condition is false -> execute the block of code
until( $a > 5 ) {
   print '<br />Value of a: '. $a;
   # increment value
   $a = $a + 1;
}

Open the PERL script via web browser and you should see the following:

Value of a: 1
Value of a: 2
Value of a: 3
Value of a: 4
Value of a: 5

Other loop statement in Perl programming language WHILE repeatedly executes a target statement as long as a given condition is true. In the following code are two construction versions, longer and shorter. In PHP there is not possible to write longer variant of the language construction of the WHILE loop.

# definition of INDEXED ARRAY from 1 to 10
@count10 = (1 .. 10);
 
# printing with loop WHILE (decreasing)
print '<br /><br />Decreasing WHILE loop:';
{
  # MY declares the listed variables to be local (lexically) to the enclosing block
  my $iCount = 9;
  # conditional statement
  while ($iCount >= 5 )
  { 
    print '<br />Array index: ' . $iCount. ' is number: ' . @count10[$iCount]; 
  }
  continue {
    # decrementation
    $iCount--;
  }
}
 
# printing with shortened loop WHILE (decreasing)
print '<br /><br />Shortened decreasing WHILE loop:';
 
$iCount = 9;
 
# conditional statement
while (  $iCount >= 5) {  
 	print '<br />Array index: ' . $iCount. ' is number: ' . @count10[$iCount];
  # decrementation
  $iCount--;   
}

Open the PERL script via web browser and you should see the following:

Decreasing WHILE loop:
Array index: 9 is number: 10
Array index: 8 is number: 9
Array index: 7 is number: 8
Array index: 6 is number: 7
Array index: 5 is number: 6

Shortened decreasing WHILE loop:
Array index: 9 is number: 10
Array index: 8 is number: 9
Array index: 7 is number: 8
Array index: 6 is number: 7
Array index: 5 is number: 6

And this is how looks the shorter version of loop WHILE in PHP:

# definition of INDEXED ARRAY from 1 to 10
$count10 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
 
# printing with loop WHILE (decreasing)
print '<br /><br />Decreasing WHILE loop:';
{
  $iCount = 9;
  while ($iCount >= 5)
  { 
    print '<br />Array index:' . $iCount. ' is number: ' . $count10[$iCount];
    # decrementation
    $iCount--;
  }
}

Result of execution of the PHP code should be something like this:

Decreasing WHILE loop:
Array index:9 is number: 10
Array index:8 is number: 9
Array index:7 is number: 8
Array index:6 is number: 7
Array index:5 is number: 6

Other loop statement in Perl programming language is DO - WHILE. Unlike FOR and WHILE loops, which test the loop condition at the top of the loop, the DO - WHILE loop checks its condition at the bottom of the loop. It means, that DO - WHILE loop is guaranteed to execute at least one time. Just see following code:

# printing with DO - WHILE loop (decreasing)
print 'DO - WHILE decreasing loop:<br /><br />';
 
$iCount = 0;
 
# printing with loop WHILE (decreasing)
do{
   print "Value of iCount: $iCount<br />";
   $iCount++;
}
# check conditional statement
while( $iCount < 10 );

Open the PERL script via web browser and you should see the following:

DO - WHILE decreasing loop:

Value of iCount: 0
Value of iCount: 1
Value of iCount: 2
Value of iCount: 3
Value of iCount: 4
Value of iCount: 5
Value of iCount: 6
Value of iCount: 7
Value of iCount: 8
Value of iCount: 9

In the PHP, DO - WHILE loop structure is the same. See next PHP code example:

# printing with DO - WHILE loop (decreasing)
print 'DO - WHILE decreasing loop:<br /><br />';
 
$iCount = 0;
 
# printing with loop WHILE (decreasing)
do{
   print "Value of iCount: $iCount<br />";
   $iCount++;
}
# check conditional statement
while( $iCount < 6 );
 
/**
output:
 
DO - WHILE decreasing loop:
 
Value of iCount: 0
Value of iCount: 1
Value of iCount: 2
Value of iCount: 3
Value of iCount: 4
Value of iCount: 5
 
*/

The FOREACH loop structure iterates over indexed or associative array. Each iteration sets the control on the value of the current element. The FOREACH loop structures in PERL have a little bit more complicated notation than in PHP. Let's have a look on the following PERL code:

# definition of INDEXED ARRAY from 1 to 10
@aCount10 = (1 .. 10);
 
# printing FOREACH loop ( over INDEXED ARRAY )
print '<br />FOREACH loop (INDEXED):<br />';
 
foreach $value (@aCount10)
{
  print '<br />Value of @aCount10: ' . $value;
}
 
# or other way to print value
foreach ( @aCount10 ) 
{
  print '<br />Value of @aCount10: ' .$_;  
}
 
 
# definition of ASSOCIATIVE ARRAY ( variable name must starts with % )
%aOsobneUdaje = ("name","Peter","surname","Ulicny","city","Zilina","street","Limbova","postal_code","82302");
 
# printing FOREACH loop ( over ASSOCIATIVE ARRAY )
print '<br />FOREACH loop (ASSOCIATIVE)<br />';
 
foreach $index (keys %aOsobneUdaje)
{
   print '<br />Index (key): ' . $index . ' --> value: ' .  $aOsobneUdaje{$index};
}

Open the PERL script via web browser and you should see the following:

FOREACH loop (INDEXED):

Value of @aCount10: 1
Value of @aCount10: 2
Value of @aCount10: 3
Value of @aCount10: 4
Value of @aCount10: 5
Value of @aCount10: 6
Value of @aCount10: 7
Value of @aCount10: 8
Value of @aCount10: 9
Value of @aCount10: 10

FOREACH loop (ASSOCIATIVE)

Index (key): city --> value: Zilina
Index (key): postal_code --> value: 82302
Index (key): street --> value: Limbova
Index (key): name --> value: Peter
Index (key): surname --> value: Ulicny

In the PHP, the FOREACH loop structures are more simple to write. See next PHP code example:

# definition of INDEXED ARRAY from 1 to 10
$aCount10 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
 
# printing FOREACH loop ( over INDEXED ARRAY )
print '<br />FOREACH loop (INDEXED):<br />';
 
foreach ($aCount10 as $value)
{
  print '<br />Value of $aCount10: ' . $value;
}
 
/**
output:
 
FOREACH loop (INDEXED):
 
Value of $aCount10: 1
Value of $aCount10: 2
...
...
Value of $aCount10: 9
Value of $aCount10: 10 
*/
 
 
# definition of ASSOCIATIVE ARRAY ( variable name must starts with % )
$aOsobneUdaje = array("name" => "Peter", "surname" => "Ulicny", "city" => "Zilina", "street" => "Limbova", "postal_code" => "82302");
 
# printing FOREACH loop ( over ASSOCIATIVE ARRAY )
print '<br />FOREACH loop (ASSOCIATIVE)<br />';
 
foreach ($aOsobneUdaje as  $index => $value)
{
   print '<br />Index (key): ' . $index . ' --> value: ' .  $value;
}
 
/**
output:
 
FOREACH loop (ASSOCIATIVE)
 
Index (key): name --> value: Peter
Index (key): surname --> value: Ulicny
Index (key): city --> value: Zilina
Index (key): street --> value: Limbova
Index (key): postal_code --> value: 82302
*/

From these short examples, you could see, that there are slight differences between PERL and PHP when working with loop structures and conditional statements.

Published:

Add / read comments

FIND ME

Share, follow or connect with me.