0% found this document useful (0 votes)
36 views

Practial 2

The document provides instructions for practical assignments related to applying in-built and user defined functions in PHP scripts. It lists 13 practical tasks to write PHP scripts demonstrating various variable functions, string handling functions, date/time functions, array functions, file handling functions and more. The aim is to practice using different PHP functions and understand their usage. Sample code is provided for two tasks - to demonstrate variable functions and type testing functions in PHP.

Uploaded by

Rajvi Mangroliya
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)
36 views

Practial 2

The document provides instructions for practical assignments related to applying in-built and user defined functions in PHP scripts. It lists 13 practical tasks to write PHP scripts demonstrating various variable functions, string handling functions, date/time functions, array functions, file handling functions and more. The aim is to practice using different PHP functions and understand their usage. Sample code is provided for two tasks - to demonstrate variable functions and type testing functions in PHP.

Uploaded by

Rajvi Mangroliya
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/ 43

I.T.

Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

CO-2 Apply In-Built and User defined functions in PHP scripts.

PRACTICAL LIST – 2

2.1 Write PHP script to demonstrate Variable function.


2.2 Write a PHP script to implement my_gettype () using various type testing functions
covered in class.
2.3 Write PHP script to demonstrate use of various string handling function.
2.4 Write a PHP script to demonstrate use of various str_replace ().
2.5 Write a PHP script to Demonstrate implode (), explode () to convert array into string and
string into array.
2.6 Write a PHP script to demonstrate use of date () with all format specifications.
2.7 Write a PHP script to Demonstrate getdate () and checkdate().
2.8 write a PHP script to Demonstrate time() and mktime()(use my birthdate).
2.9 Write PHP script to demonstrate all Math functions.
2.10 Write PHP script to demonstrate Array functions.
2.11 Write a PHP script to demonstrate sort (), asort (), ksort () on associative array.
2.12 Write PHP script to demonstrate use of fopen (), fread (), fwrite () and fclose () File
functions.
2.13 Write a PHP script to count no. of words in a file using file and string handling functions.

SEM-6 ER. No.:206120316021 Page | 30


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

Aim:
2.1 Write PHP script to demonstrate Variable function.
2.2 Write a PHP script to implement gettype() using various type testing functions covered in
class.
Software Required: Notepad, WordPad, Notepad++, Dreamweaver, Wamp/Xampp
Pre-requisite: Basic knowledge of editor
Theory/Logic:
 VARIABLE FUNCTION:
 It allows you to manipulate variables such as type of variable, set type, display value.
 Following table shows variable function with its syntax and description.

Name Syntax Description


gettype() gettype(variable It accepts variable as an argument and returns the
name); data type of that variable.
settype() settype(Variable It accepts a variable as an argument and set it to
name, Data type); specific data type passed as an argument.
isset() isset(Variable It accepts a variable as an argument and
Name); determines whether the variable exists and it is
assigned value or not.
unset() unset(Variable It accepts a variable as an argument and destroys
Name); that variable from memory.
strval() strval(variable It accepts variable as an argument and returns the
name); string value of that variable.
floatval() floatval(variable It accepts variable as an argument and returns the
name); Float value of that variable.
intval() intval(variable It accepts variable as an argument and returns the
name); Integer value of that variable.
print_r() print_r(variable It accepts variable as an argument and display it in
name); a human readable format.

Program/Solution:
P_2_1.php:
<!--P_2.1 Write PHP script to demonstrate Variable function. -->
<?php
echo "<h2>Variable Functions:</h2>";
$a = "Hello World!";
$b = 30.21;
$c = 12;

SEM-6 ER. No.:206120316021 Page | 31


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

//gettype()
echo "<h4>GETTYPE():</h4>";
echo "The $a Type is : ". gettype($a);
echo "<br>The $b Type is : ". gettype($b);
echo "<br>The $c Type is : ". gettype($c);

//settype()
echo "<h4>SETTYPE():</h4>";
echo "$a Type : ".gettype($a);
settype($a,"integer");
echo " is Set as : " . gettype($a);
echo "<br> $b Type : ".gettype($b);
settype($b,"string");
echo " is Set as : " . gettype($b);
echo "<br> $c Type : ".gettype($c);
settype($c,"double");
echo " is Set as : " . gettype($c);

//isset()
echo "<h4>ISSET():</h4>";
$i;
if(isset($i))
echo "Variable is assigned a value";
else
echo "Variable is not assigned a value";
if(isset($b))
echo "<br>Assigned a value to variable is $b";
else
echo "<br>Variable is not assigned a value";
if(isset($c))
echo "<br> Assigned a value to variable is $c";
else
echo "<br>Variable is not assigned a value";

//unset()
echo "<h4>UNSET():</h4>";
print 'The value of $a :'. $a;
unset($a);
print 'The value of $a :'. $a . "<br>";
print '<br>The value of $b :'. $b;
unset($b);
print 'The value of $b :'. $b . "<br>";
print '<br>The value of $c :'. $c;
unset($c);
print 'The value of $c :'. $c . "<br>";
$a = true;
$b = "World";
$c = 30.21;

//strval()
echo "<h4>STRVAL():</h4>";
print '$a = ';

SEM-6 ER. No.:206120316021 Page | 32


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

echo "$a Type : ".gettype($a);


$a = strval($a);
echo " is Set as : " . gettype($a);
print '<br>$b = ';
echo "$b Type : ".gettype($b);
$b = strval($b);
echo " is Set as : " . gettype($b);
print '<br> $c = ';
echo "$c Type : ".gettype($c);
$c = strval($c);
echo " is Set as : " . gettype($c);

