Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
32 views

Exercise 6: Control Statemets and Looping

Some programs

Uploaded by

NishaPauline
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Exercise 6: Control Statemets and Looping

Some programs

Uploaded by

NishaPauline
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

PHP programs

Exercise 6: CONTROL STATEMETS AND LOOPING

PROGRAM

(fibo.php)

<?php

function Fibonacci($number)

if($number==0)

return 0;

else if($number==1)

return 1;

else

return(Fibonacci($number-1)+Fibonacci($number-2));

$number =10;

for($counter=0;$counter<$number;$counter++)

echo Fibonacci($counter), ' ';

?>

OUTPUT

0 1 1 2 3 5 8 13 21 34

Exercise 7: FUNCTIONS

<?php
Function gcd($a,$b)

If($a==0||$b==0)

Return 0;

If($a==$b)

Return $a;

If($a>$b)

Return gcd($a-$b,$b);

Return gcd($a,$b-$a);

$a=98;

$b=56;

Echo “GCD of $a and $b is : “, gcd($a,$b)

?>

Exercise 8: FORM PROCESSING (GET METHOD)

<html>

<head>

<title>get_browser</title>

<?php

Error_reporting(1);

$x=$_GET[‘f’];

$y=$_GET[‘s’];

$z=$x+$y;

Echo “Sum of two number = “.$z;

?>

</head>

<body bgcolor=”sky color”>

<form method=”GET” >


<table border=”1” bgcolor=”yellow”>

<tr>

<td>Enter your first number</td>

<td><input type=”text” name=”f”/></td>

</tr>

<tr>

<td>Enter your second number</td>

<td><input type=”text” name=”s”/></td>

</tr>

Exercise 8: FORM PROCESSING (POST METHOD)

PROGRAM

(action.php)

<?php

$name=$_POST[‘name’];

$mail=$_POST[‘mail’];

$comment=$_POST[‘comment’];

Echo “<p> Thanks for this comment $name…..</p>”;

Echo “<p><i>$comment</i></p>”;

Echo “<p>We will reply to $mail</p>”;

?>

(action_handler.html)

<form action=”action_handler.php” method=”POST”>

<dl>

<dt>Name:

<dd><input type=”text” name=”name”>


<dt>Email Address:

<dd><input type=”type” name=”mail”>

<dt>Comments:

<dd><textarea rows=”8” cols=”20” name=”comment”>

</textarea>

</dl>

<p><input type=”submit”></p>

</form>

Exercise 9:VALIDATION

<html>

<head>

<style>

.error {color: #0000FF;}

</style>

</head>

<body>

<?php

// define variables and set to empty values

$nameErr = $emailErr = $genderErr = $websiteErr = "";

$name = $email = $gender = $class = $course = $subject = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

if (empty($_POST["name"])) {

$nameErr = "Name is required";

}else {

$name = test_input($_POST["name"]);

if (empty($_POST["email"])) {

$emailErr = "Email is required";


}else {

$email = test_input($_POST["email"]);

// check if e-mail address is well-formed

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

$emailErr = "Invalid email format";

if (empty($_POST["course"])) {

$course = "";

}else {

$course = test_input($_POST["course"]);

if (empty($_POST["class"])) {

$class = "";

}else {

$class = test_input($_POST["class"]);

if (empty($_POST["gender"])) {

$genderErr = "Gender is required";

}else {

$gender = test_input($_POST["gender"]);

if (empty($_POST["subject"])) {

$subjectErr = "You must select 1 or more";

}else {

$subject = $_POST["subject"];

}
}

function test_input($data) {

$data = trim($data);

$data = stripslashes($data);

$data = htmlspecialchars($data);

return $data;

?>

<h2>ONLINE COURSE REGISTRATION</h2>

<p><span class = "error">* required field.</span></p>

<form method = "POST" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<table>

<tr>

<td>Name:</td>

<td><input type = "text" name = "name">

<span class = "error">* <?php echo $nameErr;?></span>

</td>

</tr>

<tr>

<td>E-mail: </td>

<td><input type = "text" name = "email">

<span class = "error">* <?php echo $emailErr;?></span>

</td>

</tr>

<tr>

<td>Time:</td>

<td> <input type = "text" name = "course">

<span class = "error"><?php echo $websiteErr;?></span>


</td>

</tr>

<tr>

<td>Class:</td>

<td> <textarea name = "class" rows = "1" cols = "5"></textarea></td>

</tr>

<tr>

<td>Gender:</td>

<td>

<input type = "radio" name = "gender" value = "female">Female

<input type = "radio" name = "gender" value = "male">Male

<input type = "radio" name = "gender" value = "transgender">Transgender

<span class = "error">* <?php echo $genderErr;?></span>

</td>

</tr>

<tr>

<td>Select:</td>

<td>

<select name = "subject[]" size = "4" multiple>

<option value = "Android">Android</option>

<option value = "Java">Java</option>

<option value = "C#">C#</option>

<option value = "Data Base">Data Base</option>

<option value = "Hadoop">Hadoop</option>

<option value = "VB script">VB script</option>

</select>

</td>
</tr>

<tr>

<td>Agree</td>

<td><input type = "checkbox" name = "checked" value = "1"></td>

<?php if(!isset($_POST['checked'])){ ?>

<span class = "error">* <?php echo "You must agree to terms";?></span>

<?php } ?>

</tr>

<tr>

<td>

<input type = "submit" name = "submit" value = "Submit">

</td>

</tr>

</table>

</form>

<?php

echo "<h2>Your given values are as :</h2>";

echo ("<p>Your Name is $name</p>");

echo ("<p>Your Email Address is $email</p>");

echo ("<p>Your Class Time at $course</p>");

echo ("<p>your Class Info $class </p>");

echo ("<p>your Gender is $gender</p>");

for($i = 0; $i < count($subject); $i++) {

echo($subject[$i] . " ");


}

?>

</body>

</html>

Exercise 10: COOKIES

<?php

Setcookie(“usr”, “rizwan ahmed”);

Setcookie(“color”,”blue”);

?>

<html>

<head><title>The cookie Array</title></head>

<body bgcolor=”cyan”>

<font face=”verdana” size=’+1’>

<h2>$_COOKIE[]</h2>

<?php

If(!empty($_COOKIE[‘color’])){

Echo “<pre>”;

Print_r($_COOKIE);

Echo “</pre>”;

?>

</font>

</body>

</html>

You might also like