0% found this document useful (0 votes)
91 views37 pages

Practical - 3 1.: I. Write A PHP Program To Display Today's Date in Dd-Mm-Yyyy Format. Code

The document contains 5 practical assignments in PHP with solutions. The assignments include: 1) Displaying today's date, adding two numbers, and determining if a number is odd or even. 2) Checking if a year is a leap year, printing the Fibonacci series, and determining if a number is prime. 3) Checking if a number is a palindrome. 4) Printing patterns using loops. 5) Demonstrating various string functions in PHP like strtolower(), strlen(), trim(), substr(), strcmp(), etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views37 pages

Practical - 3 1.: I. Write A PHP Program To Display Today's Date in Dd-Mm-Yyyy Format. Code

The document contains 5 practical assignments in PHP with solutions. The assignments include: 1) Displaying today's date, adding two numbers, and determining if a number is odd or even. 2) Checking if a year is a leap year, printing the Fibonacci series, and determining if a number is prime. 3) Checking if a number is a palindrome. 4) Printing patterns using loops. 5) Demonstrating various string functions in PHP like strtolower(), strlen(), trim(), substr(), strcmp(), etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Name: Tirth Bhatt A1 190280107010

Practical – 3
1.
I. Write a PHP program to display today’s date in dd-mm-yyyy format.
Code:
<?php
echo("<h1> Current Date is :".date("d-m-y")."</h1>");
?>

Output:

II. Write a PHP script for Addition of two numbers.


Code:
<html>

<body>
<form>
<div>Number 1:</div><input type="number" name="num1" />
<div>Number 2:</div><input type="number" name="num2" />
<div><br><input type="submit" value="CHECK THE SUM"></div><br>
</form>
<?php if (isset($_GET['num1']) && isset($_GET['num2'])) {
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$sum = $num1 + $num2;
echo $num1 . " + " . $num2 . " = " . $sum;

Page | 1
Name: Tirth Bhatt A1 190280107010

} ?>
</body>

</html>

Output:

III. Create a PHP program to find odd or even number from given number.
Code:
<html>

<body>
<form method="post">
Number:<input type="number" name="number">
<input type="submit" value="submit"></form>
</body>

</html>

Page | 2
Name: Tirth Bhatt A1 190280107010

<?php
if ($_POST) {
$number = $_POST["number"];
if (($number % 2) == 0) {
echo "$number is an Even number";
} else {
echo "$number is Odd number";
}
} ?>

Output:

IV. Write a PHP program to find maximum of three numbers


Code:
<html>
<body>
<form method="post">
<table>
<tr>
<td>
Enter the first number:
</td>
<td>

Page | 3
Name: Tirth Bhatt A1 190280107010

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


</td>
</tr>
<tr>
<td>Enter the second number: </td>
<td><input type="text"name="num2"></td>
</tr>
<tr>
<td>Enter the third number: </td>
<td><input type="text"name="num3"> </td>
</tr>
</table>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST["submit"]))
{
$num1=$_POST["num1"];
$num2=$_POST["num2"];
$num3=$_POST["num3"];
if($num1>$num2 && $num1>$num3)
{
echo "$num1 is maximum";
}
else{

if($num2>$num1 &&$num2>$num3)

Page | 4
Name: Tirth Bhatt A1 190280107010

{
echo "$num2 is maximum";
}else
echo "$num3 is maximum";
}
}?>
</body>
</html>

Output:

V. Write a PHP program to print following pattern.

Program:
a)
<?php
$n = 1;
$char = "a";

Page | 5
Name: Tirth Bhatt A1 190280107010

for ($i = 0; $i < 5; $i++) {


for ($j = 0; $j <= $i; $j++) {
if ($i % 2 == 0) {
echo $n++ . " ";
} else {
echo $char++ . " ";
}
}
echo "<br/>";
}
?>
Output:

b)
<?php
for ($i = 0; $i < 5; $i++) {
for ($j = 0; $j <= $i; $j++) {
if (($i + $j) % 2 == 1) {
echo "0";
} else {
echo "1";
}
}

Page | 6
Name: Tirth Bhatt A1 190280107010

echo "<br/>";
}
?>
Output:

2.
I. Write a PHP script to check whether given year is leap year or not.
<!DOCTYPE html>
<html lang="en">

<head>
<title>Practical2_1</title>
</head>

<body>
<h2>PHPScript to find Leap year or not </h2>
<form action="" method="post">
<input type="text" name="year" />
<input type="submit" value="Submit" />
</form>