//floatval()
echo "<h4>FLOATVAL():</h4>";
print '$a = ';
echo "$a Type : ".gettype($a);
$a = floatval($a);
echo " is Set as : " . gettype($a);
print '<br>$b = ';
echo "$b Type : ".gettype($b);
$b = floatval($b);
echo " is Set as : " . gettype($b);
print '<br> $c = ';
echo "$c Type : ".gettype($c);
$c = floatval($c);
echo " is Set as : " . gettype($c);

//intval()
echo "<h4>INTVAL():</h4>";
print '$a = ';
echo "$a Type : ".gettype($a);
$a = intval($a);
echo " is Set as : " . gettype($a);
print '<br>$b = ';
echo "$b Type : ".gettype($b);
$b = intval($b);
echo " is Set as : " . gettype($b);
print '<br> $c = ';
echo "$c Type : ".gettype($c);
$c = intval($c);
echo " is Set as : " . gettype($c);

//print_r()
echo "<h4>PRINT_R():</h4>";
$i=array("PHP","WNS","Advanced JAVA");
echo "The Subjects are : ";
print_r($i);
$j=array("Black","White","Red");
echo "<br>The Colors are : ";
print_r($j);
$k=array("BMW","Volvo");
echo "<br>The Cars are : ";

SEM-6 ER. No.:206120316021 Page | 33


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

print_r($k);
?>

Output:

SEM-6 ER. No.:206120316021 Page | 34


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

P_2_2.php:
<!-- P_2.2 Write a PHP script to implement my_gettype () using various type testing functions
covered in class. -->
<?php
echo "<h2>Testing Funtion:</h2>";
$a = 21;
$x=30.21;
$y = true;
$z = "Hello!";
$i=(is_bool($y));
if($i == 1)
$i = "True";
else $i = "False";
echo "is_bool() testing function on $y : $i <br>";
$i=(is_numeric($x));
if($i == 1)
$i = "True";
else $i = "False";

SEM-6 ER. No.:206120316021 Page | 35


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

echo "is_numeric() testing function on $x : $i <br>";


$i=(is_long($x));
if($i == 1)
$i = "True";
else $i = "False";
echo "is_long() testing function on $x : $i <br>";
$i=(is_int($a));
if($i == 1)
$i = "True";
else $i = "False";
echo "is_int() testing function on $a : $i <br>";
$i=(is_double($x));
if($i == 1)
$i = "True";
else $i = "False";
echo "is_double() testing function on $x : $i <br>";
//echo "is_real() testing function on $x : ".is_real($x)."<br>";
$i=(is_string($z));
if($i == 1)
$i = "True";
else $i = "False";
echo "is_string() testing function on $z : $i <br>";
$i=(is_null($a));
if($i == 1)
$i = "True";
else $i = "False";
echo "is_null() testing function on $a : $i <br>";
$i=(is_null($x));
if($i == 1)
$i = "True";
else $i = "False";
echo "is_array() testing function on $x : $i <br>";
$i=(is_object($z));
if($i == 1)
$i = "True";
else $i = "False";
echo "is_object() testing function on $z : $i <br>";
$i=(is_resource($a));
if($i == 1)
$i = "True";
else $i = "False";
echo "is_resource() testing function on $a : $i <br>";
$i=(is_scalar($x));
if($i == 1)
$i = "True";
else $i = "False";
echo "is_scalar() testing function on $x : $i <br>";
?>

SEM-6 ER. No.:206120316021 Page | 36


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

Output:

Questions:
1. Describe use of isset () function in PHP.

2. Describe use of unset () function in PHP.

SEM-6 ER. No.:206120316021 Page | 37


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

Aim:
2.3 Write PHP script to demonstrate use of various string handling function.
2.4 Write a PHP script to demonstrate use of various str_replace ().
2.5 Write a PHP script to demonstrate implode (), explode () to convert array into string and
string into array.
Software Required: Notepad, WordPad, Notepad++, Dreamweaver, Wamp/Xampp
Pre-requisite: Basic knowledge of editor
Theory/Logic:
 STRING FUNCTION: -
 It allows you to manipulate String in Various Ways such as type of variable, set type, display
value.
 Following table shows string function with its syntax and description.

Name Syntax Description


chr() chr(ASCII-Value); Accepts ASCII value as an argument and
Returns a character value of it.
ord() ord(string); Accepts string as an argument and Returns
an ASCII Value of first char of that strings an
argument.
strtolower () strtolower(string); Accepts string as an argument and converts
all the characters of the specified string into
lowercase letters.
strtoupper () strtoupper(string); Accepts string as an argument and converts
all the characters of the specified string into
uppercase letters.
strlen() strlen(string); Accepts string as an argument and returns
an integer value indicating the length of a
string.
ltrim() ltrim(string [, predefine Accepts string as an argument and removes
character]); white spaces from left side of the string.

rtrim() rtrim(string [, predefine Accepts string as an argument and removes


character]); white spaces from right side of the string.

trim() trim(string [, predefine Accepts string as an argument and removes


character]); white spaces from both sides of the string.

SEM-6 ER. No.:206120316021 Page | 38


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

substr() substr(string Accepts string as an argument and returns


,st,[,number]); specific part of the string.
strcmp() strcmp(string 1 ,string 2); Accepts two strings as an argument and
compares that strings.
strpos() strpos(Original string Accepts two strings as an argument and
,Search string [, Start]); returns the position of the first occurrence
of a search string inside another string.
strstr() strstr(Original string Accepts two strings as an argument and
,Search string ); searches for the first occurrence of a search
string inside original string.
str_replace() str_replace(Findcharacter, Accepts a string as an argument and
Replacecharacter, string); Replace specified Characters of the string
with another Specified characters.
strrev() strrev(string); Accepts string as an argument and
Reverses that string.
echo() echo (string); Accepts one or more strings as an argument
and Display one or more strings on the
browser.
Print() print (string); Accepts one or more strings as an argument
and Display one or more strings on the
browser.

Program:-
P_2_3.php:
<!--P_2.3 Write PHP script to demonstrate use of various string handling function. -->
<?php
echo "<h2>String Functions:</h2>";
$a = "Hello World!";
$b = "hello";
$c = "WELCOME";
$i = 86;
$j = 0;
$k = 82;
//chr()
echo "<h4>STR():</h4>";
echo "The ASCII Value $i's Character is : " . chr($i);
echo "<br>The ASCII Value $j's Character is : " . chr($j);
echo "<br>The ASCII Value $k's Character is : " . chr($k);

//ord()
echo "<h4>ORD():</h4>";

SEM-6 ER. No.:206120316021 Page | 39


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

echo "The $a's ASCII Value is : ". ord($a);


echo "<br>The $b's ASCII Value is : ". ord($b);
echo "<br>The $c's ASCII Value is : ". ord($c);

//strtolower()
echo "<h4>STRTOLOWER():</h4>";
echo "The $a Convert into : ". strtolower($a);
echo "<br>The $b Convert into : ". strtolower($b);
echo "<br>The $c Convert into : ". strtolower($c);

//strtoupper()
echo "<h4>STRTOUPPER():</h4>";
echo "The $a Convert into : ". strtoupper($a);
echo "<br>The $b Convert into : ". strtoupper($b);
echo "<br>The $c Convert into : ". strtoupper($c);

//strlrn
echo "<h4>STRLEN():</h4>";
echo "The Lengh of String $a is : ". strlen($a);
echo "<br>The Lengh of String $b is : ". strlen($b);
echo "<br>The Lengh of String $c is : ". strlen($c);

//ltrim()
$i = "\0 Welcome!! \t";
$j = "\n\t World!\r";
$k = " Hello \t";
echo "<h4>LTRIM():</h4>";
echo "Without ltrim : $i , With ltrim : ". ltrim($i);
echo "<br>Without ltrim : $j , With ltrim : ". ltrim($j);
echo "<br>Without ltrim : $k , With ltrim : ". ltrim($k);

//rtrim()
echo "<h4>RTRIM():</h4>";
echo "Without rtrim : $i , With rtrim : ". rtrim($i);
echo "<br>Without rtrim : $j , With rtrim : ". rtrim($j);
echo "<br>Without rtrim : $k , With rtrim : ". rtrim($k);

//trim()
echo "<h4>TRIM():</h4>";
echo "Without trim : $i , With trim : ". trim($i);
echo "<br>Without trim : $j , With trim : ". trim($j);
echo "<br>Without trim : $k , With trim : ". trim($k);

//substr()
echo "<h4>SUBSTR():</h4>";
echo "The string is $a it's substring is : ". substr($a,-6,-1);
echo "<br>The string is $b it's substring is : ". substr($b,-3);
echo "<br>The string is $c it's substring is : ". substr($c,3);

//strcmp()
echo "<h4>STRCMP():</h4>";
$i = strcmp($a,$b);

SEM-6 ER. No.:206120316021 Page | 40


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

if ($i == 0){
$i = "Strings Are Equal";
} else if($i <0){
$i = "String1 is Less than String2";
}else{
$i = "String1 is Greater than String2";
}
echo "String1: $a is compare with String2: $b is : $i";
$i = strcmp($b,$c);
if ($i == 0){
$i = "Strings Are Equal";
} else if($i <0){
$i = "String1 is Less than String2";
}else{
$i = "String1 is Greater than String2";
}
echo "<br>String1: $b is compare with String2: $c is : $i";
$i = strcmp($a,$c);
if ($i == 0){
$i = "Strings Are Equal";
} else if($i <0){
$i = "String1 is Less than String2";
}else{
$i = "String1 is Greater than String2";
}
echo "<br>String1: $a is compare with String2: $c is : $i";

//strcasecmp()
echo "<h4>STRCASECMP():</h4>";
$i = strcasecmp($a,$b);
if ($i == 0){
$i = "Strings Are Equal";
} else if($i <0){
$i = "String1 is Less than String2";
}else{
$i = "String1 is Greater than String2";
}
echo "String1: $a is compare with String2: $b is : $i";
$i = strcasecmp($b,$c);
if ($i == 0){
$i = "Strings Are Equal";
} else if($i <0){
$i = "String1 is Less than String2";
}else{
$i = "String1 is Greater than String2";
}
echo "<br>String1: $b is compare with String2: $c is : $i";
$i = strcasecmp($a,$c);
if ($i == 0){
$i = "Strings Are Equal";
} else if($i <0){
$i = "String1 is Less than String2";

SEM-6 ER. No.:206120316021 Page | 41


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

}else{
$i = "String1 is Greater than String2";
}
echo "<br>String1: $a is compare with String2: $c is : $i";

$i = "World";
$j = "o";
$k = "come";
//strpos()
echo "<h4>STRPOS():</h4>";
echo "In this String: $a ,The Search String: $i Position is : ". strpos($a,$i);
echo "<br>In this String: $b ,The Search String: $j Position is : ". strpos($b,$j);
echo "<br><b>Using the STRIPOS():</b>";
echo "<br>In this String: $c ,The Search String: $k Position is : ". stripos($c,$k);

//strstr()
echo "<h4>STRSTR():</h4>";
echo "The String is $a , The Search String is : ". strstr("Hello World!","o");
echo "<br>The String is $b , The Search String is : " . strstr($b,"h");
echo "<br><b>Using the STRISTR():</b>";
echo "<br>The String is $c , The Search String is : " . stristr($c,"c"). "<br>";

//strrev()
echo "<h4>STRREV():</h4>";
echo "The is String: $a ,The Reverse String is : ". strrev($a);
echo "<br>The is String: $b ,The Reverse String is : ". strrev($b);
echo "<br>The is String: $c ,The Reverse String is : ". strrev($c);

