Practical No 06 (Answers)
Practical No 06 (Answers)
A. Practical Significance
PHP has number of built-in functions defined for various purposes and it also supports user-
defined functions. The user-defined functions can perform the task specified by the user. Using user-
defined function, it is possible to divide code in a web-page in a way that each function performs a
single task. By dividing the code in functions, the code becomes manageable and easily maintainable.
C. Resources used
Sr. Instrument/Machine
Specifications Quantity Remark
No. /Software
Windows 8.1 (64-Bit), Intel (R) Core
1 Desktop PC 01 Nil
(TM) i3 CPU @2.40 GHz, 4GB RAM
Xampp v3.2.4, Package contains
2 Xampp 01 Nil
Apache + MariaDB + PHP + Perl
Notepad++ and
3 Editor and Browser 01 and 01 Nil
Google Chrome
D. Program Code
a) Write a php program to illustrate the simple functions.
<!DOCTYPE html>
<html>
<body>
<?php
function GenerateRandomNumber() {
$min = 31;
$max = 69;
return rand($min, $max);
}
$guess = 64;
if($guess == GenerateRandomNumber()) {
echo "Lucky!! Your guess is right!</br>";
}
else {
echo "Better luck next time.";
}
?> </body>
</html>
b) Write a php program to illustrate the parameterized functions.
<!DOCTYPE html>
<html>
<body> <?php
function AreaOfCircle($radius) {
echo "Area of Circle: ";
return pi() * ($radius ** 2);
}
// call by value
$num1 = 34;
echo "Number: " . $num1 . "</br>";
Increment1($num1);
echo "Number: " . $num1 . "</br>";
// call by reference
$num2 = 78;
echo "Number: " . $num2 . "</br>";
Increment2($num2);
echo "Number: " . $num2 . "</br>";
?>
</body> </html>
Output:
Number: 34
Number: 35
Number: 34
Number: 78
Number: 79
Number: 79
<?php
function GetLicense($name, $age=18)
{
if($age >= 18)
{
echo $name . ", you got license.</br>";
echo "Your age is " . $age . "</br>";
}
else
{
echo $name . ", you are not eligible" .
" for driving license.</br>";
}
}
GetLicense("Nandkishore", 34);
GetLicense("Harshal");
GetLicense("Jayavanti", 17);
?>
</body>
</html>
Output:
Nandkishore, you got license.
Your age is 34
Harshal, you got license.
Your age is 18
Jayavanti, you are not eligible for driving license.
G. Exercise
a) Write a php program to search an element in an array using function.
<!DOCTYPE html>
<html>
<body>
<?php
function accept(&$List, $n) {
for($i=0; $i<$n; $i++) {
$List[$i] = rand(10, 99);
}
}
function display($List) {
echo "List: ";
foreach ($List as $value) {
echo $value . " ";
}
echo "</br>";
}
$list = array(0,0,0,0,0,0,0,0,0,0,0,0,0);
$n = count($list);
$key = 78;
accept($list, $n);
display($list);
if(search($list, $n, $key) != -1) {
echo "Element is found.</br>";
} else {
echo "Element isn't found.</br>";
}
?>
</body>
</html>
Output: List: 18 78 31 67 42 80 93 37 44 47 37 51 71
Element is found.
List: 14 50 47 86 29 20 39 78 69 70 37 66 43
Element is found.
List: 91 35 56 20 99 73 71 35 84 76 22 99 22
Element isn't found.
Assessment Scheme
Performance Indicator Weightage
4 Presentation of Output 20 %
Assessment
Date Signature of
Marks Obtained
Subject Teacher
Process Related Product Related
Total (50 Marks)
(30 Marks) (20 Marks)