Practial 2
Practial 2
Dept Year 2023 3361603 (WEB DESINGING USING PHP AND MYSQL)
PRACTICAL LIST – 2
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.
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;
//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 = ';
//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 : ";
print_r($k);
?>
Output:
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";
Output:
Questions:
1. Describe use of isset () function in PHP.
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.
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>";
//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);
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";
}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";
?>
Output:
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 ";
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 : ";
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";
Output:
Questions:
1. Describe use of substr () function in PHP.
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).
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)
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>";
echo "<td>".date("i")."</td></tr>";
?>
</table>
</div>
</body>
</html>
Output:
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));
Output:
Questions:-
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.
Program/Solution:
P_2_9.php:
<!-- P_2.9 Write PHP script to demonstrate all Math functions. -->
<?php
//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);
$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:
Questions:
1. List out math functions.
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.
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))
//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>";
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>';
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:
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)
//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:
Questions:
1. Describe use of asort () function in PHP.
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.
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>";
//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";
?>
Output:
Prac_2.12.txt:
This Practical for the File Function.
Demo.txt:
Welcome to the PHP world.
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 ().
Conclusion: