ITR ALL ASSIGNMENT Edited
ITR ALL ASSIGNMENT Edited
1. Create a page evenodd.php into root directory of your local web server. Here, write a
script for finding out whether it is an odd number or even number and echo on the screen.
The following numbers set can be taken as input: 11, 23, 72 and 44.
Answer:
<html>
<body>
<?php
$numbers=array(11,23,72,44);
foreach($numbers as $value)
{
if($value%2==0)
{
echo "The Number $value is an Even number<br>";
}
else
{
echo "The Number $value is an Odd number<br>";
}
}
?>
</body>
</html>
Output:
1|Page
2. Create a page vowel.php into root directory of your local web server. Write in the script
for determining whether or not the character associated with a variable is Vowel or a
Consonant. If it’s a Vowel echo out the name of the vowel otherwise echo out its a
Consonant. Use Switch for this script.
Answer:
<html>
<body>
<?php
$ch = 'a';
switch ($ch)
{
case 'a':
case 'A':
echo " $ch is vowel";
break;
case 'e':
case 'E':
echo "$ch is vowel";
break;
case 'i':
case 'I':
2|Page
case 'u':
case 'U':
echo "$ch is vowel";
break;
default:
echo "$ch is consonant";
break;
}
?>
</body>
</html>
Output:
3|Page
II) PHP Operators
1. Write a PHP Script to declare 2 variables with specific numeric value of your choice and
Perform the following operations: Addition, Subtraction, Multiplication and Division. Also
echo out the Result on the screen.
Answer:
<html>
<body>
<?php
$a=10;
$b=2;
echo "Addition is: ".$a+$b."<br>";
echo "Substraction is: ".$a-$b."<br>";
echo "Multiplication is: ".$a*$b."<br>";
echo "Division is: ".$a/$b;
?>
</body>
</html>
Output:
4|Page
2. Write a PHP Script to declare 2 variables with specific numeric value of your choice and
find out the greater number between the two. If the numbers are equal, the respective
message must appear on the screen.
Answer:
<html>
<body>
<?php
$num1=12;
$num2=10;
if($num1==$num2)
{
echo "$num1 is equal to $num2";
}
else if($num1>=$num2)
{
echo "$num1 is greater";
}
else
{
echo "$num2 is greater";
}
?>
</body>
</html>
Output:
5|Page
III) Arrays
1. Declare an array week and assign the value of the days to each index number in the order
of occurrence and echo the result on the screen.
Answer:
<html>
<body>
<?php
$week=array(1=>"Monaday",2=>"Tuesday",3=>"wednesday",4=>"Thursday",5=>"Friday",6
=>"Saturday",7=>"Sunday");
echo $week[1]."<br>";
echo $week[2]."<br>";
echo $week[3]."<br>";
echo $week[4]."<br>";
echo $week[5]."<br>";
echo $week[6]."<br>";
echo $week[7]."<br>";
?>
</body>
</html>
Output:
6|Page
2. Write a PHP script to add the two 2x2 Matrices and output the result.
Answer:
<html>
<body>
<?php
$one=array( array(6,2),array(11,3));
$two=array (array(5,3),array(4,3));
for($i=0;$i<2;$i++)
for($j=0;$j<2;$j++)
echo "<br>";
?>
</body>
</html>
Output:
7|Page
IV) Loops
1. Write a PHP Script to print the following pattern on the Screen:
*****
****
***
**
*
Answer:
<html>
<head>
<title>loop</title>
<head>
<body>
<?php
for($i=5;$i>0;$i--)
for($j=1;$j<=$i;$j++)
echo "*";
echo "<br>";
?>
</body>
</html>
8|Page
Output:
2. Declare an array variable "x" assigning it with three elements "one, two and three" and Use
for each loop to print the values of the given array.
Answer:
<html>
<head>
<title>foreach</title>
<head>
<body>
<?php
$x=array("one","two","three");
foreach($x as $value)
{
echo $value."<br>";
}
?>
</body>
</html>
Output:
9|Page
V) Functions
1. Declare and define a function named writeName and within it echo out: Krupa
2. Now call the function to retrieve the following output: My name is Krupa
Answer:
<html>
<body>
<?php
function writeName($name)
{
echo "My Name is $name";
}
writename("Krupa ");
?>
</body>
</html>
Output:
10 | P a g
e
3. Declare a function named writeName2 with a parameter $firstname and define it by
echoing out:
$firstname." Sharma". Now call the function to retrieve the following output:
Answer:
<html>
<head>
<title>function2</title>
</head>
<body>
<?php
$firstname=array(1=>"Jai",2=>"Ram");
function writename2($firstname)
{
echo "MY name is ".$firstname[1]." Sharma"."<br>";
echo "MY Brother's name is ".$firstname[2]." Sharma"."<br>";
}
writename2($firstname);
?>
</body>
</html>
Output:
11 | P a g
e
VI) PHP Special Variables and PHP and HTML
1. If the three sides of a triangle are entered by the user, write a program to check whether the
triangle is isosceles, equilateral, scalene or right angled triangle. Use the 'Get' method to post
the form.
Answer:
<html>
<form action='GET.php' method='GET'>
<h4 style="color:red;">enter side1</h4>
<input type='text' name='side1'><br>
<h4 style="color:red;">enter side2</h4>
<input type='text' name='side2'><br>
<h4 style="color:red;">enter side3</h4>
<input type='text' name='side3'><br>
<h4 style="color:blue;">please click below button to submit:!!</h4>
<input type='submit' value='click here'>
</form>
</html>
<?php
error_reporting(E_ALL);
$side1=$_GET['side1'];
$side2=$_GET['side2'];
$side3=$_GET['side3'];
if($side1 && $side2 && $side3)
{
if($side1!=$side2 && $side1!=$side3 && $side2!=$side3)
{
echo "This is scalane triangle";
}
else if($side1==$side2 && $side1==$side3 && $side2==$side3)
{
echo "This is equilateral triangle";
}
else
{
echo "This is isosceles triangle";
}
}
12 | P a g
e
else
{
echo "please fill all boxes";
}
?>
Output:
2. Create a php page and create a user form which asks for marks in five subjects out of 100
and then displays the marksheet of the student. The format is as follows:
Name of Student*:
Marks in Each Subject
Subject 1* :
Subject 2* :
Subject 3* :
Subject 4* :
Subject 5* :
Note: All the entries marked (*) are to be input by the user. And use a submit button to post
the entries in the form using the POST method.
13 | P a g
e
Answer:
<html>
<head>
<title>MARKSHEET |Krupa kasar</title>
</head>
<body>
<form action='post.php' method='POST'>
<b><p style="color:red;">enter student name :<input type='text' name='Name'></p>
<p style="color:red;">enter 1st subject marks :<input type='text' name='sub1'></p>
<p style="color:red;">enter 2nd subject marks :<input type='text' name='sub2'></p>
<p style="color:red;">enter 3rd subect marks :<input type='text' name='sub3'></p>
<p style="color:red;">enter 4th subject marks :<input type='text' name='sub4'></p>
<p style="color:red;">enter 5the subject marks :<input type='text' name='sub5'></p>
<p style="color:blue;">please click button to submit:!!<input type='submit' value='click
here'></p></b>
<?php
$Name=$_POST['Name'];
$sub1=$_POST['sub1'];
$sub2=$_POST['sub2'];
$sub3=$_POST['sub3'];
$sub4=$_POST['sub4'];
$sub5=$_POST['sub5'];
if($Name && $sub1 && $sub2 && $sub3 && $sub4 && $sub5)
{
echo "Name of Student*: ".$Name."<br>";
echo "marks in each subject:<br>";
echo "Subject 1*: ".$sub1."<br>";
echo "Subject 2*: ".$sub2."<br>";
echo "Subject 3*: ".$sub3."<br>";
echo "Subject 4*: ".$sub4."<br>";
echo "Subject 5*: ".$sub5."<br>";
$total=($sub1+$sub2+$sub3+$sub4+$sub5);
echo "totals mark obtained: ".$total."<br>";
echo "Total Marks: 500<br>";
echo "percentage: ".($total/5);
}
14 | P a g
e
else
{
echo "please fill all details<br>";
}
?>
</form>
</body>
</html>
Output:
15 | P a g
e
ASSIGNMENT NO 2
MYSQL
1. Create a php page and create a user form which asks for marks in five subjects out of 100
and then displays the mark sheet of the student. The format is as follows:
Name of Student*:
Marks in Each Subject
Subject 1* :
Subject 2* :
Subject 3* :
Subject 4* :
Subject 5* :
Total Marks Obtained:
Total Marks:
Percentage:
Answer:
<html>
<head>
<title>MARKSHEET |Krupa kasar</title>
</head>
<body>
<form action='post.php' method='POST'>
<input type='text' name='Name'><br>
<input type='text' name='sub1'><br>
<input type='text' name='sub2'><br>
<input type='text' name='sub3'><br>
<input type='text' name='sub4'><br>
<input type='text' name='sub5'><br>
<input type='submit' value='click here'></p><br>
<?php
require("connect.php");
$Name=$_POST['Name'];
$sub1=$_POST['sub1'];
$sub2=$_POST['sub2'];
$sub3=$_POST['sub3'];
16 | P a g
e
$sub4=$_POST['sub4'];
$sub5=$_POST['sub5'];
$total_marks=500;
if($Name && $sub1 && $sub2 && $sub3 && $sub4 && $sub5)
{
echo "Name of Student*: ".$Name."<br>";
echo "marks in each subject:<br>";
echo "Subject 1*: ".$sub1."<br>";
echo "Subject 2*: ".$sub2."<br>";
echo "Subject 3*: ".$sub3."<br>";
echo "Subject 4*: ".$sub4."<br>";
echo "Subject 5*: ".$sub5."<br>";
$total=($sub1+$sub2+$sub3+$sub4+$sub5);
echo "totals mark obtained: ".$total."<br>";
echo "Total Marks:$total<br>";
$percentage=($total/5);
echo "percentage:".$percentage;
mysqli_query($connect,"INSERT INTO class1 VALUES('
','$Name','$sub1','$sub2','$sub3','$sub4','$sub5','$total','$total_marks','$percentage')");
}
else
{
echo "please fill all details<br>";
}
?>
</form>
</body>
</html>
Output:
17 | P a g
e
2. In the previous assignment use the following data:Name: Rohan Marks in Subject(s):
55,66,77,88,76.
Now write a PHP Script to update the values in the database with the new marks in subject 5
as "99" and recalculating and updating database entries: total obtained and percent.
Answer:
<html>
<head>
<title>MARKSHEET |krupa kasar</title>
</head>
<body>
<form action='post.php' method='POST'>
<input type='text' name='Name'><br>
<input type='text' name='sub1'><br>
<input type='text' name='sub2'><br>
<input type='text' name='sub3'><br>
<input type='text' name='sub4'><br>
<input type='text' name='sub5'><br>
<input type='submit' value='click here'></p><br>
<?php
require("connect.php");
$Name=$_POST['Name'];
$sub1=$_POST['sub1'];
$sub2=$_POST['sub2'];
$sub3=$_POST['sub3'];
$sub4=$_POST['sub4'];
$sub5=$_POST['sub5'];
$total_marks=500;
if($Name && $sub1 && $sub2 && $sub3 && $sub4 && $sub5)
{
echo "Name of Student*: ".$Name."<br>";
echo "marks in each subject:<br>";
echo "Subject 1*: ".$sub1."<br>";
echo "Subject 2*: ".$sub2."<br>";
echo "Subject 3*: ".$sub3."<br>";
echo "Subject 4*: ".$sub4."<br>";
echo "Subject 5*: ".$sub5."<br>";
$total=($sub1+$sub2+$sub3+$sub4+$sub5);
echo "totals mark obtained: ".$total."<br>";
18 | P a g
e
echo "Total Marks:$total<br>";
$percentage=($total/5);
echo "percentage:".$percentage;
mysqli_query($connect,"UPDATE class1 SET sub1='$sub1' where name='Rohan'");
mysqli_query($connect,"UPDATE class1 SET sub2='$sub2' where name='Rohan'");
mysqli_query($connect,"UPDATE class1 SET sub3='$sub3' where name='Rohan'");
mysqli_query($connect,"UPDATE class1 SET sub4='$sub4' where name='Rohan'");
mysqli_query($connect,"UPDATE class1 SET sub5='$sub5' where name='Rohan'");
mysqli_query($connect,"UPDATE class1 SET mark_obtained='$total'
where name='Rohan'");
mysqli_query($connect,"UPDATE class1 SET percentage='$percentage'
where name='Rohan'");
}
else
{
echo "please fill all details<br>";
}
?>
</form>
</body>
</html>
Output:
BEFORE UPDATING:
19 | P a g
e
AFTER UPDATING:
20 | P a g
e
ASSIGNMENT NO 3
1 Simple Visitor Counter Implement a Simple Visitor Counter in the Assignment associated
with MYSQL.
Answer:
<html>
<head>
<title> counter</title>
</head>
<body>
<?php
$file=file_get_contents("count.txt");
$visitor=$file;
$visitorsnew= $visitor+1;
$filenew=fopen("count.txt","w");
fwrite($filenew,$visitorsnew);
?>
</body>
</html>
21 | P a g
e
Output:
2 Create a PHP Script which asks for user to enter some random string. Use the appropriate
string functions to display the following operation on the input string.
Count number of Characters in the string
. Breaking down a string into an array
Reverse the string
Convert all alphabetic characters in string to their lower-case form.
Convert all alphabetic characters in string to their upper-case form
Declare a substring and replace the content of substring into original string.
Answer:
<html>
<head>
<title> string1</title>
</head>
<body>
</form>
</body>
</html>
<?php
$string=$_GET['name'];
if($string)
?>
23 | P a g
e
Output:
3 Create a file upload for and once uploaded display the information of the file uploaded (Ex.
File Name and File Size).
Answer:
<head>
<title> upload</title>
</head>
<body>
<h3>upload file</h3>
24 | P a g
e
<?php
if(isset($_POST['submit']))
{
$name=$_FILES['file']['name'];
$type=$_FILES['file']['type'];
$size=$_FILES['file']['size'];
$temp=$_FILES['file']['tmp_name'];
$store="uploaded/".$name;
$error=$_FILES['file']['error'];
if($error>0)
{
die ("error uploading file $error");
}
else
{
move_uploaded_file($temp,$store);
echo "upload successfullly!!";
echo "name of file: ",$name,"<br>";
echo "size of file: ",$size,"<br>";
echo "type of file: ",$type,"<br>";
25 | P a g
e
Output:
4 Use the md5 function to encrypt the password entered by the user in a form which submits the
user name and the password of the user and stores the encrypted password along with the
username in a database named data1.
Answer:
<html>
<head>
<title>md5 function</title>
</head>
<body>
<form action="md5.php" method='POST'>
Username<input type='text' name='username'><br><br>
Password<input type='password' name='password'><br><br>
<input type='submit' name='submit' value='submit'>
</form>
26 | P a g
e
</body>
</html>
<?php
$server='localhost';
$user ='root';
$pass='';
$db='data1';
$conn=new mysqli($server,$user,$pass,$db);
if($conn->connect_error)
die('connection failed!!');
if(isset($_POST['submit']))
{
$username=$_POST['username'];
$password=md5($_POST['password']);
$sql="insert into user(username,password)values('$username','$password')";
if($conn->query($sql)==true){
echo 'record saved';
}
else{
echo "error",$sql,"<br>",$conn->query($sql);
}
}
$conn->close();
?>
output:
27 | P a g
e
5 Create a Feedback form which is used to take the feedback along with the following
Information: Name and Email ID. After the successful submission of the form by the user,
a mail is sent to the user thanking him for his feedback and another mail is to be sent to
the administrator consisting of the information provided by the user ie. Name, Email and
Feedback
Answer:
<html>
<form action="feedbackform.php" method="POST">
Name<br><input type="text" name="name"><br>
Email<br><input type="text" name="email"><br>
college name<br><input type="Text" name="college"><br>
rating<br><input type="Number" name="rating"><br><br>
<input type="submit" name="submit">
</form>
</html>
<?php
$name=$_POST["name"];
$email=$_POST["email"];
$collegename=$_POST["college"];
$rating=$_POST["rating"];
$sub="Rating feedback. ... ";
$msg="Thanks for your feedback";
$header="[email protected] ";
$ato="[email protected] ";
$asub="users feedbak. . ";
$amsg="Name:".$name."\n"."Email:".$email."\n"."College
Name:".$collegename."\n"."Rating:".$rating."\n";
$aheader="From:[email protected]";
if(mail($email,$sub,$msg,$header)&& mail($ato,$asub,$amsg,$aheader))
{
echo "mail successfuly sent to administrator"."<br>";
echo "mail successfuly sent to ".$email."<br>";
}
else
{
echo "mail not sended. .. !"."<br>";
28 | P a g
e
}?>
Output:
29 | P a g
e