Skip to main content

Using php print pattern

This is not new concept, you need to follow same concept like C programming to print number pattern in php.

every time on interview ask question print pattern. this is important note please keep it.
Output :
Code :

<?php for($a=1; $a<=5; $a++)
{
 for($b=$a; $b>=1; $b--)
{
echo "$b";
}
echo "<br>";
} ?>

Now simple print one column then only one for loop use to print pattern 
This is not new concept, you need to follow same concept like C programming to print star pattern or triangle of star.
Output :
Code :
<?php for ($row = 1; $row <= 5; $row++)
{
 echo "* </br>";
} ?>

print * in square size as following manner and please follow following code :

Code :
<?php for ($row = 1; $row <= 5; $row++)
{
 for ($col = 1; $col <= 5; $col++)
  {
   echo '* ';
  }
   echo "</br>";
}
 ?>

PHP start pattern of trangle as output and code here :
Output :
Code :
<?php 
for($i=0;$i<=5;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo "* ";
}
echo "<br>";
}
 ?>

as above example print pattern as mirror as follow :

Output :










Code :
<?php 

for($i=0;$i<=5;$i++)
{
for($j=5-$i;$j>=1;$j--)
{
echo "* ";
}
echo "<br>";
}
 ?>

New pattern of printing patter as following :
Output :
Code :
<?php 
for($a=5; $a>=1; $a--)
{
if($a%2 != 0)
{
for($b=5; $b>=$a; $b--)
{
echo "* ";
}
echo "<br>";
}
}
for($a=2; $a<=5; $a++)
{
 if($a%2 != 0)
{
 for($b=5; $b>=$a; $b--)
{
echo "* ";
}
echo "<br>";
}
}
 ?>

Following pattern of trangle print as refer following steps :
Output :

Code :
<?php 
for($i=0;$i<=9;$i++){

  for ($d=10-$i; $d > 0; $d--)  {

    echo    "&nbsp;&nbsp;";
  }
  for($j=1;$j<=$i;$j++){

    echo "&nbsp;".$i."&nbsp;";
 }

 echo "<br>";

} 
 ?>
please follow the following code to print pattern : 
Output :

Code :
<?php 
for($i=0;$i<=7;$i++){
for($j=1;$j<=$i;$j++){
echo $i;
}
echo "<br>";
} 
 ?>
print one one at row and column as following :
Output :


Code :
<?php 
for($i=0;$i<=7;$i++){
for($j=1;$j<=$i;$j++){
echo "1";
}
echo "<br>";
}
 ?>

HOW TO MAKE A NUMERIC POLYGON SHOWS ONLY ONE AND A PLUS SIGN WITH NUMBERS.

Output :


Code :
<?php 
$m=1; $n=9; $z=9;
for($i=1; $i<=$n; $i++){
 for($j=$i; $j<=$n-1; $j++){
   echo "&nbsp;&nbsp;&nbsp;";
   }
 for($k=1; $k<=$m; $k++){
  if($k==$m  || $k==1 || $i==$n){
     echo $k ."&nbsp";
     }
   else{
    echo "&nbsp;&nbsp;&nbsp;";
    }
  }
 for($c=$m; $c>1; $c--){
   if($c-1 == 1 || $m==$n){
     echo $c-1 ."&nbsp";
     }
   else{
     echo "&nbsp;&nbsp;&nbsp;";
     }
   }
   echo "<br>";
   $m++;
}
for($i=1; $i<=$n; $i++){
 for($j=1; $j<=$i; $j++){
   echo "&nbsp;&nbsp;&nbsp;";
   }
 for($k=1; $k<$z; $k++){
   if($k==1 || $k==$z-1){
     echo $k ."&nbsp";
     }
   else{
    echo "&nbsp;&nbsp;&nbsp;";
    }
  }
 for($c=$z-2; $c>=1; $c--){
   if($c==1){
     echo $c ."&nbsp";
     }
   else{
     echo "&nbsp;&nbsp;&nbsp;";
     }
  }
  echo "<br>";
  $z--;
}
 ?>

Make start polygon using php :
Output :

Code :
<?php 
 $m=1; $n=5;
for($i=1; $i<=5; $i++) {
    for($j=$i; $j<=4; $j++) {
        echo "&nbsp;&nbsp;";
    }
    for($k=1; $k<=$m; $k++)  {
        echo "*";
    }
    for($c=$m; $c>1; $c--) {
        echo "*";
    }
    echo "<br>";
    $m++;
}
 
for($i=1; $i<=5; $i++) {
    for($j=$i; $j>=1; $j--) {
        echo "&nbsp;&nbsp;";
    }
    for($k=$n; $k>1; $k--) {
        echo "*";
    }
    for($c=$n-1; $c>1; $c--) {
        echo "*";
    }
    echo "<br>";
    $n--;
} 
 ?>

I hope that my code and pattern you like and enjoy my code, if you like my code please share and comment,
thanks


Comments

Popular posts from this blog

Profile Share Fixing the Thumbnail Image, Title and Description for Shared Links

Profile Share Fixing the Thumbnail Image, Title and Description for Shared Links user want to share any information then use following code  and read step by step Profile Share Fixing the Thumbnail Image, Title and Description for Shared Links if you want share profile on following social link : 1-Facebook 2-twitter.com 3-LinkedIn 4-google +, Code link : https://drive.google.com/open?id=1IzTZZh_0euDqFSHL_vPRiQePlNTw3h-q Demo link : http://freeteachnology.hol.es/socialshare/ To modify a page's thumbnail image, description, and additional metadata for these services, you can provide meta tags in the HTML code of the page.Implementing Open Graph Meta Tags You can implement meta tags in a number of ways. In content management systems might  be allow you to modify a page's meta tags , then use following code in meta section of your project code, <link href="bootstrap.min.css" rel="stylesheet"> <link href="bootstrap-tour.m...

using PDO database connection add,update,delete,edit operation

PDO advantage : 1-Object Oriented 2-Bind parameters in statements (security) 3-Allows for prepared statements and rollback functionality (consistency) 4-Throws catcheable exceptions for better error handling (quality) 5-Exception mode; no need to check error state after each API call. It's best to tell PDO how you'd like the data to be fetched. You have the following options: 1-PDO::FETCH_ASSOC: returns an array indexed by column name. 2-PDO::FETCH_BOTH: (default):returns an array indexed by both column name and number. 3-PDO::FETCH_BOUND:Assigns the values of your columns to the variables set with the ->bindColumn() method. 4-PDO::FETCH_CLASS: Assigns the values of your columns to properties of the named class. It will create the properties if matching properties do not exist. 5-PDO::FETCH_INTO:Updates an existing instance of the named class. 6-PDO::FETCH_LAZY: Combines. 7-PDO::FETCH_BOTH/PDO:FETCH_OBJ, creating the object variable names as t...

Ajax Asynchronous and synchronus using jquery

What is jQuery 1-jQuery is a fast and concise JavaScript Library created by John Resig in 2006 with a nice motto - 2-Write less, do more. 3-jQuery simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. Following feature of jQuery : 1-DOM manipulation − The jQuery made it easy to select DOM elements, traverse them and modifying their content by using cross-browser open source selector engine called Sizzle. 2-Event Handling : The jQuery offers an elegant way to capture a wide variety of events,such as click,over event, 3-Ajax Support : jQuery support ajax technology, 4- Lightweight : he jQuery is very lightweight library - about 19KB in size ( Minified and gzipped ) . 5-Animations − The jQuery comes with plenty of built-in animation effects. 6-Cross Br...