0% found this document useful (0 votes)
51 views17 pages

Seema

This document contains a lab manual for PHP and MySQL practical assignments. It includes 14 practical assignments on PHP programming concepts like variables, loops, functions, and arrays. Each practical has the aim, PHP code with HTML formatting, and expected output. The practicals cover basic PHP elements like variables, loops, conditional statements, functions and arrays to more advanced topics like passing arguments to functions. The document is intended to provide guidance to students on completing PHP programming assignments.

Uploaded by

Seema Akbar
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)
51 views17 pages

Seema

This document contains a lab manual for PHP and MySQL practical assignments. It includes 14 practical assignments on PHP programming concepts like variables, loops, functions, and arrays. Each practical has the aim, PHP code with HTML formatting, and expected output. The practicals cover basic PHP elements like variables, loops, conditional statements, functions and arrays to more advanced topics like passing arguments to functions. The document is intended to provide guidance to students on completing PHP programming assignments.

Uploaded by

Seema Akbar
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
You are on page 1/ 17

III B.

Sc Semester -V PHP and MYSQL (Lab Manual)


Practical-1
Aim: - Write a PHP Program on Variables.
<html>
<body>
<?php
$a = 25; // Numerical variable
$b = "Hi Students this is seshu from TJPS College";
$c = 5.7; // Float variable
echo "<b>Number is : </b>".$a;
echo "</br>";
echo "<b>String is : </b>".$b;
echo "</br>";
echo "<b>Float value : </b>".$c;
?>
</body>
</html>

Output:

Practical-2
Aim: - Write a PHP Program on Local and Global Variables.

<html>
<body>
<?php
$x=44; // global scope
function sampledemo()
{
global $x;
$y=59; // local scope
echo "Local Variable y is: $y <br>";
echo "Global Variable x is: $x";
}
sampledemo();// Function call
?>
</body>
</html>

Output:

Department of Computer Science, TJPS College, Guntur. 1


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
Practical - 3
Aim: Write a PHP program on static variables.
<html>
<head> <title> Sample Demo program on static variables</title>
</head>
<body>
<?php

function staticdemo()
{
$a=10;
static $b=20;

$a=$a+1;

$b=$b+1;

echo "the non static varible is:".$a;


echo "</br>";
echo "the static varible is:".$b;
echo "</br>";
}

staticdemo();
staticdemo();
staticdemo();
staticdemo();
?>
</body>
</html>

Output:

Practical - 4
Aim: Write a PHP program to find odd or even number from given number.
<Html>
<Head> <Title> Find out odd or even number. . ! </title>
</Head >
<body>

Department of Computer Science, TJPS College, Guntur. 2


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
<?php
$a = 10;
if ($a % 2==0)
{
echo "Given number is" . "<br>" . "<b>EVEN<b>" ;
}
else
{
echo "Given number is" . "<br>" . "<b>ODD<b>";
}
?>
</body>
</html>
Output:

Practical - 5
Aim: Write a PHP program to find maximum of three numbers using else if.
<html>
<head>
<title> Max of three numbers </title>
</head>
<body>
<?php
// Demo on if else
$a=10;
$b=20;
$c=30;
if($a>$b && $a>$c)
echo "A is Biggest Number";
else if($b>$a && $b>$c)
echo "B is Biggest Number";
else
echo "C is Biggest Number";
?>
</body>
</html>
Output:

Department of Computer Science, TJPS College, Guntur. 3


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
Practical - 6
Aim: Write a PHP program to find addition of two numbers using nested if.
<html>
<body>
<?php
$a=10;
if($a>0)
{
$b=$a+20;
echo "the result is:<br>".$b;
if($b>10)
{
$c=$a+$b;
echo "<br>the result of nested if is:<br>".$c;

}
}
?>
</body>
</html>

Output:

Practical - 7
Aim: Write a PHP program to perform arithmetic operations using switch case.

<html>
<body>
<?php
$a=10;
$b=20;
$choice=1;
switch($choice)
{
case 1:
$c=$a+$b;
echo "the addition is:".$c;
break;

case 2:
$c=$a-$b;
echo "the subtraction is:".$c;
break;
case 3:
$c=$a*$b;

Department of Computer Science, TJPS College, Guntur. 4


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
echo "the multiplication is:".$c;
break;
case 4:
$c=$a/$b;
echo "the division is:".$c;
break;
case 5:
$c=$a%$b;
echo "the modulo division is:".$c;
break;
default: echo "the given value is not in range";
}
?>
</body>
</html>

