Wednesday, 20 May 2020

Five ways to print a string ten times

Programming can be fun because there's not just one way to do anything. With any programming language, at your disposal, you have a set of tools - loops, variables, arrays, objects and functions - and with them, a programmer can solve problems. Not only can problems be solved, those problems can be solved in a multitude of ways using these same tools.

Today will be one such example. The problem is simple - print your name 10 times. I will walk you through multiple solutions in PHP, each markedly different from the last, from the most obvious to the most intricate.

1. Straight up printing

Yes, this is a solution. I did say "most obvious", but this is clumsy, inelegant, and will quickly fail you if you need to scale up.
echo "TeochewThunder<br />";
echo "TeochewThunder<br />";
echo "TeochewThunder<br />";
echo "TeochewThunder<br />";
echo "TeochewThunder<br />";
echo "TeochewThunder<br />";
echo "TeochewThunder<br />";
echo "TeochewThunder<br />";
echo "TeochewThunder<br />";
echo "TeochewThunder<br />";


2. Looping

Here, I present multiple solutions, but they all use basically same method - a looping construct. Safe enough, but boring. There are even more variations in a language like Ruby, with Until and Do-Until loops, but we won't cover them here.

A For loop
for ($i = 0; $i < 10; $i++)
{
    echo "TeochewThunder<br />";
}


A While loop
$i = 0;

while ($i < 10)
{
   echo "TeochewThunder<br />";
   $i ++;  
}


A Do-While loop
$i = 0;

do
{
   echo "TeochewThunder<br />";
   $i ++;  
} while ($i < 10)


3. A Foreach loop

This one uses an array containing 10 elements, all of them your name. Then you just iterate through the array with a simple Foreach loop. It's great if the elements in the array are different from each other, but pretty silly if they're all the same.

$arr = array("TeochewThunder", "TeochewThunder", "TeochewThunder", "TeochewThunder", "TeochewThunder", "TeochewThunder", "TeochewThunder", "TeochewThunder", "TeochewThunder", "TeochewThunder");

foreach ($arr as $name)
{
    echo $name . "<br />";
}


4. Recursion

Now this is interesting. Here, we write a function that calls itself repeatedly, each time increasing the value of the argument i, and only stops when i is equal to max. Recursion is a useful software engineering trick, but perhaps overkill for this use.

printName("TeochewThunder<br />", 0, 10);

function printName($name, $i, $max)
{
    if ($i < $max)
    {
        echo $name;
        printName($name, $i + 1, 10);
    }
    else
    {
        return;
    }
}


5. The Replace trick

This is a nice one. Again, it's overkill, but so sly! You have a string of 10 asterisks, and simply replace each asterisk with the string you want with a basic str_replace() function call, then print the result. And if you need to print more times? Simply run str_replace() before the final str_replace() call, substituting each asterisk with multiple asterisks.

More for if you want to challenge someone to print the same string multiple times without looping.
$str = "**********";
$str = str_replace("*", "TeochewThunder<br />", $str);

echo $str;


And there you have it...

... five ways to accomplish the same thing. Programming food for thought!

**********,
T___T

No comments:

Post a Comment