PATTERN

There are some more patterns designed to give you a more clearance about patterns. these are patterns designed inside the rectangular shape like one of the below example is in diamond shape inside rectangular shaped stars.

Patterns are just easy way to describe the flow of code in friendly terms. There are some patterns designed and the output of these designed pattern is in right angled triangle. There are some variations in each pattern which will be very helpfully for u all to learn and understand the flow of program

1.

<?php
/*
Pattern

*
**
***
****
*****

*/
echo " For Loop <br>";
for($i=1;$i<=5;$i++){
	for ($j=1; $j <= $i; $j++) { 
		echo "&nbsp *";
	}
	echo "</br>";
}
echo " While loop <br>";
$i=1;
while ($i <= 5) {
	$j=1;
	while ( $j<= $i) {
		$j++;
		echo "&nbsp *";
	}
	$i++;
	echo " </br>";
}
echo " Do While <br>";
$i=1;
do{
	$j=1;
	do{
		echo "&nbsp *";
		$j++;
	}while($j<=$i);
	$i++;
	echo " <br>";
}while($i<=5);
?>

2.

<?php
/*

Pattern
*****
****
***
**
*

*/
//For loop
echo " For Loop </br>";
for($i=1; $i<=5; $i++){
	for($j=$i; $j<=5; $j++){
		echo "&nbsp *";
	}
	echo "</br>";
}
echo " Do While </br>";
//do While
$i=1;
do{
	$j=$i;
	do{
		echo " &nbsp *";
		$j++;
	}while ($j <= 5);
	$i++;
	echo "</br>";
}while($i <= 5);
//while
echo " While Loop </br>";
$i=1;
while ($i <= 5) {
	$j=$i;
	while ( $j<= 5) {
		$j++;
		echo "&nbsp *";
	}
	$i++;
	echo " </br>";
}
?>

3.

<?php
/*
    *
   **
  ***
 ****
*****
*/
//For Loop 
for ($i=5; $i >=1; $i--) {
	//Row
	//echo "i $i ";
	for($j=4; $j>5-$i; $j--) {
		//Blank
		//echo " j $j";
		echo "&nbsp ";
	}
	for($j=5; $j>=$i;$j--){
		//Star
		//echo " j$j i$j ";
		echo "*";
	}
	echo " <br> ";
}
echo " While Loop <br>";
$i = 5;
while ($i >=1){
	$j=4;
	while($j>5-$i){
		echo "&nbsp ";
		$j--;
	}
	$j=5;
	while ($j>=$i) {
		echo "*";
		$j--;
	}
	echo " </br> ";
	$i--;
}
echo " Do While <br>";
$i=5;
do{
	$j=4;
	//row
	do{
		echo "&nbsp ";
		$j--;
	}while($j>5-$i);
	//star
	$j=5;
	do{
		echo "*";
		$j--;
	}while($j>=$i); 
	$i--;
	echo " </br> ";
}while ($i >= 1);
?>

4.

<?php
/*
*****
 ****
  *** 
   **
    *
*/
//For loop
echo " For Loop <br>";
for ($i=0; $i < 5; $i++) {
	//Row
	//echo "i $i ";
	for($j=0; $j<$i; $j++) {
		//Blank
		//echo " j $j";
		echo "&nbsp ";
	}
	for($j=0; $j<5-$i;$j++){
		//Star
		//echo " j$j i$j ".$j;
		echo "*";
	}
	echo " <br> ";
}
//While Loop
echo " While Loop <br>";
$i=0;
while( $i<5 ){
	$j=0;
	while( $j < $i ){
		echo "&nbsp ";
		$j++;
	}
	$j=0;
	while ( $j<5-$i) {
		echo "*";
		$j++;
	}
	$i++;
	echo " <br> ";
}
//Do while Loop
echo " Do While Loop <br>";
$i=0;
do{
	$j=0;
	do{
		echo "&nbsp ";
		$j++;
	}while($j< $i);
	$j=0;
	do{
		echo "*";
		$j++;
	}while($j<5-$i);

	$i++;
	echo " <br> ";
}while($i<5);
?>

5.

<?php
/*
Pattern
0	X       X
1	 X     X
2	  X   X
3	   X X
4	    X
*/
echo " For loop <br>";
$k=7;
for($i=0;$i<5;$i++){
	for ($j=0; $j < $i ; $j++) { 
		echo "&nbsp ";
	}
	echo "X";
	for ($j=0; $j < $k;$j++) { 
		echo "&nbsp ";
	}
	if($k>0) echo "X";
	$k=$k-2;
	echo "</br>";
}
echo " while Loop<br>";
$k=7;
$i=0;
while($i<5){
	$j=0;
	while ($j< $i) {
		echo "&nbsp ";
		$j++;
	}
	echo "X";
	$j=0;
	while($j<$k){
		echo "&nbsp ";
		$j++;
	}
	if($k>0) echo "X";
	$k -=2;
	$i++;
	echo "<br>";
}
echo " Do while <br>";
$k=7;
$i=0;
do{
	$j=0;
	do{
		echo "&nbsp ";
		$j++;
	}while($j < $i);
	echo "X";
	$j=0;
	do{
		echo "&nbsp ";
		$j++;
	}while($j<$k);
	if($k>0) echo "X";
	$k-=2;
	$i++;
	echo "<br>";
}while($i<5);
?>

