0% found this document useful (0 votes)
346 views6 pages

PHP Lab Programs

The document contains multiple PHP code examples demonstrating different programming concepts: 1. A foreach loop to iterate through and print an array. 2. An if/else ladder to find the largest of three numbers. 3. A function to check if a number is prime using modulo. 4. A program to reverse a number by extracting digits using modulo. 5. A recursive function to print the Fibonacci series. 6. A switch statement to build a basic calculator. 7. A while loop to check if a number is prime using modulo. 8. A simple PHP script to output text on a web page.

Uploaded by

irfanahmed.dba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
346 views6 pages

PHP Lab Programs

The document contains multiple PHP code examples demonstrating different programming concepts: 1. A foreach loop to iterate through and print an array. 2. An if/else ladder to find the largest of three numbers. 3. A function to check if a number is prime using modulo. 4. A program to reverse a number by extracting digits using modulo. 5. A recursive function to print the Fibonacci series. 6. A switch statement to build a basic calculator. 7. A while loop to check if a number is prime using modulo. 8. A simple PHP script to output text on a web page.

Uploaded by

irfanahmed.dba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

<?

php

$ar=array(10,15,20,25,30);

foreach($ar as $val)

echo $val;

?>

Write a PHP program to find the largest of three numbers using if_else ladder.

<?php

$a = 15;

$b = 68;

$c = 600;

echo "<br>The given numbers are $a, $b and $c<br>";

if($a>$b && $a>$c)

echo "$a is Maximum number";

else if($b>$a && $b>$c)

echo "$b is Maximum number";

else

echo "$c is Maximum number";

}
?>

3.

<?php

function primeCheck($number)

if ($number == 1)

return 0;

for ($i = 2; $i <= $number/2; $i++)

if ($number % $i == 0)

return 0;

return 1;

$number = 100;

$flag = primeCheck($number);

if ($flag == 1)

echo "$number is prime number ";

else

echo "$number is Not a prime number "

?>

4 <?php

$num=17936;

$res=0;

echo "Given number is $num";


echo "<br>Reverse of a given number is ";

while($num>1)

$rev=$num%10;

$num=$num/10;

echo "$rev";

?>

4. Write a PHP program to print the Fibonacci series using functions.

<?php

function fibonacci($n)

if($n==0)

return 0;

if($n==1)

return 1;

else

$fib=fibonacci($n-1)+fibonacci($n-2);

return $fib;

$num = 15;

echo "The Given number is $num";


echo "<br>The Fibonacci series of first $num is";

for($i=0;$i<$num;$i++)

$res=fibonacci($i);

echo "<br> $res ";

?>

/* Write a PHP program to implement calculator using switch case.*/

<?php

$a=17;

$b=25;

$opr= '$';

switch($opr)

case '+' : echo "<br> The Operator is $opr";

echo " <br>Addition of $a and $b is " .($a+$b);

break;

case '-' : echo "<br> The Operator is $opr";

echo " <br>Substraction of $a and $b is " .($a-$b);

break;

case '*' :

echo "<br> The Operator is $opr";

echo " <br>Product of $a and $b is " .($a*$b);

break;

case '/' :

echo "<br> The Operator is $opr";

echo " <br>Quotient of $a and $b is " .($a/$b);


break;

case '%' :

echo "<br> The Operator is $opr";

echo " <br>Reminder of $a and $b is " .($a%$b);

break;

default : echo " <br>Inavalid operator or No Operator is selected";

?>

Write a PHP program to check whether the given number is prime or not using for loop.

<?php

$n=4;

$flag=1;

$i=2;

while($i<=$n/2)

if ($n % $i == 0)

$flag=0;

echo "$n is not a Prime number";

break;

$i++;

if ($flag==1)

echo "$n is a Prime number";

?>
<html>

<head>

<title>Welcome</title>

</head>

<body>

<?php

echo "Welcome to PHP Class";

?>

</body>

</html>

You might also like