Final Practical php
Final Practical php
NO:01
DATE:
NAVIGATION BAR
AIM:
To design webpage with links to different pages and allow navigation between webpages.
PROGRAM:
<!DOPCTYPEhtml>
<html>
<head>
<title>
navigationexample
</title>
</head>
<body>
<h1>
WELCOMETOMYWEBSITE
</h1>
<?php if(isset($_GET['page']))
$page=$_GET['page'];
if($page==='home')
include('home.php');
elseif($page==='about')
include('about.php');
}
elseif($page==='contact')
include('contact.php');
?>
<hr>
<h3>
NAVIGATION
</h3>
<ul>
<li><ahref="index.php?page=home">HOME</a></li>
<li><ahref="index1.php?page=about">ABOUT</a></li>
<l><ahref="index2.php?page=contact">CONTACT</a></li>
</ul>
</body>
</html>
OUTPUT:
RESULT:
PROGRAM:
<html>
<body>
<?php
$nameErr=$genderErr=$websiteErr="";
$name=$gender=$comment=$website="";if($_SERVER["REQUEST_METHOD"]=="P
OST"){
if(empty($_POST["name"])){
$nameErr="Nameisrequired";
else{
$name=test_input($_POST["name"]);
if(!preg_match("/^[a-zA-Z-']*$/",$name)){
$nameErr="onlylettersandwhitespaceallowed";
if(empty($_POST["comment"])){
$comment="";
else
$comment=test_input($_POST["comment"]);
}
if(empty($_POST["gender"])){
$genderErr="Genderisrequired";
else{
$gender=test_input($_POST["gender"]);
functiontest_input($data){
$data=trim($data);
$data=stripslashes($data);
$data=htmlspecialchars($data);return
$data;
?>
<h2>PHPformvalidationexample</h2>
<p><spanclass="error">*requiredfield</span></p>
Name:<inputtype="text"name="name"value="<?phpecho$name;?>">
<spanclass="error">*<?phpecho$nameErr;?></span>
<br><br>
Comment:<textareaname="comment"rows="5"cols="40"><?php echo
$comment;?></textarea>
<br><br>
Gender:
<inputtype="radio"name="gender"<?phpif(isset($gender)&&$gender=="female")echo"checked";?>value
="female">Female
<inputtype="radio"name="gender"<?phpif(isset($gender)&&$gender=="male")echo"checked";?>value="
male">Male
<inputtype="radio"name="gender"<?phpif(isset($gender)&&$gender=="other")echo"checked";?>value=
"other">other
<spanclass="error">*<?phpecho$genderErr;?></span>
<br><br>
<inputtype="submit"name="submit"value="submit">
</form>
<?php
echo"<h2>Your input:</h2>";echo
$name;
echo"<br>";echo
$comment;
echo"<br>";echo
$gender;
?>
</body>
</html>
OUTPUT:
RESULT:
To create page using function for comparing integers and print largest number.
PROGRAM:
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<title>
PHPprogramtofindlargestofthreenumbers
</title>
</head>
<body>
<h3>PHPprogramtofindlargestofthreenumbers</h3>
<formaction=""method="post">
Number 1:<input
type="number"name="first"min="0"value="<?=(isset($_POST['first']))?$_POST['first']:25;?>"/><br
/><br />
Number 2:<input
type="number"name="second"min="0"value="<?=(isset($_POST['second']))?$_POST['second']:75
;?>"/><br/><br/>
number 3:<input
type="number"name="third"min="0"value="<?=(isset($_POST['third']))?$_POST['third']:53;?>"/>
<br/><br/>
<inputtype="submit"><br/><br/>
</form>
</body>
</html>
<?php
if($_POST)
$first=$_POST['first'];
$second=$_POST['second']
$third=$_POST['third'];
$temp=($first>$second)?$first:$second;
$largest=($third>$temp)?$third:$temp;
print"<h2>Largestofthreenumberis:".$largest."</h2>";
?>
;
OUTPUT:
RESULT:
PROGRAM :
<html>
<head>
</head>
<body>
<formmethod="post">
<tableborder="0">
<tr>
<td><inputtype="text"name="num"value=""placeholder="Enteranumber"/></td>
</tr>
<tr>
<td><inputtype="submit"name="submit"value="submit"/></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
$n=$_POST['num'];
$factorial=1;
for($i=1;$i<=$n;$i++)
{
$factorial=$factorial*$i;
echo "Factorialofnumber".$n."is:".$factorial;
return 0;
?>
</body>
</html>
OUTPUT:
RESULT:
AIM:
PROGRAM:
<html>
<head>
<title>PHPProgramtocheckagivennumberisprimenumberornot</title>
</head>
<body>
<formmethod="post">
<tableborder="0">
<tr>
<td><inputtype="text"name="num"value=""placeholder="Enterpositive integer"/></td>
</tr>
<tr>
<td><inputtype="submit"name="submit"value="submit"/></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
$n=$_POST['num'];
$flag = 0;
for($i=2;$i<=$n/2;$i++)
{
$flag=1;
break;
if($flag==0)
echo"$nisaprimenumber"; else
echo"$nisnotaprimenumber"; return
0;
?>
</body>
</html>
OUTPUT:
RESULT:
PROGRAM:
<html>
<head>
<title>NumbertoWordConverter</title>
</head>
<body>
<h1>NumbertoWordConverter</h1>
<formmethod="POST">
<inputtype="number"name="number"placeholder="Enteranumber">
<buttontype="submit">Convert</button>
</form>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
$number=$_POST["number"];
$teens=array(
10=>'Ten',
11=>'Eleven',
12=>'Twelve',
13=>'Thirteen',
14=>'Fourteen',
15=>'Fifteen',
16=>'Sixteen',
17=>'Seventeen',
18=>'Eighteen',
19=>'Nineteen'
);
$tens=array(
2=>'Twenty',
3=>'Thirty',
4=>'Forty',
5=>'Fifty',
6=>'Sixty',
7=>'Seventy',
8=>'Eighty',
9=>'Ninety'
);
if($number<10){
return$ones[$number];
} elseif ($number < 20){
return$teens[$number];
}elseif($number<100){
$tensDigit=floor($number/10);
$onesDigit=$number% 10;
$result=$tens[$tensDigit];if
($onesDigit> 0) {
$result.=''. $ones[$onesDigit];
}
return$result;
}elseif($number<1000){
$hundredsDigit=floor($number/100);
$remainder=$number%100;
$result=$ones[$hundredsDigit].'Hundred';if
($remainder > 0) {
$result.=''.convertNumberToWord($remainder);
}
return$result;
}else{
return'Numberoutofrange';
}
}
echo'<p>'.convertNumberToWord($number).'</p>';
}
?>
</body>
</html>
OUTPUT:
RESULT:
PROGRAM:
<html>
<head>
<title>PalindromeChecker</title>
</head>
<body>
<h1>PalindromeChecker</h1>
<formmethod="POST">
<inputtype="text"name="inputString"placeholder="Enterastring">
<buttontype="submit">Check</button>
</form>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
$inputString=$_POST["inputString"];
functionisPalindrome($string){
$string=strtolower(str_replace("","",$string));
$reversedString=strrev($string);
if($string==$reversedString){ return
true;
}else{
returnfalse;
}
}
if(isPalindrome($inputString)){
echo"<p>Thestring'$inputString'isapalindrome.</p>";
}else{
echo"<p>Thestring'$inputString'isnotapalindrome.</p>";
}
}
?>
</body>
</html>
OUTPUT:
RESULT:
To find the greeting depending upon current time zone using php.
PROGRAM:
<html>
<head>
<title>PHPProgramforDisplayingGreetingdependinguponCurrentTimeZone</title>
</head>
<body>
<h3>DisplayingGreetingdependinguponCurrentTimeZone</h3
<?php
date_default_timezone_set("Asia/Kolkata");
$h=date('G');
if($h>=5&&$h<=11)
{
echo"Goodmorning";
}
elseif($h>=12&&$h<=15)
{
echo"Goodafternoon";
}
else
{
echo"Goodevening";
}
?>
</body>
</html>
OUTPUT:
RESULT:
PROGRAM:
<?php
$birthday="2004-05-26";
$currentDate=date("y-m-d");
$diff=strtotime($birthday)-strtotime($currentDate);
$days=floor($diff/(60*60*24));
$hours=floor(($diff%(60*60*24))/(60*60));
$minutes=floor(($diff%(60*60))/60);
$seconds=$diff%60;
echo"countdownto birthday:";
echo$days."days,";
echo$hours."hours,";
echo$minutes."minutes,";
echo$seconds."seconds,";
?>
OUTPUT:
RESULT:
PROGRAM:
<html>
<head>
<title>
PHP-calculateelectricittybill
</title>
</head>
<?php
$result_str=$result='';
if(isset($_POST['unit-submit']))
{
$units=$_POST['units'];
if(!empty($units))
{
$result=calculate_bill($units);
$result_str='Totalamountof'.$units.'-'.$result;
}
}
functioncalculate_bill($units)
{
$unit_cost_first=3.50;
$unit_cost_second=4.00;
$unit_cost_third=5.20;
$unit_cost_fourth=6.50;
if($units<=50)
{
$bill=$units*$unit_cost_first;
}
elseif($units>50&&$units<=100)
{
$temp=50*$unit_cost_first;
$remaining_units=$units-50;
$bill=$temp+($remaining_units*$unit_cost_second);
}
elseif($units>100&&$units<=200)
{
$temp=(50*3.5)+(100*$unit_cost_second);
$remaining_units=$units-150;
$bill=$temp+($remaining_units*$unit_cost_third);
}
else{
$temp=(50*3.5)+(100*$unit_cost_second)+(100*$unit_cost_third);
$remaining_units=$units-250;
$bill=$temp+($remaining_units*$unit_cost_fourth);
}
returnnumber_format((float)$bill,2,'.','');
}
?>
<body>
<divid="page-wrap">
<h1>
Php-CalculateElectricityBill
</h1>
<formaction=""method="post"id="quiz-form">
<inputtype="number"name="units"id="units"placeholder="pleaseenterno.ofunits"/>
<inputtype="submit"name="unit-submit"id="unit-submit"value="Submit"/>
</form>
<div>
<?phpecho'<br/>'.$result_str;?>
</div>
</div>
</body>
</html>
OUTPUT:
RESULT:
PROGRAM:
<html>
<head>
<title>submitfromexample</title>
</head>
<body>
<formmethod="post">
<labelfor="name">enteryourmailid:</label>
<inputtype="text"id="email"name="email">
<inputtype="submit"value="submit">
</form>
<?php
if($_SERVER['REQUEST_METHOD']==='POS
T')
{
$email= $_POST['email'];
if(filter_var($email,FILTER_VALIDATE_EMAIL))
{
echo"emailaddressisvalid";
}
else
{
echo"emailaddressisnotvalid";
}
}
?>
</body>
</html>
OUTPUT:
RESULT:
PROGRAM:
<?php session_start();
?>
<!DOCTYPEhtml>
<html>
<body>
<?php
$_SESSION["favcolor"]="brown";
$_SESSION["favanimal"]="cat";print_r($_SE
SSION);
?>
</body>
</html>
OUTPUT:
RESULT:
PROGRA
M
<?php
$cookie_name="user";
$cookie_value="JohnDoe";
setcookie($cookie_name,$cookie_value,time()+(86400*30),"/");//86400=1 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])){
echo"Cookienamed'".$cookie_name."'isnotset!";
}else{
?>
</body>
</html>
OUTPUT:
RESULT:
PROGRAM:
<!DOCTYPEhtml>
<html>
<head>
<title>fileupload</title>
</head>
<body>
<?phpif($_SERVER['REQUEST_METHOD']==='POST'&&isset($_FILES['uploadedFi
le']))
$targetDir='E:\UPLOAD';
$targetFile=$targetDir.basename($_FILES['uploadedFile']['name']);if(file_exists($target
File))
echo'Filealreadyexists.';
else {
if(move_uploaded_file($_FILES['uploadedFile']['tmp_name'],$targetFile))
echo'Fileuploadedsuccessfully.';
else{
echo'failedtouploadafile.';
?>
<h1>FileUpload</h1>
<formmethod="POST"enctype="multipart/form-data">
<inputtype="file"name="uploadedFile">
<inputtype="submit"value="Upload">
</form>
</body>
</html>
OUTPUT:
RESULT: