Updated - PHP Programs

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

PHP Programs

1. Write an Interactive PHP Color Sampler program by taking RGB as input.


File:color.html
<html>
<head>
<title>ColorCode</title>
</head>
<body>
<h2>An Interactive PHP Color Sampler</h2>
<form action="display.php">
R: <input type="text" name="r" size="3" /> <p />
G: <input type="text" name="g" size="3" /> <p />
B: <input type="text" name="b" size="3" /> <p />
<input type="submit" value="Show me" />
</form>
</body>
</html>

File: display.php
<html>
<head>
<title>ColorDisplay</title>
</head>
<body>
<h2>An Interactive PHP Color Sampler</h2>
<?php
// get input values
$r = $_GET['r'];
$g = $_GET['g'];
$b = $_GET['b'];
// generate RGB string from input
$rgb = $r . ',' . $g . ',' . $b;
?>
R: <?php echo $r; ?><br>
G: <?php echo $g; ?><br>
B: <?php echo $b; ?>
<p />
<div style="width:150px; height: 150px;
background-color: rgb(<?php echo $rgb; ?>)" />
</body>
</html>
2. Write a PHP program to read height and weight. Compute and print Body Mass Index (BMI).

Body mass index (BMI) is a measure of body fat based on height and weight that applies to adult
men and women.

The formula is BMI = kg/m2


where kg is a person’s weight in kilograms and m2 is their height in meters squared.

BMI Categories:
Underweight = <18.5
Normal weight = 18.5–24.9
Overweight = 25–29.9
Obesity = BMI of 30 or greater

File: bmi.php
<html>
<head>
<title>BMI Calculator</title>
</head>
<body>
<h1 align="center">Body Mass Index Calculator</h1>

<form action="bmicalc.php" method="post">


<table border="2px" align="center" cellpadding="5">
<tr><td> Enter your Weight</td>
<td><input type="float" name="weight" placeholder="Weight in Kg" required></td></tr>
<tr><td> Enter your Height</td>
<td><input type="float" name="height" placeholder="Height in mtrs" required></td></tr>
<tr><td align="center"><button>Calculate BMI</button></td></tr>
</table>
</form>
</body>
</html>

File:bmicalc.php
<?php
$val1= $_POST["weight"];
$val2= $_POST["height"];
$val3= $val2*$val2;
$mainval=$val1 / $val3;
$bmi=substr($mainval,0,4);

?>
<h2 align="center">Body Mass Index Calculator</h2>
<h2 align="center"><?=" Your BMI value is=". $bmi ?></h2>

<?php
if($bmi<=18.5){
?>
<h2 align="center">Oops!! you are Underwieght</h2>
<?php
}elseif($bmi>18.5 && $bmi<25){
?>
<h2 align="center">Ooh!! you are FIT</h2>
<?php
}elseif($bmi>25 && $bmi<30){
?>
<h2 align="center">Oops!! you are Overwieght</h2>
<?php
} else {
?>
<h2 align="center">Oops!! you have Obessity</h2>
<?php
}
?>
3. Write a PHP program to print factorial of a given number.
File: Fact.php
<html lang>
<head>
<title>Factorial</title>
</head>
<body>
<form align="center" action="Fact.php" method="post">
<h1 align="center">Factorial of a number</h1>
<h3> Enter a Number :<br>
<input type="number" name="num" ><br>
<input type="Submit" name="Submit"></h3>
</form>
</body>
</html>

<?php
$num=$_POST["num"];
$fact=1;

for($i=$num; $i>0; $i--){


$fact=$fact * $i;
}

echo "<h2 align='center'> Factorial of $num is=". $fact."</h2>";