6.

<?php
/*
xxxxxxxxxxx
xxxxx xxxxx
xxxx   xxxx
xxx     xxx
xx       xx
x         x
xx       xx
xxx     xxx
xxxx   xxxx
xxxxx xxxxx
xxxxxxxxxxx

01 xxxxxxxxxxx
02 xxxxx1xxxxx
03 xxxx123xxxx
04 xxx12345xxx
05 xx1234567xx
06 x123456789x
07 xx1234567xx
08 xxx12345xxx
09 xxxx123xxxx
10 xxxxx1xxxxx
11 xxxxxxxxxxx
   01234567891
*/
echo "<h3> For Loop </h3>";
for($j=1;$j<=11;$j++){
		echo "x";	
}
$s=1;
$z=5;
echo "<br>";
for($i=1;$i<=5;$i++){
	for($j=$z;$j>0;$j--){
		echo "x";
	}
	for($k=1;$k<=$s;$k++){
		echo "&nbsp ";
	}
	$s+=2;
	for($l=$z;$l>0;$l--){
		echo "x";
	}
	$z--;
	echo "<br>";
}
$m=7;
for($i=1;$i<=4;$i++){
	for($j=0;$j<=$i;$j++){
		echo "x";
	}
	for($k=1;$k<=$m;$k++){
		echo "&nbsp ";
	}
	for($j=0;$j<=$i;$j++){
		echo "x";
	}
	$m-=2;
	echo "<br>";
}
for($j=1;$j<=11;$j++){
		echo "x";	
}
echo "<h3>While Loop </h3>";
$j=1;
while($j<=11){
	echo "x";
	$j++;
}
echo"<br>";
$s=1;
$z=5;
$i=1;
while($i<=5){
	$j=$z;
	while($j>0){
		echo "x";
		$j--;
	}
	$k=1;
	while($k<=$s){
		echo "&nbsp ";
		$k++;
	}
	$s+=2;
	$l=$z;
	while($l>0){
		echo "x";
		$l--;
	}
	$z--;
	$i++;
	echo "<br>";
}
$m=7;
$i=1;
while($i<=4){
	$j=0;
	while($j<=$i){
		echo "x"; 
		$j++;
	}
	$k=1;
	while($k<=$m){
		echo "&nbsp ";
		$k++;
	}
	$j=0;
	while($j<=$i){
		echo "x";
		$j++;
	}
	$m-=2;
	$i++;
	echo "<br>";
}
$j=1;
while($j<=11){
	echo "x";
	$j++;
}
echo "<h3>Do While</h3>";
$j=1;
do{
	echo "x";
	$j++;
}while($j<=11);
$s=1;
$z=5;
echo "</br>";
$i=1;
do{
	$j=$z;
	do{
		echo "x";
		$j--;
	}while($j>0);
	$k=1;
	do{
		echo "&nbsp ";
		$k++;
	}while($k<=$s);
	$s+=2;
	$l=$z;
	do{
		echo "x";
		$l--;
	}while($l>0);
	$z--;
	$i++;
	echo "<br>";
}while($i<=5);
$m=7;
$i=1;
do{
	$j=0;
	do{
		echo "x";
		$j++;
	}while($j<=$i);
	$k=1;
	do{	
		echo"&nbsp ";
		$k++; 
	}while($k<=$m);
	$j=0;	
	do{
		echo "x";
		$j++;
	}while($j<=$i);
	$i++;
	$m-=2;
	echo "<br>";
}while($i<=4);
$i=1;
do{
	echo "x";
	$i++;
}while($i<=11);
?>

7.

<?php
/*
     x
    xxx
   xxxxx
  xxxxxxx
 xxxxxxxxx
  xxxxxxx
   xxxxx
    xxx 
     x
12345x
1234xxx
123xxxxx
12xxxxxxx
1xxxxxxxxx
12xxxxxxx
123xxxxx
1234xxx
12345x
*/
echo "<h3>For Loop</h3>";
$r=5;
$c=0;
for ($i=0; $i < 5; $i++){
	for($j=$r;$j>0;$j--){
		echo"&nbsp ";
	}
	for($k=0;$k<=$c;$k++){
		echo "x";
	}
	echo "<br>";
	$r--;
	$c+=2;
}
$c=7;
for ($i=0; $i < 5; $i++){	
	echo "&nbsp";
	for($k=0;$k <= $i;$k++){
		echo " &nbsp ";
	}
	for($j=$c;$j>0;$j--){
		echo"x";
	}
	echo "<br>";
	$c-=2;
}
echo "<h3>While loop</h3>";
$r=5;
$c=0;
$i=0;
while ($i<5) {
	$j = $r;
	while($j > 0){
		echo "&nbsp ";
		$j--;
	}
	$k=0;
	while($k<=$c){
		echo "x";
		$k++;
	}
	echo "<br>";
	$r--;
	$c+=2;
	$i++;
}
$c=7;
$i=0;
while($i<5){
	echo "&nbsp ";
	$k=0;
	while ($k<= $i) {
		echo "&nbsp ";
		$k++;
	}
	$j=$c;
	while($j>0){
		echo "x";
		$j--;
	}
	echo "<br>";
	$c-=2;
	$i++;
}
?>

