0% found this document useful (0 votes)
28 views9 pages

PHP Scripts for Web Programming Tasks

The document outlines practical exercises for web programming using PHP, focusing on database interactions, cookie and session management, and mathematical operations. It includes source code examples for retrieving data from HTML forms, calculating factorials, generating Fibonacci series, finding prime numbers, reversing numbers, and working with arrays. Additionally, it covers file operations such as reading from and writing to files.

Uploaded by

jiinrang
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)
28 views9 pages

PHP Scripts for Web Programming Tasks

The document outlines practical exercises for web programming using PHP, focusing on database interactions, cookie and session management, and mathematical operations. It includes source code examples for retrieving data from HTML forms, calculating factorials, generating Fibonacci series, finding prime numbers, reversing numbers, and working with arrays. Additionally, it covers file operations such as reading from and writing to files.

Uploaded by

jiinrang
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

Web Programming

Practical No. - 5 Roll No. - Date –

Aim – Write PHP script for


a. Working with Databases (Storing records/ Reprieving Records and display the)
[Link] and Retrieving cookies
[Link] and Retrieving sessions

A) Retrieving data from HTML forms


1. File: [Link]
Source code 1 -
<form action="[Link]" method="get">

Name:<input type="text" name="name"/>

<input type="submit" value="value"/>

</form>

B) File: [Link]

Source code 2 -
<?php

$name= $_GET["name"];//receiving name field value in $name variable

echo "welcome, $name";

?>
Output –

C) Performing certain mathematical oprations such as calculating factorian /


finding Fibonaaci Series / Dispaying Prime Numbers in a given range /
Evaluating Expressions / Calculating reserve of a number.
1. Factorial
Source code -
<?php
$n =4 ; $f = 1; for ($x = 1;
$x <= $n; $x++)
{
$f=$f*$x;
}
echo "factorial of $num is $f";
?>
Output -

2) Fibonaaci Series
Source code –
<?php
$n=10;
$a=0; $b=1; echo
"$a $b ";// 0 1 for($i=3
; $i<=$n ;$i++)
{
echo $c=$a+$b; echo"";
$a=$b;
$b=$c;
}
?>
Output -

2. Prime numbers
Source Code –
<?php $n=50; for($j=2;$j<=$n;$j++)
{
for($k=2;$k<$j;$k++)
{
if($j%$k==0)
{
break;
}
}
if($k==$j) echo "prime
number:",$j ."</br>";
}
?>
Output –

3. Reverse Number

Source code -
<?php

$num=139;

$r=0; while($num!=0)

$r=$r*10+$num%10;

$num=(int)($num/10);

}
echo "Reverse number:",$r;

?>
Output-

4. Working with arrays

Source code -
<!DOCTYPE html>

<html>

<body>

<?php $numb=array(1,2,3,4,5);

foreach($numb as $v){ echo

"value=$v";

?>

</body>

</html>
D) Working with files (Reading/Writing)
1. File Writing
Source code

<html>

<body>

<?php

$filename="[Link]";

$file=fopen($filename,"w");

if($file==false){ echo ("Error in opening

new file");

exit();

fwrite($file, "This is a simple test/n"); fclose($file);

?>

</body>

</html>
2. File reading
Source code -
<html>
<head>
<title>Reading a file using PHP</title>
</head>
<body>
<?php
$filename="[Link]";
$file=fopen($filename,"r");
if($file==false){ echo ("error in
opening file"); exit();
}
$filetext=fread($file,filesize($filename)); fclose($file); echo
("<pre>$filetext</pre>");
?>
</body>
</html>
Output -

You might also like