Output:

Practical - 8
Aim: Write a PHP program to swap two numbers.
<html> <head>
<title> Swapping of two numbers. . ! </title>
</head>
<body>
<?php
$a = 10;
$b = 20;
echo "a = $a"."<br>"."b = $b"."<br>";
$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
echo "<b>After Swapping"."<br>"." a = $a"."<br>"."b = $b<b>";
?>
</body>
</html>
Output:

Practical - 9
Aim: Write a PHP Program to display the Fibonacci series using while loop.
<html>
<body>
<?php
$a=0;

Department of Computer Science, TJPS College, Guntur. 5


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
$b=1;
$c=$a+$b;
$n=10;
echo "<b>The Fibonacci series is:<br>";
echo "$a\t";
echo "$b\t";
echo "$c\t";
while($c<=$n)
{
$a=$b;
$b=$c;
$c=$a+$b;
echo "$c\t";
}

?>
</body>
</html>
Output:

Practical - 10
Aim: Write a PHP Program to display the sum of “N” natural numbers using do while loop.
<html><body>
<?php
$n=10;
$sum=0;
$i=1;
while($i<=$n)
{
$sum=$sum+$i;
$i++;
}
echo "<b>The sum of $n Natural Numbers are:".$sum;
?>
</body></html>
Output:

Department of Computer Science, TJPS College, Guntur. 6


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
Practical - 11
Aim: Write a PHP Program to display the to display the Mathematical Table for the given
number using for loop.
<html>
<body>
<?php
$n=10;
$i=1;
$range=10;
echo "<b><br>The mathematical table is:<br>";
for($i=1;$i<=$range;$i++)
{
$n1=$n*$i;
echo "$n"."*"."$i"."="."$n1"."<br>";
}
?>
</body>
</html>

Output:

Practical - 12
Aim: Write a PHP Program to display the text using Function.
<html>
<head><title> Deno on Functions in PHP</title></head>
<body> <?php
function DisplayText()
{
echo "<b>Hello this is Seshu from TJPS College";
}
DisplayText(); //calling function
?>
</body>
</html>
Output:

Department of Computer Science, TJPS College, Guntur. 7


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
Practical - 13
Aim: Write a PHP Program to pass Single argument in Function.
<html>
<head><title> Demo on Functions in PHP</title>
</head>
<body>
<?php
function Argument($name)
{
echo "<b>Hello $name<br/>";
}
Argument ("Murali Sir");
Argument ("Markandeyulu Sir");
Argument ("Seshu Sir");
?>
</body>
</html>

Output:

Practical - 14
Aim: Write a PHP Program to pass Multiple Arguments in Function.
<html>
<head><title> Demo on Functions in PHP</title>
</head>
<body>
<?php
function MultipleArguments($name,$age)
{
echo "Hello $name, you are $age years old<br/>";
}
MultipleArguments ("Murali Sir",50);
MultipleArguments ("Mark Sir",45);
MultipleArguments ("Seshu",35);
?>
</body>
</html>
Output:

Department of Computer Science, TJPS College, Guntur. 8


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
Practical - 15
Aim: Write a PHP Program on Arrays.
<html>
<body>
<?php
echo "<b>NUMERIC ARRAY</b><br>" ;
/* First method to create array. */
echo "<b>First method to create array</b><br>" ;
$numbers = array( 1, 2, 3, 4, 5);
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
/* Second method to create array. */
echo "<br><b>Second method to create array</b><br>" ;
$numbers[0] = "One";
$numbers[1] = "Two";
$numbers[2] = "Three";
$numbers[3] = "Four";
$numbers[4] = "Five";
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
echo "<BR><b>ASSOCIATIVE ARRAY</b><br>" ;
echo "<b>First method to create array</b><br>" ;
$salaries = array("SESHU" => 2000, "MARK" => 1000, "TRIVENI" => 500);
echo "Salary of SESHU is ". $salaries['SESHU'] . "<br />";
echo "Salary of MARK is ". $salaries['MARK']. "<br />";
echo "Salary of TRIVENI is ". $salaries['TRIVENI']. "<br />";
echo "<br><b>Second method to create array</b><br>" ;
$salaries['SESHU'] = "high";
$salaries['MARK'] = "medium";
$salaries['TRIVENI'] = "low";
echo "Salary of SESHU is ". $salaries['SESHU'] . "<br />";
echo "Salary of MARK is ". $salaries['MARK']. "<br />";
echo "Salary of TRIVENI is ". $salaries['TRIVENI']. "<br />";
echo "<BR><b>MULTIDIMENTIONAL ARRAY</b><br>" ;
$emp = array
(
array(1,"SESHU",400000),
array(2,"SAI",500000),
array(3,"MOHAN",300000)
);
for ($row = 0; $row < 3; $row++) {
for ($col = 0; $col < 3; $col++) {
echo $emp[$row][$col]." ";
}
echo "<br/>";
}
?>
</body></html>

