PHP programs for printing pyramid patterns
This article is aimed at giving a PHP implementation for pattern printing.
Simple Pyramid Pattern
Time Complexity: The time complexity of this algorithm is O(N^2) where N is the number of rows.
Space Complexity: The space complexity of this algorithm is O(1) because only a fixed amount of memory is used.
<?php
for ($i = 0; $i <= 5; $i++) {
for ($j = 1; $j <= $i; $j++) {
echo "* ";
}
echo "<br>";
}
?> Pattern 1
<?php
for ($i = 0; $i <= 5; $i++) {
for ($j = 5 - $i; $j >= 1; $j--) {
echo "* ";
}
echo "<br>";
}
?>
Pattern 2
<?php
for($i=0;$i<=5;$i++){
for($k=5;$k>=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
for($i=4;$i>=1;$i--){
for($k=5;$k>=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
?>
Pattern 3
<?php
for ($i=1; $i<=5; $i++)
{
for ($j=1; $j<=5; $j++)
{
echo '* ';
}
echo "</br>";
}
?>
Pattern 4
<?php
// printing pattern of numbers
// printing pattern
function numpat($n)
{
// initializing starting number
$num = 1;
// outer loop to handle
// number of rows
// n in this case
for ($i = 0; $i < $n; $i++)
{
// inner loop to handle
// number of columns
// values changing acc.
// to outer loop
for ($j = 0; $j <= $i; $j++ )
{
// printing number
echo $num." ";
}
// incrementing number
// at each column
$num = $num + 1;
// ending line after
// each row
echo "<br>";
}
}
// Driver Code
$n = 5;
numpat($n);
?>
Pattern 5
<?php
// printing pattern of numbers
// Numbers without re assigning
function numpat($n)
{
// initialising starting
// number
$num = 1;
// outer loop to handle
// number of rows
// n in this case
for ($i = 0; $i < $n; $i++)
{
// inner loop to handle
// number of columns
// values changing acc.
// to outer loop
for ($j = 0; $j <= $i; $j++ )
{
// printing number
echo $num." ";
// incrementing number
// at each column
$num = $num + 1;
}
// ending line after
// each row
echo "<br>";
}
}
// Driver Code
$n = 5;
numpat($n);
?>
Pattern 6
<?php
// pattern of alphabets
// printing pattern
function alphapat($n) {
// initializing value
// corresponding to 'A'
// ASCII value
$num = 65;
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j <= $i; $j++) {
// explicitly converting
// to char
$ch = chr($num);
// printing char value
echo $ch . " ";
}
// incrementing number
$num = $num + 1;
// each row
echo "<br>";
}
}
// Driver Code
$n = 5;
alphapat($n);
?>
Pattern 7
<?php
// PHP code to demonstrate printing
// pattern of alphabets
// Function to demonstrate
// printing pattern
function contalpha($n)
{
// initializing value
// corresponding to 'A'
// ASCII value
$num = 65;
// outer loop to handle
// number of rows
// n in this case
for ($i = 0; $i < $n; $i++)
{
// inner loop to handle
// number of columns
// values changing acc.
// to outer loop
for ($j = 0; $j <= $i; $j++ )
{
// explicitly converting
// to char
$ch = chr($num);
// printing char value
echo $ch." ";
// incrementing number
// at each column
$num = $num + 1;
}
// ending line after each row
echo "<br>";
}
}
// Driver Code
$n = 5;
contalpha($n);
?>
