php 1 to 8 program pdf
php 1 to 8 program pdf
<?php
echo"hello computer";
?>
OUT PUT.
hello computer
PROGRAM 2 : write a php program which
work on the global keyword.
<?php
$x=10;
$y=20;
function sum() {
global $x,$y;
$y=$x+$y;
}
sum();
echo $y;
?>
OUT PUT.
30
PROGRAM 3 : write a php program to prepare
student marksheet using switch statement.
<html>
<head>
<title>student marksheet</title>
</head>
<body>
<h2>enter student marks</h2>
<form method="get"action="">
enter marks (0-100); <input type="number"
name="marks" required>
<input type="submit" name="submit"
value="check grade">
</form>
<?php
if(isset($_GET['submit']))
{$marks=$_GET['marks'];
$grade="";
switch (true){
case($marks>=90 && $marks<=100);
$grade='A+';
break;
case($marks>=80 && $marks<90);
$grade='A';
break;
case($marks>=70 && $marks<80);
$grade='b';
break;
case($marks>=60 && $marks<70);
$grade='c';
break;
case($marks>=50 && $marks<60);
$grade='d';
break;
case($marks>=0 && $marks<50);
$grade='f';
break;
default:
$grade='invalid marks';
}
echo "<h3>student grade: $grade</h3>";
}
?>
</body>
</html>
OUT PUT.
enter student marks
enter marks (0-100); 89 CHECK GRADE
student grade: A
PROGRAM 4 : write a php program to calculate
sum of given number.
<?php
$num=1234;
$sum=0; $rem=0;
for ($i=0; $i<=strlen($num);$i++)
{
$rem=$num%10;
$sum=$sum+$rem;
$num=$num/10;
}
echo "sum of digits is $sum";
?>
OUT PUT.
sum of digits is 10
PROGRAM 5 : write a php program for check
given number is prime or not.
<form method="get">
enter a number:<input type="text"
name="input"><br>
<br>
<input type="submit" name="submit"
value="submit">
</form>
<?php
if($_GET)
{
$input=$_GET['input'];
for($i=2;$i<=$input-1;$i++){
if($input % $i ==0){
$value=true;
}
}
if(isset($value) && $value){
echo'the number'. $input. 'is not prime';
} else{
echo 'the number'. $input. 'is prime';
}
}
?>
OUT PUT.
enter a number: 67
the number 67 is prime
PROGRAM 6 : write a program for check given
number is Armstrong number or not.
<?php
$num=371;
$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";
}
?>
OUT PUT.
yes it is an armstrong number
PROGRAM 7 :write a php programs to display the
Fibonacci series.
<?php
$num=0;
$n1=0;
$n2=1;
echo "<h3>fibonacci series for first 12 numbers:
</h3>";
echo "\n";
echo $n1.' '.$n2.' ';
while($num<10)
{
$n3=$n2+$n1;
echo $n3.' ';
$n1=$n2;
$n2=$n3;
$num=$num+1;
}
?>
OUT PUT.
FIBONACCI SERIES FOR FIRST 12 NUMBERS:
0 1 1 2 3 5 8 13 21 34 55 89
PROGRAM 8 : write a php program to
demonstrate the use of array functions.
<?php
$car=array("volvo","bmw","maruti");
echo count($car);
echo "<hr>";
?>
<?php
$people=array("jiyan","riya","arohi","sagar");
if(in_array("sagar",$people))
{
echo "match found";
}
else
{
echo "match not found";
}
echo"<hr>";
?>
<?php
$my_array=array("dog","cat","horse");
list($a,$b,$c)=$my_array;
echo"i have a serval animals,a $a,a $b and a $c";
echo"<hr>";
?>
<?php
$car=array("volvo","bmw","maruti");
sort($car);
print_r($car);
echo"<hr>";
?>
<?php
$car=array("bmw","volvo","maruti");
print_r(array_reverse($car));
echo"<hr>";
?>
<?php
$a1=array("red","greean");
$b1=array("blue","yello");
print_r(array_merge($a1,$b1));
echo"<hr>"
?>
<?php
$fruit=array("apple","orange","graps");
array_push($fruit,"banana");
print_r($fruit);
echo"<hr>";
?>
<?php
$fruit=array("apple","orange","graps");
array_pop($fruit);
print_r($fruit);
?>
Out put.
3
match found
Array ( [0] => bmw [1] => maruti [2] => volvo )
Array ( [0] => maruti [1] => volvo [2] => bmw )
Array ( [0] => red [1] => greean [2] => blue [3] =>
yello )
Array ( [0] => apple [1] => orange [2] => graps [3]
=> banana )