Department of Computer Science, TJPS College, Guntur. 9


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
Output:

Practical - 16
Aim: Write a PHP Program to Perform String Operations.
<html><body>
<?php
$str='Hello we are from TJPS College Students';
echo $str;
$str="<br>Hello we are from TJPS College Students ";
echo $str;
echo "<br>";
echo "The length of the string is:";
echo $len=strlen("Hello this is Seshu");
echo "<br>";
echo "The length of the word is:";
echo str_word_count("Hello this is Seshu");
echo "<br>";
echo "The reverse of the string is:";
echo strrev("Hello world");
echo "<br>";
echo "The position of the string is:";
echo strpos("Hello world!", "world");
echo "<br>";
echo "The replace of the string is:";
echo str_replace("world","Seshu","Hello world!");
echo "<br>";
$str="Hello friends i am SESHU";
$str=strtolower($str);
echo "$str";
echo "<br>";
$str="Hello friends i am Mohan";
$str=strtoupper($str);
echo "$str";
?></body></html>

Department of Computer Science, TJPS College, Guntur. 10


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
Output:

Practical - 17
Aim: Write a PHP Program to Create Student Registration Form.

<center><h1> ***** <u>Student Registration Form</u> ***** </h1></center>


<b>Name of the Student: <input type="text" name="name" value=""><br><br>
<b>Father Name: <input type="text" name="email" value=""><br><br>
<b>Course: <input type="text" name="website" value=""><br><br>
<b>Email: <input type="text" name="website" value=""><br><br>
<b>Phone Number: <input type="text" name="website" value=""><br><br>
<b>Address: <br><textarea name="comment" rows="5"
cols="40"></textarea><br>

<b>Gender:
<input type="radio" name="Gender:"
<?php if (isset($gender) && $gender=="female") echo "checked";?>
value="female">Female
<input type="radio" name="gender"
<?php if (isset($gender) && $gender=="male") echo "checked";?>
value="male">Male
<input type="radio" name="gender"
<?php if (isset($gender) && $gender=="other") echo "checked";?>
value="other">Others<br><br><br>

<center>
<a href="submission.html"><input type="button" name="Ok"
value="Submit"></a>
<input type="button" name="Ok" value="Cancel">

Department of Computer Science, TJPS College, Guntur. 11


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
Output 1:

Output 2:

Practical - 18
Aim: Write a PHP Program to Create Employee Registration Form.
<center><h1> @@@@ Employee Registration Form @@@@ </h1></center>
<b>Name of the Employee: <input type="text" name="name" value=""><br><br>
<b>Father Name: <input type="text" name="email" value=""><br><br>
<b>Gender:
<input type="radio" name="Gender:"
<?php if (isset($gender) && $gender=="female") echo "checked";?>
value="female">Female
<input type="radio" name="gender"
<?php if (isset($gender) && $gender=="male") echo "checked";?>
value="male">Male
<input type="radio" name="gender"
<?php if (isset($gender) && $gender=="other") echo "checked";?>
value="other">Others<br><br>
<b>Date of Join: <input type="text" name="website" value=""><br><br>
<b>Date of Birth: <input type="text" name="website" value=""><br><br>
<b>Designation: <input type="text" name="website" value=""><br><br>
<b>Email: <input type="text" name="website" value=""><br><br>
<b>Phone Number: <input type="text" name="website" value=""><br><br>

Department of Computer Science, TJPS College, Guntur. 12


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
<b>Department: <input type="text" name="website" value=""><br><br>
<b>Salary: <input type="text" name="website" value=""><br><br>

<b>Address: <br><textarea name="comment" rows="5"


cols="40"></textarea><br>

<center>
<a href="submission.html"><input type="button" name="Ok"
value="Submit"></a>
<input type="button" name="Ok" value="Cancel">