//echo()
$y = "PHP";
echo "<h4>ECHO():</h4>";
echo "The Subject is : " . $y . "<br>";
echo 'The Subject is : $y <br>';
echo "The Subject is : $y";

//print()
$z = "Black";
echo "<h4>PRINT():</h4>";
print "The Color is : " . $z . "<br>";
print 'The Color is : $z <br>';
print "The Color is : $z";
?>

SEM-6 ER. No.:206120316021 Page | 42


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

Output:

SEM-6 ER. No.:206120316021 Page | 43


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

SEM-6 ER. No.:206120316021 Page | 44


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

P_2_4.php:
<!--P_2.4 Write a PHP script to demonstrate use of various str_replace (). -->
<?php
echo "<h2>Use of Various STR_REPLACE() : </h2>";
//Replace a String
echo "<h4>Replace a String:</h4>";
$a = "Hello World!!";
$i = str_replace("World!!","PHP Language",$a);
echo "The String is : $a , That Replace with : $i ";

//Replace an Element in Array


echo "<h4>Replace an Element in Array</h4>";
$b = array("PHP","WNS","DS");
echo "The Array is : ";
print_r($b);
echo " , That Replace Element by : ";
print_r(str_replace("DS","Android",$b));

//Replace an Array with other Array


echo "<h4>Repalce an Array with Other Array : </h4>";
$x = array("Red");
$y = array("Black");
$z = array("Your","Favourite","Color","is","Red");
echo "The Array is : ";
print_r($z);
echo " , That Replace by : ";
print_r(str_replace($x,$y,$z));
?>

Output:

P_2_5.php:
<!-- P_2.5 Write a PHP script to Demonstrate implode (), explode () to convert array into
string and string into array. -->
<?php
//implode() Array into String
$a = array("PHP","Andoid");
$b = array("WNS","Advanced Java");
echo "<h4>Convert Array Into String : </h4>";
echo "The 1st Array Value is : ";

SEM-6 ER. No.:206120316021 Page | 45


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

foreach($a as $value)
{
echo "$value " ;
}
echo "<br>The 2nt Array Value is : ";
foreach($b as $value)
{
echo "$value ";
}
$i = implode(",", $a) . "," . implode(",", $b);
echo "<br><br> The Array Convert into String : $i";

//explode() String into An Array


$str = "Black,Red,Blue,Orange";
echo "<h4>Convert String Into Array : </h4>";
echo "The String is : $str <br><br>";
$arr = explode("," , $str);
echo "The String Convert into Array : <br>";
foreach($arr as $value)
{
echo "$value <br>";
}
?>

Output:

SEM-6 ER. No.:206120316021 Page | 46


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

Questions:
1. Describe use of substr () function in PHP.

2. Describe use of echo () function in PHP.

SEM-6 ER. No.:206120316021 Page | 47


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

Aim:
2.6 Write a PHP script to demonstrate use of date () with all format specifications.
2.7 Write a PHP script to Demonstrate getdate () and check date ().
2.8 Write a PHP script to Demonstrate time () and mktime () (use my birth date).

Software Required: Notepad, WordPad, Notepad++, Dreamweaver, Wamp/Xampp


Pre-requisite: Basic knowledge of editor
Theory/Logic:
 DATE FUNCTION:
 It allows you to display, format and manipulate the date and time on server.
 Following table shows date function with its syntax and description.

Name Syntax Description


date() date( format, Allows you to format and display current date and
timestamp); time of web server.
getdate() getdate(timestamp); Returns an array which contains following
information of current UNIX timestamp.
set date() set date (year, month, It will set the year, month and day as per passing
day); parameters from set date () function.
check check date( month Accepts month, day & year of date as an argument
date() ,day, year); and determines specified date is valid or not.
time() time( ); Returns current UNIX timestamp.
mktime() mktime(hour, minute, Returns current UNIX timestamp if no parameter
second, month, day, is passed.
year, is_dst);

Program/Solution:-
P_2_6.php:
<!-- P_2.6 Write a PHP script to demonstrate use of date () with all format specifications.
-->
<!DOCTYPE html>
<html lang="en">
<body>
<div>
<table Border=1>
<tr> <th colspan="2"> DATE(): </th> </tr>
<tr> <th> Format: </th> <th> Answer: </th> <tr>
<?php
//date()
//d - The day of the month (from 01 to 31)

SEM-6 ER. No.:206120316021 Page | 48


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

echo "<tr><th>d</th>";
echo "<td>".date("d")."</td></tr>";
//j - The day of the month without leading zeros (1 to 31)
echo "<tr><th>j</th>";
echo "<td>".date("j")."</td></tr>";
//D - A textual representation of a day (three letters)
echo "<tr><th>D</th>";
echo "<td>".date("D")."</td></tr>";
//l (lowercase 'L') - A full textual representation of a day
echo "<tr><th>l</th>";
echo "<td>".date("l")."</td></tr>";
// m - A numeric representation of a month (from 01 to 12)
echo "<tr><th>m</th>";
echo "<td>".date("m")."</td></tr>";
// n - A numeric representation of a month, without leading zeros (1 to 12)
echo "<tr><th>n</th>";
echo "<td>".date("n")."</td></tr>";
// M - A short textual representation of a month (three letters)
echo "<tr><th>M</th>";
echo "<td>".date("M")."</td></tr>";
// F - A full textual representation of a month (January through December)
echo "<tr><th>F</th>";
echo "<td>".date("F")."</td></tr>";
// Y - A four digit representation of a year
echo "<tr><th>Y</th>";
echo "<td>".date("Y")."</td></tr>";
// y - A two digit representation of a year
echo "<tr><th>y</th>";
echo "<td>".date("y")."</td></tr>";
// a - Lowercase am or pm
echo "<tr><th>a</th>";
echo "<td>".date("a")."</td></tr>";
// A - Uppercase AM or PM
echo "<tr><th>A</th>";
echo "<td>".date("A")."</td></tr>";
// H - 24-hour format of an hour (00 to 23)
echo "<tr><th>H</th>";
echo "<td>".date("H")."</td></tr>";
// G - 24-hour format of an hour (0 to 23)
echo "<tr><th>G</th>";
echo "<td>".date("G")."</td></tr>";
// h - 12-hour format of an hour (01 to 12)
echo "<tr><th>h</th>";
echo "<td>".date("h")."</td></tr>";
// g - 12-hour format of an hour (1 to 12)
echo "<tr><th>g</th>";
echo "<td>".date("g")."</td></tr>";
// s - Seconds, with leading zeros (00 to 59)
echo "<tr><th>s</th>";
echo "<td>".date("s")."</td></tr>";
// i - Minutes with leading zeros (00 to 59)
echo "<tr><th>i</th>";

