Reffered from : http://www.letsknowit.com/php-programming-questions
PHP Programming Questions
Answer
<?php
------------------------------------------------------------------------------------------------------------------
Answer
<?PHP
$first = 0;
$second = 1;
echo $first.' ,';
echo $second.' ,';
for($limit=0;$limit<10;$limit++)
{
$third = $first+$second;
echo $third.' ,';;
$first = $second;
$second = $third;
}
?>
Write a program to find whether a number is Armstrong or not
Answer
// If the sum of cubes of individual digits of a number is equal to the number itslef then it is called Armstrong Number.
<?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";
}
}
?>
------------------------------------------------------------------------------------------------------------------------
Answer
<?php
if(isset($_POST['rev2']))
{
$rev=0;
$num=$_POST['rev'];
while($num>=1)
{
$re=$num%10;
$rev=$rev*10+$re;
$num=$num/10;
}
}
?>
<html>
<head>
<title>Reverse</title>
</head>
<body>
<table>
<form name="frm" method="post">
<tr><td>Number</td><td><input type="text" name="rev"></td></tr>
<tr><td>Reverse is:</td><td><input type="text" value="<?php if(isset($_POST['rev2'])){echo $rev;} ?>" name="rev1"></td></tr>
<tr><td> </td><td><input type="Submit" value="Reverse" name="rev2"></td></tr>
</form>
</table>
</body>
</html>
To check whether a number is Prime or not.
Answer
<?php
if(isset($_POST['submit']) && $_POST['submit']=='Check' )
{
$check=0;
$num=$_POST['number'];
for($i=2;$i<=($num/2);$i++)
{
if($num%$i==0)
{
$check++;
if($check==1)
{
break;
}
}
}
}
?>
<html>
<head>
<title>Prime Number</title>
</head>
<body>
<table>
<form name="frm" method="post" action="">
<tr><td>Number:</td><td><input type="text" name="number" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="Check" /></td>
<td>
<center><span>
<?php if(isset($_POST['sub']))
{if($
check
==0)
{echo "It is a Prime Number";
}
else
{
echo "It is not a Prime Number";}
}
?>
</span>
</center>
</td>
</tr>
</form>
</table>
</body>
</html>
Write a program to print Fibonacci series
Answer
<?php
if(isset($_POST['sub']))
{ $fibo=array(0,1);
$num=$_POST['nm1'];
for($i=2;$i<=$num-1;$i++)
{
$fibo[$i]=$fibo[$i-1]+$fibo[$i-2];
}
}
?>
<html>
<head>
<title>Fibonacci Series</title>
</head>
<body>
<table>
<form name="frm" method="post" action="">
<tr><td>Number of Terms:</td><td><input type="text" name="nm1" /></td></tr>
<tr><td></td><td><input type="submit" name="sub" /></td>
<td><center><span>
<?php if(isset($_POST['sub'])){
for($i=0;$i<=$num-1;$i++){echo $fibo[$i].",";}
}
?>
</span></center>
</td></tr>
</form>
</table>
</body>
</html>-
----------------------------------------------------------------------------
Write a program to find HCF of two numbers
Answer
<?php
if(isset($_POST['submit']))
{
$num1=$_POST['number1'];
$num2=$_POST['number2'];
function hcf($i1,$i2)
{
if($i2!=0)
{
return hcf($i2,$i1%$i2);
}
else
{
return $i1;
}
}
$hcfofnumber=hcf($num1,$num2);
}
?>
<html>
<head>
<title>Hcf</title>
</head>
<body>
<table>
<form name="frm" method="post" action="">
<tr><td>Number1:</td><td><input type="text" name="number1" /></td></tr>
<tr><td>Number2:</td><td><input type="text" name="number2" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="submit" /></td>
<td><center><span>
<?php if(isset($_POST['submit']))
{
echo "HCF is ".$hcfofnumber; }
?>
</span></center></td></tr>
</form>
</table>
</body>
</html>
Program to find whether a year is LEAP year or not
Answer
<?php
if(isset($_POST['submit']))
{
$year=$_POST['year'];
if($year%4==0)
{
$leap="It is a leap year";
}
else
{
$leap="It is not a leap year";
}
}
?>
<html>
<head>
<title>Leap Year</title>
</head>
<body>
<table>
<form name="frm" method="post" action="">
<tr><td>Enter the year:</td><td><input type="text" name="year" /></td></tr>
<tr><td></td> <td><input type="submit" name="submit" value="submit" /></td>
<td><center><span>
<?php
if(isset($_POST['submit'])){
echo $leap; }
?>
</span></center></td></tr>
</form>
</table>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------
Program to print the below format
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Answer
<?php
if(isset($_POST['sub']))
{
$rows=$_POST['row'];
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 />";
}
}
?>
<table>
<form method="post" name="frm" action="">
<tr> <td>Enter Number of rows:</td> <td><input type="text" name="row" /></td> </tr>
<tr><td></td> <td><input type="submit" name="sub" /></td> </tr>
</form>
</table>
-----------------------------------------------------------------------------------------------------------------------
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
Answer
<?php
if(isset($_POST['sub']))
{
$rows=$_POST['row'];
for($i=$rows;$i>=1;--$i)
{
for($j=1;$j<=$i;++$j)
{
echo $j;
}
echo "<br />";
}
}
?>
<table>
<form method="post" name="frm" action="">
<tr> <td>Enter Number of rows:</td> <td><input type="text" name="row" /></td> </tr>
<tr><td></td> <td><input type="submit" name="sub" /></td> </tr>
</form>
</table>
Program to print below format
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
Answer
<?php
if(isset($_POST['sub']))
{
$rows=$_POST['row'];
for($i=1;$i<=$rows;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo "*";
}
echo "<br />";
}
}
?>
<table>
<form method="post" name="frm" action="">
<tr> <td>Enter Number of rows:</td> <td><input type="text" name="row" /></td> </tr>
<tr><td></td> <td><input type="submit" name="sub" /></td> </tr>
</form>
</table>
--------------------------------------------------------------------------------
Write a program to print below format
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
Answer
<?php
if(isset($_POST['sub']))
{
$row=$_POST['row'];
for($i=1;$i<=$row;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo $j;
}
echo "<br />";
}
}
?>
<table>
<form method="post" name="frm" action="">
<tr> <td>Enter Number of rows:</td> <td><input type="text" name="row" /></td> </tr>
<tr><td></td> <td><input type="submit" name="sub" /></td> </tr>
</form>
</table>
Write a program to find factor of any number
Answer
<?php
if(isset($_POST['sub']))
{ $j=0;
$factor=array();
$num=$_POST['nm1'];
for($i=1;$i<=$num;$i++)
{
if($num%$i==0)
{
$j++;
$factor[$j]=$i;
}
}
}
?>
<table>
<form name="frm" method="post" action="">
<tr> <td>Number:</td> <td><input type="text" name="nm1" /></td> </tr>
<tr><td></td><td><input type="submit" name="sub" /></td>
<td><center><span>
<?php
if(isset($_POST['sub']))
{
echo "Factors are :";for($i=1;$i<=count($factor);$i++)
{ echo $factor[$i].",";
}
}
?>
</span></center></td></tr>
</form>
</table>
Write a program to find table of a number
Answer
if(isset($_POST['sub']))
{
$num=$_POST['num'];
echo "<h1><center>Table of " .$num."</center></h1>";
for($i=1;$i<=10;$i++)
{
echo $num*$i;
echo "<br />";
}
}
?>
<table>
<form name="frm" method="post">
<tr><td>Number</td><td><input type="text" name="num"></td></tr>
<tr><td> </td><td><input type="Submit" value="Submit" name="sub"></td></tr>
</form>
</table>
----------------------------------------------------------------------------------------------------------------------
Write a Program for finding the biggest number in an array without using any array functions.
Answer
<?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;
?>
Write a Program to swap two numbers in PHP.
Answer
<?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 />";
?>
--------------------------------------------------------------------------------------------------------
Hide Answer
<?php
$numbers=array(12,23,45,20,5,6,34,17,9,56);
$n=count($numbers);
for ($c = 0 ; $c < ( $n - 1 ); $c++)
{
for ($d = 0 ; $d < ($n - $c - 1); $d++)
{
if ($numbers[$d] > $numbers[$d+1]) /* For decreasing order use < */
{
$swap = $numbers[$d];
$numbers[$d] = $numbers[$d+1];
$numbers[$d+1] = $swap;
}
}
}
echo "Sorted list in ascending order <br />";
for ( $c = 0 ; $c < $n ; $c++ )
echo $numbers[$c]." ";
?>
-------------------------------------------------------------------------------------------------------------------------
Write a Program for finding the smallest number in an array
Answer
<?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;
?>
------------------------------------------------------------------------------------------------------------------------
Write a program to print the below format :
1 5 9
2 6 10
3 7 11
4 8 12
Answer
<?php
for($i=1;$i<=4;$i++)
{
$i1=$i+4;
$i2=$i+8;
echo $i." ".$i1." ".$i2;
echo "<br />";
}
?>
-------------------------------------------------------------------------------------------------------------------
*****
* *
* *
* *
*****
Answer
<?php
for($i = 1; $i<=5; $i++){
for($j = 1; $j<=5; $j++){
if($i == 1 || $i == 5){
echo "*";
}
else if($j == 1 || $j == 5){
echo "*";
}
else {
echo " ";
}
}
echo "<br/>";
}
?>
-------------------------------------------------------------------------------------------------------------------
How to print this Pattern:
*0
***00
******000
**********0000
***************00000
Answer
<?php
$k = 0;
for ($i=1; $i<=5; $i++)
{
$k += $i;
for ($j=1; $j<=$k; $j++)
{
echo "*";
}
for ($z=0; $z<$i; $z++)
{
echo "0";
}
echo "</br>";
}
?>
----------------------------------------------------------------------------------------------------------------------
How to write a Floyd's Triangle?
1
23
456
78910
1112131415
Answer
<?php
$a = 1;
for($i = 1; $i<=5; $i++)
{
for($j = 1; $j<=$i; $j++)
{
echo $a;
$a++;
}
echo '<br/>';
}
?>
---------------------------------------------------------------------------------------------------------------------
Write a program to make a chess:
Answer
<table width="270px" cellspacing="0px" cellpadding="0px" border="1px">
---------------------------------------------------------------------------------------------------------------------
how to print following table as Output:
Answer
<table align="left" border="1" cellpadding="3" cellspacing="0">
-----------------------------------------------------------------------------------------------------------------------------
<table width="270px" cellspacing="0px" cellpadding="0px" border="1px">
------------------------------------------------------------------------------------------------------------------------
What would be the output of following:
$a = '1';
echo $b = &$a;
echo $c = "2$b";
Answer
OUTPUT:
1 // $b value will be 1
21 // $c value will be 21 as 2 is concatenated to the value of $b.
----------------------------------------------------------------------------------------------------------------------
What would be the output of the following?
var_dump(0123 == 123);
var_dump('0123' == 123);
var_dump('0123' === 123);
Answer
OUTPUT:
bool(false) // As number precede by 0 leads to an octal number.
bool(true) // as 0123 is enclosed in quotes so it would be concerted to it's numeric value which is 123 so it is true.
bool(false) // it's false as o123 is quoted and is string so they are not identical.Their data type is different.
--------------------------------------------------------------------------------------------------------------------
What will be the output of the following:
$x = ture and false;
var_dump($x);
Answer
OUTPUT:
bool(true) // and operator works as OR because = operator has the precedence over and.
$array = array(
1 => "a",
"1" => "b",
1.5 => "c",
true => "d",
);
print_r($array);
Answer
OUTPUT:
Array ( [1] => d )
Here all keys will be type casted to number 1. but the value the array will hold will be of the data type whose precedence is higher.precedence hirarchy in increment order is as follows:
String("1") => Number(1) => Float(1.5) => bool(true). so if we omit true element from array then result will be :
Array ( [1] => c )
and so like that.....
---------------------------------------------------------------------------------------------------------------------
Write a program to concatenate two strings character by character. e.g : JOHN + SMITH = JSOMHINTH
Answer
<?php
-----------------------------------------------------------------------------------------------------------------------
Program to find the LCM of two numbers.
Answer
<?php