Friday 28 July 2017

Program to print below format..

Program to print below format.
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1





<?php
   $rows=5;  // number of row
   for($i=$rows;$i>=1;--$i)
     {
         for($j=1;$j<=$i;++$j)
         {
            echo $j;
         }
     echo "<br />";
     }
 ?>
 

Program to print the below format.

Program to print the below format
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *



<?php


   $rows=5;  // number of row enter.
   for($i=$rows;$i>=1;--$i)
     {
         for($space=0;$space<$rows-$i;++$space)
            echo "  ";
         for($j=$i;$j<=2*$i-1;++$j)
           echo "* ";
         for($j=0;$j<$i-1;++$j)
             echo "* ";
         echo "<br />";
     }



?>

Program to find year is LEAP year or not.


<?php
    $year=2017;
    if($year%4==0)
          {
            $leap="It is a leap year";
          }
      else
         {
           $leap="It is not a leap year";
        }
 ?>

Write a program to print Fibonacci series

Write a program to print Fibonacci series

<?php
$fibo=array(0,1);
$num=4;
     for($i=2;$i<=$num-1;$i++)
{
       $fibo[$i]=$fibo[$i-1]+$fibo[$i-2];
      }
?>

To check whether a number is Prime or not.

<?php

   $check=0;
   $num=$_POST['number'];
   for($i=2;$i<=($num/2);$i++)
     { 
       if($num%$i==0)
         {
          $check++;
          if($check==1)
          {
             break;
          }
         }
     }

?>

Write a program to print Reverse in php

<?php
         $rev=0;
         $num=4;
         
          while($num>=1)
                {
                  $re=$num%10;
                  $rev=$rev*10+$re;
                  $num=$num/10;
                 }

?>

Write a program to find whether a number is Armstrong or not in php.


<?php
if(isset($_POST['number']) && $_POST['number']!='') 
    {  
      
        $number = $_POST[ 'number' ];      // get the number entered by user
     
        $temp = $number;
        $sum  = 0;
     
        while($temp != 0 )
        {
            $remainder   = $temp % 10; //find reminder
            $sum         = $sum + ( $remainder * $remainder * $remainder );
            $temp        = $temp / 10;

       }
        if( $number == $sum )
        {
            echo "$number is an Armstrong Number";
        }else
        {
            echo "$number is not an Armstrong Number";
        }
    }
?>

Write a program in PHP to print Fibonacci series . 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

<?PHP 
   $first = 0;
   $second = 1;
   echo $first.'&nbsp;,';
   echo $second.'&nbsp;,';
  
  for($limit=0;$limit<10;$limit++)
   {
     $third = $first+$second;
     echo $third.'&nbsp;,';;
     $first = $second;
     $second = $third;

   
   }
?>

Write a program to print Factorial number in php.

<?php

$num = 4;
$factorial = 1;
for ($x=$num; $x>=1; $x--)
{
  $factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";

?>


Output
Factorial of 4 is 24

Friday 21 July 2017

Display latest comments in wordpress template.

Display latest comments for each post in WordPress

<?php 
$commentArr = array_reverse(get_approved_comments($wp_query->post->ID));
$count = 1; // number of comments
if ($commentArr) { ?>
    <h3><?php commentsNum('0 comment', '1 comment', '% comments'); ?></h3>
    <ul>
    <?php foreach ($commentArr as $comment) {
        if ($count++ <= 2) { ?>
        <li><?php comment_author_link(); ?>: <?php comment_excerpt(); ?></li>
        <?php }
    } ?>
    </ul>
<?php } else {
    echo '<p>No Comments..</p>';
} ?>