Write a php program to print the butterfly pattern of the numbers by using the for loop
Write a php program to print the butterfly pattern of the numbers by using the for loop. There are you will learn how to print the butterfly pattern of the numbers by using the for loop.
Take an example to print this pattern through a php program:
<?php
// Write a PHP program to print the butterfly pattern
// of the numbers by using the for loop
// It's the size of butterfly pattern
$h = 7;
echo "-----This the butterfly pattern-----\n\n";
// This will print the butterfly pattern
butterflyPattern($h);
// It's the function to
// print the butterfly pattern
function butterflyPattern($h) {
for($r = 1; $r <= $h - 1; $r++) {
echo "\t";
for($d = 1; $d <= $r; $d++)
echo $d . " ";
for($s = 1; $s <= 2 * ($h - $r); $s++)
echo " ";
for($d = $r; $d >= 1; $d--)
echo $d . " ";
echo "\n";
}
for($r = $h; $r >= 1; $r--) {
echo "\t";
for($d = 1; $d <= $r; $d++)
echo $d . " ";
for($s = 1; $s <= 2 * ($h - $r); $s++)
echo " ";
for($d = $r; $d >= 1; $d--)
echo $d . " ";
echo "\n";
}
}
?>
Output:
-----This the butterfly pattern-----
1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
1 2 3 4 5 5 4 3 2 1
1 2 3 4 5 6 6 5 4 3 2 1
1 2 3 4 5 6 7 7 6 5 4 3 2 1
1 2 3 4 5 6 6 5 4 3 2 1
1 2 3 4 5 5 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 3 2 1
1 2 2 1
1 1
Tags:
# pattern program in php using
# toughest pattern programs in php
# alphabet pattern programs in php