SEM-6 ER. No.:206120316021 Page | 49


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

echo "<td>".date("i")."</td></tr>";
?>
</table>
</div>
</body>
</html>

Output:

SEM-6 ER. No.:206120316021 Page | 50


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

P_2_7.php:
<!-- P_2.7 Write a PHP script to Demonstrate getdate() and checkdate(). -->
<?php
//getdate()
echo "<h4>GETDATE(): </h4>";
echo "The Current Date & Time is : ";
print_r(getdate());

//chechdate()
echo "<h4> CHECKDATE(): </h4>";
echo "The Date (07,13,2003) is :";
$i = checkdate (07,13,2003);
If($i)
echo "Valid";
else
echo "Not Valid";
echo "<br>The Date (03,01,2005) is :";
$i = checkdate (3,01,2005);
If($i)
echo "Valid";
else
echo "Not Valid";
echo "<br>The Date (02,30,2001) is :";
$i = checkdate (02,30,2001);
If($i)
echo "Valid";
else
echo "Not Valid";
?>

Output:

P_2_8.php:
<!-- P_2.8 write a PHP script to Demonstrate time() and mktime()(use my birthdate). -->
<?php
//time()
echo "<h4> TIME(): </h4>";
echo "The Time is from 1 January 1970 00:00:00 to Current Time : ". time();

//mktime()
echo "<h4> MKTIME(): </h4>";
echo "My Birthdate : ".date("M-d-Y",mktime(0,0,0,3,1,2005));

SEM-6 ER. No.:206120316021 Page | 51


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

echo "<br> The Current Date : ".date("M-d-Y",mktime(0,0,0,2,14,2023));


echo "<br>The Time is from 1 January 1970 00:00:00 to My Birthdate : "
.mktime(1,1,1,3,1,2005);
?>

Output:

Questions:-

1. How to find current date and time?

2. Describe use of mktime () function in PHP.

SEM-6 ER. No.:206120316021 Page | 52


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

Aim:
2.9 Write PHP script to demonstrate all Math functions.
Software Required: Notepad, WordPad, Notepad++, Dreamweaver, Wamp/Xampp
Pre-requisite: Basic knowledge of editor
Theory/Logic:
 MATH FUNCTION:
 It allows you to perform various operations on numeric values.
 Following table shows math function with its syntax and description.

Name Syntax Description


abs() abs(Number); Accepts numbers an argument and Returns the absolute
value of a number.
ceil() ceil(Number); Accepts number as an argument and Returns the
number which is rounded upwards to the nearest
integer value.
floor() floor(Number); Accepts number as an argument and Returns the
number which is rounded downwards to the nearest
integer value.
round() round(Number Accepts number as an argument and Returns the
[,Precision ]); number rounded to the nearest integer.
fmod() fmod(Number Accepts two numbers as an argument and divides num1
1,Number2); by num2 and returns reminder of division.
min() min(Number Accepts two numbers as an argument and returns
1,Number2); lowest value among them.
max() max(Number Accepts two numbers as an argument and returns
1,Number2); highest value among them.
pow() pow(Number Accepts two numbers as an argument and raises num1
1,Number2); to the power of num2 and returns result.
sqrt() sqrt(Number); Accepts a number as an argument and Returns square
root of a number.
rand() rand([min],[max Generate a random integer between the ranges of 0 to
]); ROUND_MAX.

Program/Solution:
P_2_9.php:
<!-- P_2.9 Write PHP script to demonstrate all Math functions. -->
<?php

SEM-6 ER. No.:206120316021 Page | 53


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

echo "<h2> Math Funtion : </h2>";


$a = 30.21;
$b = -21.60;
$c = 0.23;
//abs()
echo "<h4> ABS() : </h4>";
echo "The Number is $a , It's Absolute Number is : ". abs($a);
echo "<br>The Number is $b , It's Absolute Number is : ". abs($b);
echo "<br>The Number is $c , It's Absolute Number is : ". abs($c);

//ceil()
echo "<h4> CEIL() : </h4>";
echo "The Number is $a , It's Ceiling Number is : ". ceil($a);
echo "<br>The Number is $b , It's Ceiling Number is : ". ceil($b);
echo "<br>The Number is $c , It's Ceiling Number is : ". ceil($c);

//floor()
echo "<h4> FLOOR() : </h4>";
echo "The Number is $a , It's Floor Number is : ". floor($a);
echo "<br>The Number is $b , It's Floor Number is : ". floor($b);
echo "<br>The Number is $c , It's Floor Number is : ". floor($c);

//round()
echo "<h4> ROUND() : </h4>";
echo "The Number is $a , It's Round Number is : ". round($a);
echo "<br>The Number is $b , It's Round Number is : ". round($b);
echo "<br>The Number is $c , It's Round Number is : ". round($c);

