The Ultimate Pattern Programs in PHP

by  
Share:
The Ultimate Pattern Programs in PHP

Students preparing to learn the PHP language and want to clear their concepts relating to loop construct, decision-making statements & control statements through coding various number and star pattern programs in PHP should read this book.

I actually hate programming, but I love solving problems. – Rasmus Lerdorf (Creator of PHP)

Book Overview

I am pleased to let you know that this book contains 100 programs with source codes readily available in different text files arranged as per their order.

The book is divided into five sections arranged in a very easy-to-learn manner. After reviewing and practicing everything inside the book, I make sure you will master this topic.

If you want a book that covers every aspect of pattern programs in PHP, including loops, break, and continue statements.

Do not lose your time looking for anything else. Purchase this book at the cost of less than a cup of coffee.

Buy now

Warmup Exercise

This section has five programs to help you get acquainted with the working of for loops, grid system, and understanding star pattern programs in PHP. Take a look at one of the programs in this section.

PHP program to print table from 1 to 10

PHP program to print table from 1 to 10

In this PHP program, we need to print the table from 1 to 10. But before we get to code, let us first understand the program logic.

  • Take ten rows and ten columns.
  • Start an outer loop (row) that iterate ten times.
  • Inside the outer loop start an inner loop (column), that iterates 10 times.
  • Multiply the current row value with the current column value in the inner loop.
<?php
$row_length = 10;
$column_length = 10;

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        echo ($row * $column ) . "&nbsp;";
    }
    echo '<br>';
}
?>

Geometrical Set: pattern programs in PHP

This section has 30 programs. I have prepared them in easy-to-learn order, i.e. the simple pattern first and the complex pattern at last. You can print various geometrical patterns, like pyramid patterns, half pyramid patterns, diamond patterns, hollow diamond patterns, and so on.

Program to print inverted mirror right-angle triangle with star pattern in PHP

Program to print inverted mirror right-angle triangle with star pattern in PHP

As you can see, I have mentioned both the program output and the program logic with a side-by-side comparison to help understand the program.

  • Take ten rows and ten columns.
  • The blank spaces in the grid (marked as yellow) will come in play here.
  • As the rows increase, column value (star) decreases. Row one has ten star & Row two has nine stars and so on.
  • Start an outer loop (row) that will iterate ten times.
  • Under the outer loop, start an inner loop (for spaces) that iterate till current row value.
  • After the inner loop for spaces, start loop again (for star) that iterate till total row length minus current row value. Print star inside this loop.
<?php
$row_length = 10;
for ($row = 0; $row < $row_length; $row++) {
    for ($spaces = 0; $spaces < $row; $spaces++) {
        // 3 spaces, 2 spaces for character used below and one additional space
        echo "&nbsp;&nbsp;&nbsp;";
    }
    for ($column = 0; $column < $row_length - $row; $column++) {
        // using one extra space to pretty output
        echo "*&nbsp;";
    }
    echo '<br>';
}
?>

Numbers Set with pattern in PHP

This section consists of 10 programs. You will be printing the numbers from 0 to 9 with the star pattern.

Print number four with star pattern program in PHP

Print number four with star pattern program in PHP

With the side by side-by-side comparison as well as color coding, I have marked the coding logic.

  • Take eleven rows and ten columns, for the 6th row will be the exact middle.
  • On the first column (marked with red), print star from first till middle row, use
    this condition $column == 1 && $row <= $row_middle
  • At the mid row (marked with blue), print star in all columns, use this condition ($row == $row_middle)
  • Finally, on the last column (marked with red) print star in all row, use this condition ($column == $column_length)
  • If the condition does not satisfy, print spaces.
<?php
$row_length = 11;
$column_length = 10;
$row_middle = ceil($row_length / 2);
for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($column == 1 && $row <= $row_middle) {
            echo "*&nbsp;";
        } else if ($row == $row_middle) {
            echo "*&nbsp;";
        } else if ($column == $column_length) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

Alphabet Set: star pattern in PHP

There are 26 programs in this section. You will learn to print letters from A to Z with star patterns.

PHP program to print letter H with star pattern

PHP program to print letter H with star pattern

With the side by side-by-side comparison as well as color coding, understand the coding logic.

  • Take eleven rows and ten columns, for the 6th row will be the exact middle.
  • Print star at the first and last column of each row (marked with green), print star in all columns, use this condition ($column == 1 || $column == $column_length)
  • On the middle row (marked with red), print star in all column, use this condition $row == $row_middle
  • If the condition does not satisfy, print spaces.
<?php
$row_length = 11;
$column_length = 10;
$row_middle = ceil($row_length / 2);

for ($row = 1; $row <= $row_length; $row++) {
    for ($column = 1; $column <= $column_length; $column++) {
        if ($row == $row_middle ) {
            echo "*&nbsp;";
        } else if ($column == 1 || $column == $column_length ) {
            echo "*&nbsp;";
        } else {
            echo "&nbsp;&nbsp;&nbsp;";
        }
    }
    echo "<br>";
}
?>

Mixed Set: patterns of numbers and stars

There are 29 programs in this section. It has various programs like pyramid pattern of numbers, reverse pyramid patterns, inverted pyramid patterns, hollow diamond pattern program in PHP.

PHP program to print pyramid pattern of numbers

PHP program to print pyramid pattern of numbers

With the side-by-side comparison and color coding, you can understand there are two triangles in this pyramid pattern.

  • Break the program in two section.
  • The left section (marked with green), has spaces and value. Here, number of rows is equals to number of columns. After printing the spaces use value_print to print the output and increment it by 1.
  • Adjust the value_print by decrementing it by 2.
  • The right section (marked with red), number of columns is one less than number of rows.
  • Inside the inner loop, print value_print variable and decrement it by 1.
<?php
$row_length = 5; $value_print = 1;
for ($row = 1; $row <= $row_length; $row++) {
    for ($spaces = 1; $spaces <= $row_length - $row; $spaces++) {
        echo "&nbsp;";
    }
    $value_print = $row;
    for ($column1 = 1; $column1 <= $row; $column1++) {
        echo $value_print;
        $value_print++;
    }
    $value_print -= 2;
    for ($column2 = 1; $column2 < $row; $column2++) {
        echo $value_print;
        $value_print--;
    }
    echo '<br>';
}
?>

Program to print number pattern programs in PHP

Program to print number pattern programs in PHP

Look at the side-by-side comparison and understand what we are going to code.

  • It is a right-angle triangle shape.
  • For the first column of each row (marked with green) print current row value.
  • For the second section (marked with red) inside the inner loop, print the current column value
<?php
$row_length = 5;
for ($row = 1; $row <= $row_length; $row++) {
    echo $row . "&nbsp;";
    for ($column = 1; $column < $row; $column++) {
        echo $column . "&nbsp;";
    }
    echo '<br>';
}
?>

Claim this exclusive offer now

Don’t waste your time looking for anything else. Purchase this book at the cost of less than a cup of coffee.

Buy now

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.

Share:
Hasan

Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.