<?php
if ($_POST) {
$year = $_POST['year'];

Page | 7
Name: Tirth Bhatt A1 190280107010

if (($year % 4 == 0) and ($year % 100 != 0) or ($year % 400 == 0)) {


echo "<br>$year is a leap year";
} else {
echo "<br>$year is not a leap year";
}
}
?>

</body>
</html>

Output:

II. Write a PHP script to print Fibonacci series.

Program:

<!DOCTYPE html>
<html lang="en">

<head>
<title>Document</title>
</head>

Page | 8
Name: Tirth Bhatt A1 190280107010

<body>
<h2>PHP Script to print FibonacciSeries</h2>
<form action="" method="post">
<input type="text" name="number" />
<input type="submit" value="Submit" />
</form>
<?php
$number = $_POST['number'];
if (isset($number)) {
$count = 0;
$f1 = 0;
$f2 = 1;
echo "<h3>First $number Fibonacci numbers are: <br></h3>";
echo $f1 . " ,";
echo $f2;
while ($count < ($number - 2)) {
$f3 = $f2 + $f1;
echo " , " . $f3;
$f1 = $f2;
$f2 = $f3;
$count = $count + 1;
}
}
?>

</body>

Page | 9
Name: Tirth Bhatt A1 190280107010

</html>

Output:

III. Write a PHP script to check the given number is prime or not.

Program:

<!DOCTYPE html>
<html lang="en">

<head>
</head>

<body>
<form method="post">
<h3>Enter Your Number:</h3>
<input type="text" name="number">
<input type="submit" name="submit" value="submit">
</form>
<?php
if (isset($_POST['submit'])) {

Page | 10
Name: Tirth Bhatt A1 190280107010

$num = $_POST['number'];
$Flag = 1;
for ($i = 2; $i < $num; $i++) {
if (($num % $i) == 0) {
$Flag = 0;
break;
}
}
}
if ($Flag == 1) {
echo "$num is Prime Number";
} else {
echo "$num is not Prime Number";
}
?>

</body>
</html>

Output:

IV. Write a PHP script to Check the given number is Palindrome or Not.

Program:

Page | 11
Name: Tirth Bhatt A1 190280107010

<html>
<head>
</head>

<body>
<form method="POST">
Enter Number: <input type="text" name="number"><br>
<input type="submit" value="Check Number" name="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$n = $_POST['number'];
$temp = $n;
$new = 0;
while (floor($temp)) {
$d = $temp % 10;
$new = $new * 10 + $d;
$temp = $temp / 10;
}
if ($new == $n) {
echo "$n is Palindrome";
} else {
echo "$n is Not a Palindrome";
}
}
?>

Page | 12
Name: Tirth Bhatt A1 190280107010

</body>
</html>

Output:

V. Write PHP script to demonstrate use of string function. strtolower (),


strtoupper(),strlen(), ltrim(), rtrim(), trim(), substr(), strcmp(), strcasecmp(),
strpos(), str_replace(), strrev()

Program:

<?php
$str="Hello World";
echo "Input string : $str";

echo "<h3> strtolower() function </h3>";


$ans=strtolower($str); echo "Output :$ans";

echo "<h3> strtoupper() function </h3>";


$ans=strtoupper($str); echo "Output :$ans";

echo "<h3>strlen() function </h3>";


$ans=strlen($str); echo "Output :$ans";

echo "<h3>trim(), ltrim() and rtrim() function</h3>";


Page | 13
Name: Tirth Bhatt A1 190280107010

$str1=" programming ";


$str2="\r\nPHP is a popular programming language\r\n";
echo "Input String = $str1<br>";
echo "Output of trim() without optional value: <b>". trim($str1)."</b><br/>";
echo "Input String = $str2<br>";
echo "Output of trim() with optional value: <b>". trim($str2, "\r\n")."</b><br/><br/>";

$Str1=" PHP is a popularlanguage ";


$Str2="JavaScript is client-side scripting language5";
echo "Output of ltrim() without optional value: <b>". ltrim( $Str1
)."</b><br/>";
echo "Output of ltrim() with optional value: <b>". ltrim( $Str2, "0..9"
)."</b><br/><br/>";
echo "Output of rtrim() without optional value: <b>". rtrim( $Str1
)."</b><br/>";
echo "Output of rtrim() with optional value: <b>". rtrim( $Str2, "0..9" )."</b><br/>";

echo "<h3> substr() function </h3>";


$ans=substr($str, 2, 7);
echo "Output of substr($str, 2, 7) : $ans"; echo "<h3> strcmp() function</h3>";

