You are on page 1of 14

MSBTE CAMPUS

PHP IMP QUESTIONS


Msbtebook.blogspot.com
--Basic programs-
1.To check number is prime or not.
<?php
$no=7;
$flag=0;
for($i=2;$i<=$no/2;$i++)
{
if($no%$i==0)
{
$flag=1;
}
}
if($flag==1)
{
echo "Number is not prime";
}
else
{
echo "Number is prime";
}
?>
2.To check number is armstrong or not.
<?php
$no=153;
$rem=0;
$temp=$no;
$sum=0;
while($no>0)
{
$rem=$no%10;
$sum=$sum+$rem**3;
$no=$no/10;
}
if($sum==$temp)
{
echo "Number is armstrong";
}
else
{
echo "Number is not armstrong";
}
?>
3.To check number is palindrome or not.
<?php
$no=555;
$number=strval($no);
$reversenumber=strrev($number);
if($reversenumber==$number)
{
echo "Number is palindrome";
}
else
{
echo "Number is not palindrome";
}
?>
4.Count the sum of digits from number.
<?php
$no=15;
$rem=0;
$sum=0;
while($no>0)
{
$rem=$no%10;
$sum=$sum+$rem;
$no=$no/10;
}
echo "sum of all digits=",$sum;
?>
5.Find the factorial of number.
<?php
$no=5;
$fact=1;
for($i=1;$i<=$no;$i++)
{
$fact*=$i;
}
echo "Factorial=",$fact;
?>

Msbtebook.blogspot.com
6.print the Fibonacci series up to n numbers.
<?php
$no=5;
$no1=0;
$no2=1;
$no3=0;
echo $no1;
echo $no2;
for($i=1;$i<$no;$i++)
{
$no3=$no1+$no2;
echo $no3;
$no1=$no2;
$no2=$no3;
}
?>
7.write a program which reverse the given number.
<?php
$no=18;
strval($no);
print("reverse of $no"."=".strrev($no));
?>

Msbtebook.blogspot.com

--Array related program.


8.find the addition of all the elements in array.
<?php
$a=array(10,20,30,40);
echo "sum of all the elements=",array_sum($a); ?>
without using method-
<?php
$a=array(10,20,30,40);
$sum=0;
foreach($a as $no)
{
$sum+=$no;
}
echo "sum of all the elements=",$sum;
?>
9.find the first occurance of particular number in array .
<?php
$a=array(10,20,30,40);
$no=30;
for($i=0;$i<$a;$i++)
{
if($a[$i]==$no)
{
echo "First occurance of 30=",$i;
break;
}
}
?>

10.find the last occurance of particular number in array.


<?php
$a=array(10,20,30,40,30);
$no=30;
for($i=count($a);$i>0;$i--)
{
if($a[$i]==$no)
{
echo "last occurance of 30=",$i;
break;
}
}
?>
11.find the length of array without using method.
<?php
$a=array(10,20,30,40,30);
$length=0;
$no=0;
while($a[$no]!=null)
{
$length+=1;
$no++;
}
echo "Length of given array=",$length;
?>
12.write a program lambda function.
<?php
$sum = create_function('$a, $b', 'return $a + $b;');
$result = $sum(3, 4);
echo "The result is: " .$result;
?>

String based programs-.


13.find the length of string.(without using method)
<?php
$str="AKASH";
$length=0;
$i=0;
while($str[$i]!=null)
{
$length+=1;
$i++;
}
echo "Length of string=",$length;
?>

14.Count the total number of words in given string.(without using method)


<?php
$str="AKASH IS GPAN STUDENT";
$word=0;
$i=0;
while($i<strlen($str))
{
if($str[$i]==" ")
{
$word+=1;
}
$i++;
}
echo "words in string=",$word+1;
?>
15.reverse the string without using method.
<?php
$str="AKASH";
$rev="";
$i=strlen($str)-1;
while($i>=0)
{
$rev.=$str[$i];
$i--;
}
echo "reverse string=",$rev;
?>

16.find the index of particular character from string(First occurenace).


<?php
$str="AKASH";
$ch="H";
$i=0;
while($i<strlen($str))
{
if($str[$i]==$ch)
{
echo "Index of $ch"."=".$i;
break;
}
$i++;

}
?>
17. find the index of particular character from string(Last occurenace).
<?php

$str="AKASH";
$ch="A";
$i=strlen($str);
while($i>=0)
{
if($str[$i]==$ch)
{
echo "Index of $ch"."=".$i;
break;
}
$i--;

}
?>

Msbtebook.blogspot.com
18. convert the string into uppercase to lowercase without using method.
<?php
$str="AKASH";
$i=0;
$lowerstring="";
$length=strlen($str);
while($i<$length)
{
if(ord($str[$i])>=65 && ord($str[$i])<=90)
{
$lowerstring.="".chr(ord($str[$i])+32);
}
$i++;
}
echo "lowercase string=".$lowerstring;
?>
18. convert the string into lowercase to uppercase without using method.
<?php
$str="akash";
$i=0;
$upperstring="";
$length=strlen($str);
while($i<$length)
{
if(ord($str[$i])>=97 && ord($str[$i])<=122)
{
$upperstring.="".chr(ord($str[$i])-32);
}
$i++;
}
echo "upercase string=".$upperstring;
?>

19.pdf program in php


<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(0, 10, 'Hello, World!', 0, 1, 'C');
$pdf->Output();
?>
19.Cloning of object.
<?php
class akash
{
function msg()
{
echo "Hello i am akash";
}
function msg2()
{
echo "From GPAN";
}
}
$obj=new akash();
$obj->msg();
$obj2=clone $obj;//shallow
$obj2->msg();
$obj3=$obj;//deep
$obj3->msg2();
?>

Msbtebook.blogspot.com

MSBTE CAMPUS
PHP IMP QUESTIONS

You might also like