Task Five - Repetition Using the FOR Loop - PHP Tutorial

This program introduces repetition and some text/string manipulation functions. It prompts for some input text and then displays the text inside a simple box. The "for" loop is used. There is more information about "for" loops here.

Here is the result of running the program.

Here is the program code.

	
<?php

// ===== INPUT =========================
echo "\n\nPlease enter some text ... ";
$text = trim(fgets(STDIN));
// =====================================

// ===== PROCESS =======================
// Put the text into a box
// =====================================
$length = strlen($text) + 4;

$output = "\n\n";

for ($ii = 0; $ii < $length; $ii++)
{
  $output = $output . "=";
}

$output = $output . "\n= $text =\n";

for ($ii = 0; $ii < $length; $ii++)
{
  $output = $output . "=";
}

$output = $output . "\n\n";
// =====================================

// ===== OUTPUT ========================
echo $output;
// =====================================

?>

 

Code

Explanation

$text = trim(fgets(STDIN));

The fgets(STDIN) function reads text from the standard input (keyboard) until the Enter key is pressed.

The trim() function removes blank spaces from the ends of the text you tyed in. It also removes the Carriage Return CR and Line Feed LF characters generated by pressing the Enter key.

The result from the functions is stored into the $text variable.

$length = strlen($text) + 4;

The function strlen($text) returns the length of the text you typed in.

4 is added to this length and the result stored in the $length variable.

$output = "\n\n";

$output will contain the text to be displayed when the processing has finished. This line stores two "new-lines" into the $output variable.

for ($ii = 0; $ii < $length; $ii++)
{
   $output = $output . "=";
}

The FOR loop is used for repeating a block of code enclosed inside curly brackets. The loop is usually used for counting too.

This example has a variable named $ii used for counting. The Start value is set to 0. The loop will stop repeating when the Boolean labelled Stop becomes false. This happens when $ii is equal to or greater than the value held in $length. $ii++ is a shorthand for "add one to $ii". This controls how $ii changes on each repetition.

This example joins a row of "=" signs to the text to be output.

The "." operator is used for joining text.

$output = $output . "\n= $text =\n";

The new value in $output is set to the

  • original value in $output joined to
  • a new line character joined to
  • an "=" sign and a " " character joined to
  • the value held in the $text valiable joined to
  • another " " character and another "=" sign and another new line character.

for ($ii = 0; $ii < $length; $ii++)
{
  $output = $output . "=";
}

This is the same as the earlier FOR loop.

When code is repeated like this, it is usually a good idea to write your own function to do the task. In this way the code only has to be designed, written and tested once. This gives a useful time saving and the code will be more reliable too.

The next example demonstrates this by re-doing this program with home-grown functions.

$output = $output . "\n\n";

The final value stored in $output is set to the earlier value joined to two new-line characters.

 

Learning Task

Write a program to display text like this using FOR loops to control the layout. (Quite tricky.)


 *      *
  *    *
   *  *
    **
    **
   *  *
  *    *
 *      *