$str1 = "HELLO";
$str2 = "hello";
if (strcmp($str1, $str2) == 0)
{
echo "$str1 is equal to $str2";
}

Page | 14
Name: Tirth Bhatt A1 190280107010

else {
echo "$str1 is not equal to $str2";
}

echo "<h3> strcasecmp() function </h3>";


$str1 = "Google";
$str2 = "Google";
if (strcasecmp($str1, $str2) == 0) { echo "$str1 is equal to $str2";
}
else {
echo "$str1 is not equal to $str2";
}

echo "<h3> strpos() function </h3>";


$str="PHP is Most Popular Language";
$findme="Most";
$pos = strpos($str, $findme);
if ($pos === false) {
echo "The string $findme was not found in the string $str";
} else{
echo "The string $findme was found in the string $str"; echo " and exists at position
$pos";
}

echo "<h3> str_replace() function </h3>";


$str="PHP is Most Popular Language";
echo "Input string : $str <br/>";

Page | 15
Name: Tirth Bhatt A1 190280107010

$output_str=str_replace("PHP","JavaScript",$str); echo "Output string : $output_str";

echo "<h3> strrev() function </h3>";


$str="Hello World";
echo "Input string : $str <br/>";
$output_str=strrev($str);
echo "Output string : $output_str";

?>
Output:

Page | 16
Name: Tirth Bhatt A1 190280107010

VI. Write PHP script to demonstrate use of Math functions. abs () &
round (), ceil () &floor(), min () & max (), pow () &sqrt ()
Program:

<?php
echo "<h3> abs() function </h3>";
$num=-20;
echo "Your number is :$num".'<br>';
echo "Using abs function your number is : ".abs($num);

echo "<h3> round() function </h3>";


$num=7.45;
echo "Your number is :$num".'<br>';

Page | 17
Name: Tirth Bhatt A1 190280107010

echo "Using round function your number is : ".round($num);

echo "<h3>floor() function </h3>";


$num=10.89;
echo "Your number is :$num".'<br>';
echo "Using floor function your number is : ".floor ($num).'<br>';

echo "<h3>ceil() function </h3>";


$num=10.89;
echo "Your number is :$num".'<br>';
echo "Using ceil function your number is : ".ceil ($num).'<br>';

echo"<h3>min() and max()function</h3>";


$num=array(15,20,7,3,25);
echo "Your number are :" ."".
$num[0].",".$num[1].",".$num[2].",".$num[3].",".$num[4].'<br>';
echo"Maximumnumber is : ".max($num).'<br>';
echo "Minimum number is : ".min ($num);

echo "<h3> pow() function</h3>";


$a=2;
$b=3;
echo "a :$a".'<br>';
echo "b :$b".'<br>';
echo "Using pow function a^b is : ".pow($a,$b).'<br>';

Page | 18
Name: Tirth Bhatt A1 190280107010

echo "<h3>sqrt() function </h3>";


$num=36;
echo "Your number is :$num".'<br>';
echo "Using sqrt function your number is : ".sqrt ($num);
?>

Output:

VII. Write PHP script to demonstrate use of date/time functions. date (), getdate(),
setdate(), time(), checkdate()

Program:

Page | 19
Name: Tirth Bhatt A1 190280107010

<?php
echo "<h3> date() function </h3>";
echo "Using date function :".date("d/m/Y");

echo "<h3> getdate() function </h3>";


$today=getdate();
echo "$today[weekday], $today[month] $today[mday], $today[year]";

echo "<h3> setDate() function </h3>";


$datetime = new DateTime();
$datetime->setDate(2001,07,27);
echo $datetime->format('Y-m-d');

echo "<h3> time() function </h3>";


$t=time();
echo($t . "<br>");
echo(date("Y-m-d",$t));

echo "<h3> checkdate() function </h3>";


echo"Using checkdate function(2,29,2022)is : ";
var_dump(checkdate(2,29,2003));
echo '<br>'."Using checkdate function(1,29,2022)is : ";
var_dump(checkdate(1,29,2003));

?>
Output:

Page | 20
Name: Tirth Bhatt A1 190280107010

VIII. Write a PHP program to check whether the given string is palindrome or
not.
Program:

<html>
<head>
<title>Practical2_8</title>
</head>

<body>
<h3>PHP Script to Check Whether the String is palindrome or not</h3>
<form action="" method="post">

Page | 21
Name: Tirth Bhatt A1 190280107010

<input type="text" name="myString" />


