PHP Interview questions and answers (Programming/ Logical)
Q 1. Write a program for this pattern ?
* * * * *
* * * *
* * *
* *
*
Answer –
1 2 3 4 5 6 7 8 |
<?php for($i=0;$i<=5;$i++){ for($j=5-$i;$j>=1;$j--){ echo "* "; } echo "<br>"; } ?> |
Q2 -Write a program for this pattern ?
*
* *
* * *
* * * *
* * * * *
Answer-
1 2 3 4 5 6 7 8 |
<?php for($i=0;$i<=5;$i++){ for($j=1;$j<=$i;$j++){ echo "* "; } echo "<br>"; } ?> |
Q3 -Write a program for this pattern ?
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Answres-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php for($i=0;$i<=6;$i++){ for($k=6;$k>=$i;$k--){ echo " "; } for($j=1;$j<=$i;$j++){ echo "* "; } echo "<br>"; } for($i=5;$i>=1;$i--){ for($k=6;$k>=$i;$k--){ echo " "; } for($j=1;$j<=$i;$j++){ echo "* "; } echo "<br>"; } ?> |
Q4 -Write a program for this pattern ?
*****
*****
*****
*****
*****
Answer-
1 2 3 4 5 6 7 8 9 10 |
<?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-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?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-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?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-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?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-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<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 –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?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 –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?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> |