?>
4. Write a PHP program to do the simple calculator operation.
File: SimpleCalc.php
<?php
$num1=$_POST["num1"];
$num2=$_POST["num2"];
$op=$_POST["op"];
switch($op){
case "+" :
$res= $num1 + $num2;
break;
case "-" :
$res= $num1 - $num2;
break;
case "/" :
if($num2==0){
echo "<h3 align='center'>Division is not possible / by zero error";
exit;
}else {
$res= $num1 / $num2;
break;
}
case "*" :
$res= $num1 * $num2;
break;
case "%" :
$res= $num1 % $num2;
break;
default :
$res= "Invald_operator";
}
?>
<html>
<head>
<title>Simple Calc</title>
</head>
<body>
<h2 align="center"> A simple Calculator Operation </h2>
<form action="SimpleCalc.php" method="post" >
<table align="center" border="2" cellpadding="5">
<tr><th colspan="2">Simple Calculator</th>
<tr><td>First Number:</td><td><input type="number" name="num1" ></td></tr>
<tr><td align="right">Operator </td><td><input type="text" name="op"></td></tr>
<tr><td>Second Number:</td><td><input type="number" name="num2"></td></tr>
<tr><td><input type="submit"></td>
<td><input type="text" value=<?php echo $res ?> disabled></td> </tr>
</table>
</form>
</body>
</html>
5. Write a PHP program to print and count the prime numbers given in the range using functions.
Read the range from HTML.
File: prime.php
<html>
<head>
<title>Prime Number</title>
</head>
<body>
<h2 align="center"> Prime numbers in the range</h2>
<form action="prime.php" method="post">
<table border="3px" align="center" >
<tr><td align="center">Enter the first number</td>
<td><input type="number" name="num1" placeholder="num1"></td></tr>
<tr><td align="right">Enter the second number</td>
<td><input type="number" name="num2" placeholder="num2"></td></tr>
<tr align="center"><td><input type="submit" name="submit"></td></tr>
</table></form>
</body>
</html>

<?php
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$prime_nums=array();
echo "<br> <br>";
echo "<h2 align='center'> Prime numbers from " . $num1 . " to ". $num2 . " are : ";
function prime($num1, $num2){
$x= $num1;
while ($num1 <=$num2){
$isprime=true;
for($i=2; $i<= $num1/2; ++$i){
if($num1%$i==0){
$isprime=false;
break;
}
}
if($isprime==true){
$prime_nums[] = $num1 ;
}
++$num1;
}
$count= count($prime_nums);
foreach($prime_nums as $prime){
echo $prime . ", ";
}
echo "<br align='center'>There are " . $count ." number of prime numbers from " . $x . " to " .
$num2 ;
}
prime($num1, $num2);
?>
6. Write a program to validate user credentials. HTML Login page with username and password
fields and PHP program to validate the credentials stored in an array.
File: login.php
<?php

/* Check Login form submitted */


if(isset($_POST['Submit'])){
/* Define username and associated password array */
$logins = array('Guru' => '123456','user1' => 'pass1','user2' => 'pass2');

/* Check and assign submitted Username and Password to new variable */


$Username = isset($_POST['Username']) ? $_POST['Username'] : ' ';
$Password = isset($_POST['Password']) ? $_POST['Password'] : ' ';

/* Check Username and Password existence in defined array */


if (isset($logins[$Username]) && $logins[$Username] == $Password){
/* Success: redirect to Protected page */
echo" <h2 align='center'>Congratulation! You have logged into password
protected page. <a href='logout.php'>Click here</a> to Logout.";
exit;
} else {
/*Unsuccessful attempt: Set error message */
$msg="<span style='color:red'>Invalid Login Details</span>";
}
}
?>

<h2 align="center"> Simple Login page for User </h2>


<form action="" method="post" name="Login_Form">
<table width="300" border="2" align="center" cellpadding="5">
<?php if(isset($msg)){?>
<tr>
<td colspan="2" align="center" valign="top"><?php echo $msg;?></td>
</tr>
<?php } ?>
<tr>
<td colspan="2" align="left" valign="top"><h3>Login</h3></td>
</tr>
<tr>
<td align="right" valign="top">Username</td>
<td><input name="Username" type="text"></td>
</tr>
<tr>
<td align="right">Password</td>
<td><input name="Password" type="password"></td>
</tr>
<tr>
<td> </td>
<td><input name="Submit" type="submit" value="Login"></td>
</tr>
</table>
</form>

File: logout.php
<?php
header("location:login.php"); /* Redirect to login page */
exit;
?>

You might also like