<input type="submit" value="Submit" />
</form>
<?php
if (isset($_POST['myString'])) {
$str = $_POST['myString'];
$flag = false;
for ($i = 0; $i < strlen($str); $i++) {
if ($str[$i] != $str[strlen($str) - 1 - $i]) {
$flag = true;
break;
}
}
if ($flag) {
echo ("String is not Palindrome<br/>");
} else {
echo ("String is Palindrome<br/>");
}
}
?>
</body>

</html>

Output:

Page | 22
Name: Tirth Bhatt A1 190280107010

IX. Write a PHP program to find number of vowels in a given string.

Program:

<html>
<head>
<title>Practical2_9</title>
</head>

<body>
<h2>PHP Script to Check number of vowels in aString</h2>
<form action="" method="post">
<input type="text" name="myString" />
<input type="submit" value="Submit"/>
</form>
<?php if (isset($_POST['myString'])) {
$string = strtolower($_POST['myString']);

$vowels = array('a', 'e', 'i', 'o', 'u');


$len = strlen($string);
$num = 0;
for ($i = 0; $i < $len; $i++) {
if (in_array($string[$i], $vowels)) {
$num++;
}
}
echo "Number of vowels : <span style='color:green; font- weight:bold;'>" . $num .
"</span>";
}
?>
</body>

</html>

Output:

Page | 23
Name: Tirth Bhatt A1 190280107010

3
I. Write an HTML page that has one input, which can take multi-line
text and a submit button. Once the user clicks the submit button, it
should show the number of characters, lines and words in the text
entered using an alert message. Words are separated with whitespace
and lines are separated with new line character.

Program:

<html>
<head>
<script>
function countWords(str) {
var count = 0;
words = str.split(" ");
for (i = 0; i < words.length; i++) {
// inner loop -- do the count if (words[i] !="")
count += 1;
}
theForm.displaycountwords.value = count;
}

function countLines() {
var area = document.getElementById("texta");

Page | 24
Name: Tirth Bhatt A1 190280107010

// trim trailing return char if exists


var text = area.value.replace(/\s+$/g,"");
var split =text.split("\n");
theForm.displaylinecount.value = split.length;
}

function countit(what) {
formcontent = what.form.inStr.value;
what.form.displaycount.value = formcontent.length;
}
</script>
</head>

<body>
<h3>
<font>COUNT TOTAL NUMBER OF WORDS CHARACTERS
AND LINES</font>
</h3>
<form name="theForm"> Enter a text string:
<textarea name="inStr" id="texta" rows=5 cols=15>
</textarea>

<br>
<table>
<tr>
<td width="100%">
<div align="right">

Page | 25
Name: Tirth Bhatt A1 190280107010

<p><input type="button" value="Calculate Words"


onClick="countWords(document.theForm.inStr.value)">
<input type="text" name="displaycountwords" size="20">
</p>
</div>
</td>
</tr>
<tr>
<td width="100%">
<div align="right">
<p><input type="button" value="Calculate Characters"
onClick="countit(this)">
<input type="text" name="displaycount" size="20">
</p>
</div>
</td>
</tr>

<tr>
<td width="100%">
<div align="right">
<p><input type="button" value="Count Lines" onClick="countLines()">
<input type="text" name="displaylinecount" size="20">
</p>
</div>
</td>
</tr>
<tr>
Page | 26
Name: Tirth Bhatt A1 190280107010

<td colspan="2" align="center">


<input type=button name="theButton" value="ClearResults"
onClick="document.theForm.reset()">
</td>
</tr>
</table>
<br>
</form>
</body>

</html>

Output:

II. Write PHP Script to demonstrate use of Indexed/Numeric arrays and for FOR
EACH loop execution.

Program:
Page | 27
Name: Tirth Bhatt A1 190280107010

<?php
echo "<h3> Indexed array </h3>";
$color = array("one", "Two", "Three", "Four");
foreach ($color as $val) {
echo $val . "<br/>";
}

Output:

III. Write PHP Script to demonstrate use of associative arrays and for FOR EACH
loop execution.

Program:

<?php
echo "<h3> Associative array </h3>";
$num=array("1"=>"One", "2"=>"Two","3"=>"Three","4"=>"Four");
foreach ($num as $key=>$val)
{
echo $key."--".$val."<br/>";
}
?>

Page | 28
Name: Tirth Bhatt A1 190280107010

Output:

IV. Create HTML page that contain textbox, submit / reset button. Write a PHP
program to display this information and also store into text file.

Program:

<!DOCTYPE html>
<html lang="en">

<head>
</head>

