PHP programs for printing pyramid patterns
Last Updated :
03 May, 2023
This article is aimed at giving a PHP implementation for pattern printing.
Simple Pyramid Pattern
PHP
<?php
function pypart( $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j <= $i ; $j ++ )
{
echo "* " ;
}
echo "\n" ;
}
}
$n = 5;
pypart( $n );
?>
|
Output :
*
* *
* * *
* * * *
* * * * *
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.
After 180 degree rotation
PHP
<?php
function pypart2( $n )
{
for ( $i = 1; $i <= $n ; $i ++) {
for ( $j = 1; $j <= $n ; $j ++) {
if ( $j <=( $n - $i )){
echo " " . " " ;
} else {
echo "* " ;
}
}
echo PHP_EOL;
}
}
$n = 5;
pypart2( $n );
?>
|
Output :
*
* *
* * *
* * * *
* * * * *
Time Complexity: O(N^2) where N is the number of rows.
Space Complexity: O(1) because only a fixed amount of memory is used.
Printing Triangle
PHP
<?php
function triangle( $n )
{
$k = 2 * $n - 2;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $k ; $j ++)
echo " " ;
$k = $k - 1;
for ( $j = 0; $j <= $i ; $j ++ )
{
echo "* " ;
}
echo "\n" ;
}
}
$n = 5;
triangle( $n );
?>
|
Output :
*
* *
* * *
* * * *
* * * * *
Time complexity: O(N^2) where N is given number of rows
Auxiliary Space: O(1)
Star Triangle Pattern 2
PHP
<?php
function triangle_pattern( $len ){
$string = "*" ;
$pyramid_str = "" ;
$mid_point = ceil ( $len /2);
for ( $i =1; $i <= $mid_point ; $i ++){
for ( $j = 1; $j <= $i ; ++ $j ) {
$pyramid_str .= $string . " " ;
}
$pyramid_str .= "\r\n" ;
}
for ( $i = $mid_point ; $i >=1; $i --){
for ( $j = 1; $j < $i ; ++ $j ) {
$pyramid_str .= $string . " " ;
}
$pyramid_str .= "\r\n" ;
}
return $pyramid_str ;
}
echo triangle_pattern(9);
?>
|
Output
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Time Complexity: O(n^2), where n is the input parameter.
Space Complexity: O(n), where n is the input parameter.
Number Pattern
PHP
<?php
function numpat( $n )
{
$num = 1;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j <= $i ; $j ++ )
{
echo $num . " " ;
}
$num = $num + 1;
echo "\n" ;
}
}
$n = 5;
numpat( $n );
?>
|
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Numbers without re assigning
PHP
<?php
function numpat( $n )
{
$num = 1;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j <= $i ; $j ++ )
{
echo $num . " " ;
$num = $num + 1;
}
echo "\n" ;
}
}
$n = 5;
numpat( $n );
?>
|
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Character Pattern
PHP
<?php
function alphapat( $n )
{
$num = 65;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j <= $i ; $j ++ )
{
$ch = chr ( $num );
echo $ch . " " ;
}
$num = $num + 1;
echo "\n" ;
}
}
$n = 5;
alphapat( $n );
?>
|
Output:
A
B B
C C C
D D D D
E E E E E
Continuous Character pattern
PHP
<?php
function contalpha( $n )
{
$num = 65;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j <= $i ; $j ++ )
{
$ch = chr ( $num );
echo $ch . " " ;
$num = $num + 1;
}
echo "\n" ;
}
}
$n = 5;
contalpha( $n );
?>
|
Output:
A
B C
D E F
G H I J
K L M N O
Time complexity: O(n^2) where N is given input no of rows
Auxiliary Space: O(1)
Related Article: Programs for printing pyramid patterns in C++ Programs for printing pyramid patterns in Java Programs for printing pyramid patterns in Python
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...