Output 1:

Output 2:

Practical - 19
Aim: Write a PHP Program on Form Validations.
<!DOCTYPE html>
<html>
<head>
<style>
.error {color: #FF0001;}
</style> </head> <body>

Department of Computer Science, TJPS College, Guntur. 13


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
<?php
// define variables to empty values
$nameErr = $emailErr = $mobilenoErr = $genderErr = $websiteErr = $agreeErr =
"";
$name = $email = $mobileno = $gender = $website = $agree = "";

//Input fields validation


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

//String Validation
if (emptyempty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = input_data($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only alphabets and white space are allowed";
}
}

//Email Validation
if (emptyempty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = input_data($_POST["email"]);
// check that the e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}

//Number Validation
if (emptyempty($_POST["mobileno"])) {
$mobilenoErr = "Mobile no is required";
} else {
$mobileno = input_data($_POST["mobileno"]);
// check if mobile no is well-formed
if (!preg_match ("/^[0-9]*$/", $mobileno) ) {
$mobilenoErr = "Only numeric value is allowed.";
}
//check mobile no length should not be less and greator than 10
if (strlen ($mobileno) != 10) {
$mobilenoErr = "Mobile no must contain 10 digits.";
}
}

//URL Validation
if (emptyempty($_POST["website"])) {
$website = "";
} else {

Department of Computer Science, TJPS College, Guntur. 14


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
$website = input_data($_POST["website"]);
// check if URL address syntax is valid
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-
9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}

//Empty Field Validation


if (emptyempty ($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = input_data($_POST["gender"]);
}

//Checkbox Validation
if (!isset($_POST['agree'])){
$agreeErr = "Accept terms of services before submit.";
} else {
$agree = input_data($_POST["agree"]);
}
}
function input_data($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<center><h1>***** Validations in PHP *****</h1> </center>
<span class = "error">* Required field </span>
<br><br>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]); ?>" >
Name:
<input type="text" name="name">
<span class="error">* <?php echo $nameErr; ?> </span>
<br><br>
E-mail:
<input type="text" name="email">
<span class="error">* <?php echo $emailErr; ?> </span>
<br><br>
Mobile No:
<input type="text" name="mobileno">
<span class="error">* <?php echo $mobilenoErr; ?> </span>
<br><br>
Website:
<input type="text" name="website">
<span class="error"><?php echo $websiteErr; ?> </span>
<br><br>

Department of Computer Science, TJPS College, Guntur. 15


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
Gender:
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
<input type="radio" name="gender" value="other"> Other
<span class="error">* <?php echo $genderErr; ?> </span>
<br><br>
Agree to Terms of Service:
<input type="checkbox" name="agree">
<span class="error">* <?php echo $agreeErr; ?> </span>
<br><br>
<input type="submit" name="submit" value="Submit">
<br><br>
</form>

<?php
if(isset($_POST['submit'])) {
if($nameErr == "" && $emailErr == "" && $mobilenoErr == "" && $genderErr ==
"" && $websiteErr == "" && $agreeErr == "") {
echo "<h3 color = #FF0001> <b>You have sucessfully registered.</b>
</h3>";
echo "<h2>Your Input:</h2>";
echo "Name: " .$name;
echo "<br>";
echo "Email: " .$email;
echo "<br>";
echo "Mobile No: " .$mobileno;
echo "<br>";
echo "Website: " .$website;
echo "<br>";
echo "Gender: " .$gender;
} else {
echo "<h3> <b>You didn't filled up the form correctly.</b> </h3>";
}
}
?>
</body> </html>
Output:

Department of Computer Science, TJPS College, Guntur. 16


III B. Sc Semester -V PHP and MYSQL (Lab Manual)
Practical - 20
Aim: Write a PHP Program on Image Creation.
<?php

// Create the size of image or blank image


$image = imagecreate(500, 300);

// Set the background color of image


$background_color = imagecolorallocate($image, 0, 153, 255);

// Set the text color of image


$text_color = imagecolorallocate($image, 255, 255, 200);

// Function to create image which contains string.


imagestring($image, 5, 180, 100, "TJPS COLLEGE", $text_color);
imagestring($image, 3, 160, 120, "Royal III MECs & MPCs", $text_color);

header("Content-Type: image/png");

imagepng($image);
imagedestroy($image);

?>

Output:

Department of Computer Science, TJPS College, Guntur. 17

You might also like