PHP

PHP Programming Logical Interview questions and answers

Pinterest LinkedIn Tumblr

Q1) Write a program for a pattern ?

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

Answer

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

Q2) Write a program for this pattern?

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

Answer

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

Q3) Write a program for this pattern?

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

Q4) Write a program for this pattern?

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

Anser

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

Q5) How to find a factorial of a number ?

Factorial of 5 using for loop is shown below.

Example:

<?php  

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

?> 

OR

<?php

   $value = 5;
   for ($i=$value-1; $i>=1; $i--){
     $value = $value*$i;
   } 
   echo $value;
   
?>
Output: 

5*4*3*2*1 = 120

Q6) Write a program to print Factorial of any number

Example:

<?php

 $number = 6;                   
 $fact   = 1;
 for($k=1;$k<=$number;++$k)    
       {
          $fact =  $fact*$k;
       }
 echo "Factorial of $number is ".$fact;
 
?> 

Q7) 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;
   
   }

?>

Q8) Write a program to find whether a number is Armstrong or not

<?php 
 
    $num=370;  
    $total=0;  
    $x=$num;  
    while($x!=0)  
    {  
        $rem=$x%10;  
        $total=$total+$rem*$rem*$rem;  
        $x=$x/10;  
    }  
    if($num==$total)  
    {  
        echo "Yes it is an Armstrong number";  
    }  
    else  
    {  
        echo "No it is not an armstrong number";  
    }  

?>  

Q 9) Write a program to print Reverse of any number

<?php
function reverse_integer($n)
{
    $reverse = 0;
    while ($n > 0)
      {
        $reverse = $reverse * 10;
        $reverse = $reverse + $n % 10;
        $n = (int)($n/10);
      }
     return $reverse;
}   
print_r(reverse_integer(1234)."\n");
print_r(reverse_integer(23)."\n");
?>

Q 10) How to check whether a number is Prime or not.

<?php

function check_prime($num)
{
   if ($num == 1)
   return 0;
   for ($i = 2; $i <= $num/2; $i++)
   {
      if ($num % $i == 0)
      return 0;
   }
   return 1;
}

$num = 2;

$flag_val = check_prime($num);

if ($flag_val == 1)
   echo "It is a prime number";
else
   echo "It is a non-prime number"

?>

Q 11) Write a Program for finding the biggest number in an array without using any array functions.

<?php

$numbers = array(12,23,45,20,5,6,34,17,9,56,999);
$length      = count($numbers);
$max         = $numbers[0];
for($i=1;$i<$length;$i++)
  {
      if($numbers[$i]>$max)
        {
          $max=$numbers[$i];
        }
  }
echo "The biggest number is ".$max;
?>

Q12) Write a Program to swap two numbers in PHP.

<?php

$a=15;
$b=10;
echo "The value of a is ".$a." and b is ".$b;
echo " before swapping.<br />";
$temp=$a;
$a=$b;
$b=$temp;
echo "The value of a is ".$a." and b is ".$b;
echo " After swapping <br />";

?>

Q13) Write a Program for Bubble sorting in PHP

<?php

 $array_value = array(10,21,2,5);
   
 for($i=0; $i<count($array_value); $i++){
    for($j=0;$j<count($array_value)-1;$j++){
     if($array_value[$j] > $array_value[$j+1]){ /* For decreasing order use < */
              $temp = $array_value[$j+1];
              $array_value[$j+1] = $array_value[$j];
              $array_value[$j] = $temp;
          }
        }
   }
  print_r($array_value);

?>

Q14) Write a Program for finding the smallest number in an array

<?php

$numbers=array(12,23,45,20,5,6,34,17,9,56);
$length=count($numbers);
$min=$numbers[0];
for($i=1;$i<$length;$i++)
  {
      if($numbers[$i]<$min)
        {
          $min=$numbers[$i];
        }
  }
echo "The smallest number is ".$min;
?>

Q15) Write a program to print the below format :
1 5 9
2 6 10
3 7 11
4 8 12

<?php
 

for($i=1;$i<=4;$i++)
{
 $i1=$i+4;
 $i2=$i+8;
echo $i." ".$i1." ".$i2;
echo "<br />";
}


?>

Q16) What would be the output of following:
$a = ‘1’;
echo $b = &$a;
echo $c = “2$b”;

OUTPUT:
1   // $b value will be 1
21  // $c value will be 21 as 2 is concatenated to the value of $b.

Q17) Write a program to find second highest number in an array.

<?php 

$array = array('200', '12','69','250','50','500');
 
$maxnum1 = 0;
$maxnum2 = 0;
 
for($i=0; $i< count($array); $i++)
{
    if($array[$i] > $maxnum1)
    {
      $maxnum2 = $maxnum1;
      $maxnum1 = $array[$i];
    }
    else if($array[$i] > $maxnum2)
    {
      $maxnum2 = $array[$i];
    }
}
 
echo "Second Maximum NO is ".$maxnum2;
 
?>

Q18) Count duplicate element of an array in PHP without using in-built function

<?php

$arraychars=array(1,2,2,3,3,5,0,3,19,19,19,200);
$arrlength=count($arraychars);
$arrCount = array();
for($i=0;$i<=$arrlength-1;$i++){
	$key=$arraychars[$i];
	if(@$arrCount[$key]>=1){
		$arrCount[$key]++;
	} else{
		$arrCount[$key]=1;
	}
}

print_r($arrCount);

?>

How to remove duplicate values from an array in PHP

<?php

$array = array(10,20,50,50,20,50);
$array_count = count($array);
$duplicate_array = array();

for($i = 0;$i< $array_count; $i++){
    $duplicate_array[$array[$i]] = $array[$i];
}
echo '<pre>';
print_r($duplicate_array);

Read More: https://www.letsknowit.com/php-programming-questions

Write A Comment