<body>
<form name="myForm" method="post"> Enter Your Text Here:<br>
<input type="text" name="textdata">
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST['textdata'])) {
$data = $_POST['textdata'];
$fp = fopen('Nikhil.txt', 'w');

Page | 29
Name: Tirth Bhatt A1 190280107010

fwrite($fp, $data);
fclose($fp);
}
?>

</body>
</html>

Output:

V. Write a PHP program that demonstrate passing variable using URL.


Code:

Main File:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>

<body>
<?php
$fname = "PHP";
$lname = "Programming";
?>

<a href="prac_3_3_5_a.php?First_name=<?php echo $fname;?>

Page | 30
Name: Tirth Bhatt A1 190280107010

&last_name=<?php echo $lname; ?>"> Click here to Pass Variable


through URL
</a>
</body>

</html>

Secondary File:
<?php
echo"FirstName : ".$_GET['First_name']."<br>";
echo "Last Name : ".$_GET['last_name'];
?>

Output:

VI. Create a Student Registration form using different HTML form elements
and display all student information on another page using PHP.

Program:

Main php file:


<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>

Page | 31
Name: Tirth Bhatt A1 190280107010

<body>
<form method="post" action="prac3_6display.php">
<h2> Student Form: </h2>
<label for='name'>Enter your name: </label>
<input type='text' name='name' /><br><br>
<label for='no'>Enter your enrollment no.: </label>
<input type='number' name='no' /><br><br>
<label for='college'>Enter your college: </label>
<input type='text' name='college' /><br><br>
<input type='submit' value='Submit' name='submit' />
</form>
</body>
</html>

Display php file:

<?php
if (isset($_POST['submit'])) {
$sn = $_POST['name'];
$en = $_POST['no'];
$inst = $_POST['college'];
echo "Student Name : $sn<br>";
echo "EnrollmentNo. : $en<br>";
echo "Institute : $inst<br>";
}

Page | 32
Name: Tirth Bhatt A1 190280107010

?>

Output:

VII. Write a PHP script to make a web application takes a name as input and on
submit it shows a hello<name> page where <name> is taken from the request. And
it shows a start time at the right top corner of the page and provides the logout
button on clicking this button it should shoe a logout page with thankyou<name>
message with the duration of Usage.

Code:
Main File:
<?php
session_start();
date_default_timezone_set("Asia/Calcutta");
$_SESSION['luser'] = $_POST['name'];
$_SESSION['start'] = time();
$tm=$_SESSION['start'];
print "<p>Session started at " . date("h:i:sa",$tm) . "<br>";
Page | 33
Name: Tirth Bhatt A1 190280107010

print "<form action='prac_3_3_7_c.php' method='post'>";


print "<input type='submit' value='Logout'></p>";
print "</form>";
print "Hello " . $_SESSION['luser'];
?>

Login File:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>

<body>
<form action='prac_3_3_7_a.php' method='post'>
<label for='name'>Enter username: </label>
<input type='text' name='name'>
<br>
<input type='submit' value='Signin' name='submit'>
</form>
</body>

</html>

Logout:
<?php
session_start();
date_default_timezone_set("Asia/Calcutta");
print "<p>Session started at " . date("h:i:sa",time()). "</p><br>";
print "Thank you " . $_SESSION['luser'];
$sessiontime = time() - $_SESSION['start'];
print "<br> Your session duration: " . $sessiontime . " seconds";
session_destroy();
?>
Output:

Page | 34
Name: Tirth Bhatt A1 190280107010

VIII. Write a program that demonstrate use of cookies.


Main file:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<?php
setcookie("name","JOHN", time() + 3600, "/", "", 0);
setcookie("id", "75", time() + 3600, "/", "", 0);
echo "The cookies created for name and id.";
?>
<br>
<a href="prac_3_3_8_a.php">Show Cookies</a>
</body>
</html>

Cookie Set:
Page | 35
Name: Tirth Bhatt A1 190280107010

<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<?php
if(isset($_COOKIE["name"]) &&isset($_COOKIE["id"]))
{
echo " The name = " .$_COOKIE["name"]. "<br/>";
echo "The id = ". $_COOKIE["id"];
}
else
{
echo "Sorry !! cookies is not set.";
}
?>
<br>
<a href="prac_3_3_8_c.php">Delete Cookies</a>

</body>
</html>
Cookie unset:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>

Page | 36
Name: Tirth Bhatt A1 190280107010

</head>
<body>
<?php
setcookie("name","", time() - 3600, "/", "", 0);
setcookie("id", "", time() - 3600, "/", "", 0);
echo "The cookies Deleted for name and id.";
?>

</body>
</html>

Output:

Page | 37

You might also like