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

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...

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...

GUID for globally unique identifier

How to create GUID in php 1-guid stands for globally unique identifier generally used to create random unique strings in php, create access token in php 2-Mostly use of GUID for generating access token, generate unique id, generating unique string in php. Using this article how to create guide in php you can create a random string for any use to keep unique 3-GUID consists of alphanumeric characters only and is grouped in five groups separated by hyphens as seen in this example: 3F2504E0-4F89-11D3-9A0C-0305E82C3301 Eg:- <?php /** * Generate Globally Unique Identifier (GUID) * E.g. 2EF40F5A-ADE8-5AE3-2491-85CA5CBD6EA7 * * @param boolean $include_braces Set to true if the final guid needs * to be wrapped in curly braces * @return string */ function generateGuid($include_braces = false) { if (function_exists('com_create_guid')) { if ($include_braces === true) { return com_create_guid(); } else { return substr(com_cr...