PHP Interview Questions and Answers

PHP Programming/ Logical Interview questions and answers

Q 1. Write a program for this 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 ?
*
*  *
*  *  *
*  *  *  *
*  *  *  *  *
*  *  *  *  *  *
*  *  *  *  *
*  *  *  *
*  *  *
*  *
*
Answres-

<?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 ?
*****
*****
*****
*****
*****
Answer-

<?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 ?
Answer- 

<?php
if(isset($_POST['submit']))
{
 $number = $_POST['num'];     /*number to get factorial */
 $fact   = 1;
 for($k=1;$k<=$number;++$k)
 {
 $fact =  $fact*$k;
 }
 echo "Factorial of $number is ".$fact;
}
 ?>
 <!DOCTYPE html>
 <html>
 <head>
 <title>Factorial of any number</title>
 </head>
 <body>
 <form name="factorial" action="" method="post">
 Number :<input type="text" name="num" value="" required=""><br>
 <input type="submit" value="Submit" name="submit">
 </form>
 </body>
 </html>

Q 6- How to find whether a number prime or not ?
Answer-

<?php
if(isset($_POST['submit']))
{
$check=0;
$num=$_POST['num'];
for($i=2;$i<=($num/2);$i++)
{
if($num%$i==0)
{
$check++;
if($check==1)
{
 break ;
}
}
}
if($check==0)
{
echo "It is a Prime Number";
}
else
{
echo "It is not a Prime Number";
}
}
?>
<!DOCTYPE html>
 <html>
 <head>
 <title>Check whether a number prime or not</title>
 </head>
 <body>
 <form name="primenumber" action="" method="post">
 Number :<input type="text" name="num" value="" required><br>
 <input type="submit" value="Submit" name="submit">
 </form>
 </body>
 </html>

Q 7- How to find whether a number armstrong or not ?
Answer- An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.
For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

<?php
if(isset($_POST['submit']))
{
$number = $_POST['num'];      // 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";
}
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Whether a number Armstrong or not</title>
</head>
<body>
 <form name="armstrong" action="" method="post">
 Number :<input type="text" name="num" value="" required=""><br>
 <input type="submit" value="Submit" name="submit">
 </form>
</body>
</html>

Q 8- How to find  whether a year leap year or not ?
Answer-

<?php
if(isset($_POST['submit']))
{
$year=$_POST['year'];
if($year%4==0)
{
echo "It is a leap year";
}
else
 {
   echo "It is not a leap year";
 }
}
 ?>
 <!DOCTYPE html>
 <html>
 <head>
 <title>Leap year</title>
 </head>
 <body>
 <form name="leapyear" action="" method="post">
 Year :<input type="text" name="year" value="" required=""><br /><br />
 <input type="submit" value="Submit" name="submit">
 </form>
 </body>
 </html>

Q9 -Write a program for this pattern(number pyramid) ?
1
22
333
4444
55555
Answer-

<html>
<head>
<title>Number Pyramid</title>
</head>
<body>
<?php
$r;
$c;
for($r=1;$r<=5;$r++)
{
for($c=1;$c<=$r;$c++)
{
print("$r");
}
print "<br>";
}
?>
</body>
</html>

Q 10- How to print a number reverse ?
Answer –

<?php
if(isset($_POST['submit']))
{
         $rev=0;
         $num=$_POST['num'];
          while($num>=1)
                {
                  $re=$num%10;
                  $rev=$rev*10+$re;
                  $num=$num/10;
                 }
                   echo "Reverse number of is " .$rev;
}
?>
<!DOCTYPE html>
 <html>
 <head>
 <title>Reverse of a number</title>
 </head>
 <body>
 <form name="reversenumber" action="" method="post">
 Number :<input type="text" name="num" value="" required=""><br>
 <input type="submit" value="Submit" name="submit">
 </form>
 </body>
 </html>

Q11 – Swap two values without third variable ?
Answer –

<?php
if(isset($_POST['submit']))
{
$value1=$_POST['num1'];
$value2=$_POST['num2'];
$value1=$value1+$value2;
$value2=$value1-$value2;
$value1=$value1-$value2;
        echo "Value of first variable after swapping" .$value1."<br />";
        echo "Value of second variable after swapping"  .$value2;
}
?>
<!DOCTYPE html>
 <html>
 <head>
 <title>Swap two values without third Variable</title>
 </head>
 <body>
 <form name="factorial" action="" method="post">
 Number 1 :<input type="text" name="num1" value="" required=""><br /><br />
 Number 2 :<input type="text" name="num2" value="" required=""><br />
 <input type="submit" value="Submit" name="submit">
 </form>
 </body>
 </html>

Recommended Tutorials for you


Related posts

Basic PHP Programs

Anuj Kumar

Common Sql Interview Questions and Answers

Anuj Kumar

CodeIgniter Interview Questions and Answers

Anuj Kumar

PHP Interview Questions and Answers for Fresher

Anuj Kumar

MySql Interview questions and answers

Anuj Kumar

WordPress Interview Questions And Answers

Anuj Kumar

10 comments

santhiya November 10, 2018 at 5:58 am

super

Reply
enesh November 10, 2018 at 5:58 am

Nice

Reply
Abhi Rathor November 10, 2018 at 5:58 am

HELLO SIR,
I m Website designer and i ask one question . i want every user click on button add on +1 in Previous value and update in sql

Reply
Anuj Kumar November 10, 2018 at 6:18 am

Visit this tutorial–Hit counter in PHP

Reply
Taniya Patel November 10, 2018 at 5:58 am

A great example of PHP concepts.
Thanks a lot.
Keep it up.

Reply
santhiya October 29, 2018 at 10:33 am

super

Reply
Taniya Patel August 2, 2018 at 7:46 pm

A great example of PHP concepts.
Thanks a lot.
Keep it up.

Reply
enesh July 10, 2018 at 1:10 pm

Nice

Reply
Abhi Rathor October 27, 2017 at 7:09 am

HELLO SIR,
I m Website designer and i ask one question . i want every user click on button add on +1 in Previous value and update in sql

Reply
Anuj Kumar October 27, 2017 at 7:35 am

Visit this tutorial–Hit counter in PHP

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.