$x = 21;
$y = 6;
$i = 14.4;
$j = 1.2;
$p = 15;
$q = 10;
//fmod()
echo "<h4> FMOD() : </h4>";
echo "The Number $x is Divided by $y , It's Reminder is : ". fmod($x,$y);
echo "<br>The Number $i is Divided by $j , It's Reminder is : ". fmod($i,$j);
echo "<br>The Number $p is Divided by $q , It's Reminder is : ". fmod($p,$q);

//min()
echo "<h4> MIN() : </h4>";
echo "The Number is $x and $y , Minimun Number is : ". min($x,$y);
echo "<br>The Number is $i and $j , Minimun Number is : ". min($i,$j);
echo "<br>The Number is $p and $q , Minimun Number is : ". min($p,$q);

//max()
echo "<h4> MAX() : </h4>";
echo "The Number is $x and $y , Maximun Number is : ". max($x,$y);
echo "<br>The Number is $i and $j , Maximun Number is : ". max($i,$j);
echo "<br>The Number is $p and $q , Maximun Number is : ". max($p,$q);

SEM-6 ER. No.:206120316021 Page | 54


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

$x = 5;
$y = 2;
$i = 16;
$j = 3;
$p = 5;
$q = 5;
//pow()
echo "<h4> POW() : </h4>";
echo "The $x Power $y is : ". pow($x,$y);
echo "<br>The $i Power $j is : ". pow($i,$j);
echo "<br>The $p Power $q is : ". pow($p,$q);

$a = 100;
$b = -25;
$c = 225;
//sqrt()
echo "<h4> SQRT() : </h4>";
echo "The Square Root of $a is : ". sqrt($a);
echo "<br>The Square Root of $b is : ". sqrt($b);
echo "<br>The Square Root of $c is : ". sqrt($c);

$a = 21;
$b = 30;
$x = 100;
$y = 225;
//rand()
echo "<h4> RAND() : </h4>";
echo "Generate The Random Number Between 0 to rand_max : ". rand();
echo "<br>Generate The Random Number Between The $a to $b : ". rand($a,$b);
echo "<br>Generate The Random Number Between The $x to $y : ". rand($x,$y);
?>
Output:

SEM-6 ER. No.:206120316021 Page | 55


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

SEM-6 ER. No.:206120316021 Page | 56


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

Questions:
1. List out math functions.

2. Describe use of rand () function in PHP.

SEM-6 ER. No.:206120316021 Page | 57


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

Aim:
2.10 Write PHP script to demonstrate Array functions.
2.11 Write a PHP script to demonstrate sort (), asort (), ksort () on associative array.
Software Required: Notepad, WordPad, Notepad++, Dreamweaver, Wamp/Xampp
Pre-requisite: Basic knowledge of editor
Theory/Logic:
 ARRAY FUNCTION:
 It allows you to manipulate Arrays in Various Ways in One dimensional as well as multi
dimensional arrays.
 Following table shows array function with its syntax and description.

Name Syntax Description


count() count(Array Name, Accepts an array as an argument and returns
mode); Number of elements in an array.
list() list(Variable1,Variabl Accepts one or more variables as an argument and
2,….,VariableN)=Arra assigns them values from an array.
yName;
in_array() in_array( Search It is used to search specific value from an array.
Value,
ArrayName,SearchTy
pe);
current() current(Array Name); Accepts an array as an argument and returns value
of current element in an array.
next() next(Array Name); Accepts an array as an argument and moves
internal pointer position ahead so that it points to
next element in array from current pos.
prev() prev( Array Name); Accepts an array as an argument and moves
internal pointer position down so that it points to
prev element in array from current pos.
end() end(Array Name); Accepts an array as an argument and moves
internal pointer pos last so that it points to last
element in array from current pos.
each() each(Array Name); Accepts an array as an argument and Returns the
key and value of current element and then it will
moves the internal point forward to the next
element.

SEM-6 ER. No.:206120316021 Page | 58


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

sort() sort(Array Name, Accepts an array as an argument and sorts


sortType); elements of an array in ascending order.
array_mer array_merge(Array Accepts one or more than one array as an
ge() Name1, Array Name2, argument and merges them and returns a single
Array NameN); array that contains all the elements.
array_rev array_reverse(Array Accepts an array as an argument and returns an
erse() Name, Preserve); array that contains elements in reverse order.
reset() reset(Array Name); Used to move the array's internal pointer to the
first element.

Program/Solution:
P_2_10.php:
<!-- P_2.10 Write PHP script to demonstrate Array functions. -->
<?php
echo "<h2>Array Function: </h2>";
$a = array("PHP","WNS","Android","Advanced Java");
$b = array("Black","White","Red");
$name = array("Rajvi","Vishal");
$n = array(21,67,30,40);
//count()
echo "<h4>COUNT():</h4>";
echo 'The $a Array has '. count($a) . " Elements.<br>";
echo 'The $b Array has '. count($b) . " Elements.<br>";
echo 'The $n Array has '. count($n) . " Elements.";

//list()
echo "<h4>LIST():</h4>";
list($i,$j)=$name;
echo 'The Elements of $name Array are '."$i & $j";
list($i,$j,$k)=$b;
echo '<br>The Elements of $b Array are '."$i, $j & $k";
list($i,$j,$k,$l)=$a;
echo '<br>The Elements of $a Array are '."$i, $j, $k & $l";

//in_array()
$x = "30";
$y = "rose";
$z = 21;
echo "<h4>IN_ARRAY():</h4>";
$i = array("Rose","Lotus",30,21);
//we not define true in searchtype
if(in_array($x,$i))
echo "The $x is Found in the Array.<br>";
else
echo "The $x is not Found in the Array.<br>";
//Case-sensitive
if(in_array($y,$i))

SEM-6 ER. No.:206120316021 Page | 59


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

echo "The $y is Found in the Array.<br>";


else
echo "The $y is not Found in the Array.<br>";
//true so chech the datatype of element
if(in_array($z,$i,true))
echo "The $z is Found in the Array.<br>";
else
echo "The $z is not Found in the Array.<br>";

//current()
echo "<h4>CURRENT():</h4>";
echo 'The $a '. "Array's Current Element is : ". current($a) . "<br>";
echo 'The $b '. "Array's Current Element is : ". current($b) . "<br>";
echo 'The $name '. "Array's Current Element is : ". current($name) . "<br>";

//next()
echo "<h4>NEXT():</h4>";
echo 'The $a '. "Array's Current Element is : ". current($a) . "<br>";
next($a); //moves internal pointer position ahead
echo 'The $a '. "Array's Current Element After the NEXT() Function : ". current($a) .
"<br><br>";
echo 'The $b '. "Array's Current Element is : ". current($b) . "<br>";
next($b);
echo 'The $b '. "Array's Current Element After the NEXT() Function : ". current($b) .
"<br><br>";
echo 'The $name '. "Array's Current Element is : ". current($name) . "<br>";
next($name);
echo 'The $name '. "Array's Current Element After the NEXT() Function : ".
current($name) . "<br>";

//previous()
echo "<h4>PREV():</h4>";
echo 'The $a '. "Array's Current Element is : ". current($a) . "<br>";
next($a); next($a);
prev($a); //moves internal pointer position down
echo 'The $a '. "Array's Current Element After the PREV() Function : ". current($a) .
"<br><br>";
echo 'The $b '. "Array's Current Element is : ". current($b) . "<br>";
prev($b);
echo 'The $b '. "Array's Current Element After the PREV() Function : ". current($b) .
"<br><br>";
echo 'The $name '. "Array's Current Element is : ". current($name) . "<br>";
prev($name);
echo 'The $name '. "Array's Current Element After the PREV() Function : ".
current($name) . "<br>";

//end()
echo "<h4>END():</h4>";
echo 'The $a '. "Array's Current Element is : ". current($a) . "<br>";
end($a); //moves internal pointer pos last
echo 'The $a '. "Array's Last Position Element using END() Function : ". current($a) .
"<br><br>";
echo 'The $b '. "Array's Current Element is : ". current($b) . "<br>";

SEM-6 ER. No.:206120316021 Page | 60


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

end($b);
echo 'The $b '. "Array's Last Position Element using END() Function : ". current($b) .
"<br><br>";
echo 'The $name '. "Array's Current Element is : ". current($name) . "<br>";
end($name);
echo 'The $name '. "Array's Last Position Element using END() Function : ".
current($name) . "<br>";

//each()
$b = array("Black","White","Red");
echo "<h4>EACH():</h4>";
foreach($b as $val)
{
echo "The Value of Array is: ";
print_r($val);
echo "<br>";
}

//sort()
echo "<h4>SORT():</h4>";
sort($a,SORT_STRING);
echo "The Array After Sorting in Ascending Order(String) : <br>";
print_r($a);
sort($b,SORT_REGULAR);
echo "<br><br>The Array After Sorting in Ascending Order(Regular) : <br>";
print_r($b);
sort($n,SORT_NUMERIC);
echo "<br><br>The Array After Sorting in Ascending Order(Numeric) : <br>";
print_r($n);

//array_merge()
echo "<h4>ARRAY_MERGE():</h4>";
$merge = array_merge($b,$name);
echo 'The Array $b and array $name is Merged : <br>';
print_r($merge);
$merge = array_merge($a,$n);
echo '<br>The Array $a and array $n is Merged : <br>';
print_r($merge);
$merge = array_merge($b,$a);
echo '<br>The Array $b and array $a is Merged : <br>';
print_r($merge);

//array_reverse()
echo "<h4>ARRAY_REVERSE():</h4>";
$k = array_reverse($b);
echo 'The Array $b Reversed Array : <br>';
print_r($k);
$k = array_reverse($a);
echo '<br>The Array $a Reversed Array : <br>';
print_r($k);
$k = array_reverse($i);
echo '<br>The Array $i Reversed Array : <br>';

SEM-6 ER. No.:206120316021 Page | 61


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

print_r($k);

//reset()
echo "<h4>RESET():</h4>";
next($a);
echo 'The $a '. "Array's Current Element is : ". current($a) . "<br>";
reset($a); //move the array's internal pointer to the first element
echo 'The $a '. "Array's Current Element After the RESET() Function : ". current($a) .
"<br><br>";
next($b);
echo 'The $b '. "Array's Current Element is : ". current($b) . "<br>";
reset($b);
echo 'The $b '. "Array's Current Element After the RESET() Function : ". current($b) .
"<br><br>";
echo 'The $name '. "Array's Current Element is : ". current($name) . "<br>";
reset($name);
echo 'The $name '. "Array's Current Element After the RESET() Function : ".
current($name) . "<br>";
?>
Output:

SEM-6 ER. No.:206120316021 Page | 62


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

SEM-6 ER. No.:206120316021 Page | 63


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

SEM-6 ER. No.:206120316021 Page | 64


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

P_2_11.php:
<!-- P_2.11 Write a PHP script to demonstrate sort (), asort (), ksort () on associative
array. -->
<?php
echo "<h2>Sorting on Associative Array: </h2>";
$i = array("Strawberry","Apple","Mango","Cherry");

//sort()
echo "<h4>SORT():</h4>";
echo 'The array $i is Sort in Ascending Order Using SORT() : <br>';
sort($i);
foreach($i as $value)
echo "$value ";
echo '<br><br>The array $i is Sort in Descending Order Using RSORT() : <br>';
rsort($i);
foreach($i as $value)
echo "$value ";

$i = array(40=>"Strawberry",30=>"Apple",20=>"Mango",50=>"Cherry");
//asort()
echo "<h4>ASORT():</h4>";
echo 'The Array $i is Sort in Ascending Order of Value Using ASORT() : <br>';
asort($i);
foreach($i as $key=>$value)

SEM-6 ER. No.:206120316021 Page | 65


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

echo "$value = $key <br>";


echo '<br>The Array $i is Sort in Descending Order of Value Using ARSORT() : <br>';
arsort($i);
foreach($i as $key=>$value)
echo "$value = $key <br>";

//ksort()
echo "<h4>KSORT():</h4>";
echo 'The Array $i is Sort in Ascending Order of Key Using KSORT() : <br>';
ksort($i);
foreach($i as $key=>$value)
echo "$key = $value <br>";
echo '<br>The Array $i is Sort in Descending Order of Key Using KRSORT() : <br>';
krsort($i);
foreach($i as $key=>$value)
echo "$key = $value <br>";
?>

Output:

SEM-6 ER. No.:206120316021 Page | 66


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

Questions:
1. Describe use of asort () function in PHP.

2. Describe use of next () function in PHP.

SEM-6 ER. No.:206120316021 Page | 67


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

Aim:
2.12 Write PHP script to demonstrate File functions.
2.13 Write PHP script to count no. of words in a file using file and string handling functions.
Software Required: Notepad, WordPad, Notepad++, Dreamweaver, Wamp/Xampp
Pre-requisite: Basic knowledge of editor
Theory/Logic:
 FILE FUNCTION
 It allows you to create & open file, Reading & Writing contents of file and closing file.
 Following table shows file function with its syntax and description.

Name Syntax Description


fopen() fopen (FileName ,File Used to create or open file.
Mode, FilePath,
Context);
fread() fread (File Used to read content from specified file.
,Maxlength);
fwrite() fwrite (File, string, Used to write some text into the file.
Max length);
fclose() fclose($File); Used to close file.

Program/Solution:
P_2_12.php:
<!-- P_2.12 Write PHP script to demonstrate use of fopen (), fread (), fwrite () and fclose
() File functions. -->
<?php
echo "<h2>File Function: </h2>";
//fopen()
echo "<h4>FOPEN():</h4>";
//r : Opens file in Read Only Mode.
$file = fopen("D:\Hello.txt","r");
if($file)
echo "File Open Successfully";
else
echo "File doesn't open Successfully";

/*x+ : Creates and opens file in read - write mode. It places the file pointer at the
beginning of the file.
If file is not found, fopen() function returns FALSE.*/
$file1 = fopen("D:\Demo.txt","x+");
if($file1)
echo "<br>File is Created Successfully.<br>";
else
echo "File doesn't Created Successfully.<br>";

SEM-6 ER. No.:206120316021 Page | 68


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

//w+ : Opens file in Read/Write Mode. Delete previous content.


$file2 = fopen("D:\Demo.txt","w");
if($file2)
echo "<br>File is Found.<br>";
else
echo "File is not Found.<br>";

//if file is not exist than,c+ Creates a file and that file in read/write mode.
$file3 = fopen("D:\Prac_2.12.txt","c+");
if($file3)
echo "<br>File Created Successfully.<br>";
else
echo "File doesn't Created Successfully.<br>";

//a : Opens file in Write mode. Add new content without delete.
$file4 = fopen("D:\Demo.txt","a");
if($file4)
echo "<br>File is Found.<br>";
else
echo "File is not Found.<br>";

//fwrite()
echo "<h4>FWRITE():</h4>";
$k = fwrite($file1,"This is Demo File.",25);
echo "The Characters in the \$file1 is : $k";
$n = fwrite($file3,"This Practical for the File Function.",50);
echo "<br>The Total Character in the \$file3 is : $n";
$m = fwrite($file4,"\n Welcome!!",50);
echo "<br>The Character in the \$file4 is : $m";

//fread()
echo "<h4>FREAD():</h4>";
$i = fread($file,5);
echo "$i Character Read from the \$file.<br>";
$j = fread($file1,100);
echo "$j Character Read from the \$file2.<br>";

//fclose()
echo "<h4>FCLOSE():</h4>";
$a = fclose($file3);
//fclose($file);
if($a)
echo "The File is Closed Successfully";
else
echo "The File is Still Open";
?>

SEM-6 ER. No.:206120316021 Page | 69


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

Output:

Prac_2.12.txt:
This Practical for the File Function.
Demo.txt:
Welcome to the PHP world.

SEM-6 ER. No.:206120316021 Page | 70


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

Hello1.txt:
Welocome!! to the PHP World.PHP is Case Sensitive.
P_2_13.php:
<!-- P_2.13 Write a PHP script to count no. of words in a file using file and string
handling functions. -->
<?php
$file = "D:\Prac_2.12.txt";
$open = fopen($file,"r");
$content = fread($open,filesize($file));
$words = str_word_count($content);
echo "The Number of Words in the file : $file is $words";
fclose($open);

$file1 = "D:\Demo.txt";
$open1 = fopen("$file1","r");
$content1 = fread($open1,filesize($file1));
$words1 = str_word_count($content1);
echo "<br><br>The Number of Words in the file : $file1 is $words1";
fclose($open1);

$file2 = "D:\Hello1.txt";
$open2 = fopen("$file2","r");
$content2 = fread($open2,filesize($file2));
$words2 = str_word_count($content2);
echo "<br><br>The Number of Words in the file : $file2 is $words2";
fclose($open2);
?>
Output:

Questions:
1. Describe use of x+ mode in fopen ().

SEM-6 ER. No.:206120316021 Page | 71


I.T. Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)

2. Describe use of a+ mode in fopen ().

 Conclusion:

SEM-6 ER. No.:206120316021 Page | 72

You might also like