8.

<?php
/*
xxxxx
 xxxx
  xxx
   xx
    x
   xx
  xxx
 xxxx
xxxxx

*/
//For
echo "<h3> For Loop </h3>";
for($i=0;$i<=4;$i++){
	for($j=0;$j<=$i;$j++){
		echo "&nbsp ";
	}
	for($k=$i;$k<5;$k++){
		echo "x";
	}
	echo "<br>";
}//one loop
for($i=4;$i>=1;$i--){
	for($j=$i;$j>0;$j--){
		echo "&nbsp ";
	}
	for($k=$i;$k<=5;$k++){
		echo "x";
	}
	echo "<br>";
}//second loop
echo " <h3>While Loop </h3>";
$i=0;
while ($i<=4) {
	$j=0;
	while ($j <= $i) {
		echo "&nbsp ";
		$j++;
	}
	$k=$i;
	while ($k<5) {
		echo "x";
		$k++;
	}
	echo "<br>";
	$i++;
}//one loop
$i=4;
while ($i>=1) {
	$j=$i;
	while ($j > 0) {
		echo "&nbsp ";
		$j--;
	}
	$k=$i;
	while ($k<=5) {
		echo "x";
		$k++;
	}
	echo "<br>";
	$i--;
}//one loop
?>

9.

<?php
/*
Pattern 
 xxxxxxxxxx 9
   xxxxxxxx 8
     xxxxxx 7
       xxxx 6
         xx 5
       xxxx 4
     xxxxxx 3
   xxxxxxxx 2
 xxxxxxxxxx 1
 1234567890	
*/
echo "<h3>For Loop</h3>";
for($i=0;$i<=4;$i++){
	for($j=0;$j<=$i;$j++){
		echo "&nbsp &nbsp ";
	}
	for($n=$i;$n<5;$n++){
		echo "xx";
	}
echo "<br>";
}
for($i=4;$i>=1;$i--){
	for($j=$i;$j>0;$j--){
		echo "&nbsp &nbsp ";
	}
	for($n=$i;$n<=5;$n++){
		echo "xx";		
	}
echo "<br>";
}
echo "<h3>While Loop </h3>";
$i=0;
while($i<=4){
	$j=0;
	while($j<=$i){
		echo "&nbsp &nbsp ";
		$j++;
	}
	$n=$i;
	while($n<5){
		echo "xx";
		$n++;
	}
	echo "<br>";
	$i++;
}
$i=4;
while($i>=1){
	$j=$i;
	while($j>0){
		echo "&nbsp &nbsp ";
		$j--;
	}
	$n=$i;
	while($n<=5){
		echo "xx";
		$n++;
	}
	$i--;
	echo "<br>";
}
?>

10.

<?php
/*
 Pattern
 xxxxx
  xxxx
   xxx
    xx
     x
     xx
     xxx
     xxxx
     xxxxx
*/
echo "<h3>For Loop</h3>";
for($i=0;$i<=4;$i++){
	for($j=0;$j<=$i;$j++){
		echo "&nbsp ";
	}
	for($k=$i;$k<5;$k++){
		echo "x";
	}
	echo "<br>";
}
for($i=0;$i<=4;$i++){
	for($j=4;$j>=0;$j--){
		echo "&nbsp ";
	}
	for($k=$i;$k<5;$k++){
		echo "x";
	}
	echo "<br>";
}
echo "<h3>While Loop</h3>";
$i=0;
while($j<=4){
	$j=0;
	while($j<=$i){
		echo "&nbsp ";
		$j++;
	}
	$k=$i;
	while($k<5){
		echo "x";
		$k++;
	}
	echo"<br>";
	$i++;
}
$i=0;
while($i<=4){
	$j=4;
	while ($j>=0) {
		echo "&nbsp ";
		$j--;
	}
	$k=$i;
	while($k<5){
		echo "x";
		$k++;
	}
	echo "<br>";
	$i++;
}
?>

Comment Below If Pattern Code Want to Ask

(Visited 6,237 times, 1 visits today)
Share with Friends :
Written by:

3 thoughts on “List of Pattern in Php

  • 03/02/2022 at 8:06 pm
    Permalink

    Can you provied pyramid pattern program ?
    I want it.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *