[go: up one dir, main page]

0% found this document useful (0 votes)
582 views6 pages

Practical No 06 (Answers)

This document provides instructions for a practical assignment on functions in PHP. Students are asked to write programs demonstrating simple and parameterized functions. For simple functions, students create a program that generates a random number and checks a guess. For parameterized functions, students write a program that calculates the area of a circle and triangle using functions. The document also provides examples of other functions, questions to test understanding of concepts like default parameters and call by reference, and an exercise to search an array using a function.

Uploaded by

Rutuja Bhagat
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)
582 views6 pages

Practical No 06 (Answers)

This document provides instructions for a practical assignment on functions in PHP. Students are asked to write programs demonstrating simple and parameterized functions. For simple functions, students create a program that generates a random number and checks a guess. For parameterized functions, students write a program that calculates the area of a circle and triangle using functions. The document also provides examples of other functions, questions to test understanding of concepts like default parameters and call by reference, and an exercise to search an array using a function.

Uploaded by

Rutuja Bhagat
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/ 6

Practical No 06: Functions in PHP

Practical Outcomes (PrOs):


Write a simple PHP program to demonstrate use of Simple function and Parameterized function.

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.

B. Minimum Theoretical Background


In PHP, the user-defined function can be defined as given below –
function function-name(parameter-list) : return-type
{
Code to be executed;
}
For example,
<!DOCTYPE html>
<html>
<body>
<?php
function add($num1, $num2) : int {
return $num1 + $num2;
}

$sum = add(56, 44);


print("Sum = $sum");
?>
</body>
</html>
Output: Sum = 100

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);
}

function AreaOfTriangle($base, $height) {


echo "Area of Triangle: ";
return 0.5 * $base * $height;
}

echo AreaOfCircle(3.4) . "</br>";


echo AreaOfTriangle(7.5, 3.9) . "</br>";
?> </body>
</html>

E. Result (Output of Code):


Program a): Better luck next time.
Program b): Area of Circle: 36.316811075498
Area of Triangle: 14.625
F. Practical Related Questions
a) State the use of functions – bindec(), sqrt() and base_convert().
b) Write a php program to illustrate call by value and call by reference.
c) Write a php program to illustrate default value for parameters.

a) Answer: use of functions – bindec(), sqrt() and base_convert() –


1. The bindec() function converts a binary number to a decimal number.
Syntax: integer/float bindec(binary_string);
Example, $bin_num = "101101";
$dec_num = bindec($bin_num); // returns 45
2. The sqrt() function returns the square root of a number.
Syntax: float sqrt(number);
Example $num = sqrt(144); // returns 12
3. The base_convert() function converts a number from one number base to another.
Syntax: string base_convert(number, frombase, tobase);
Example: $num = base_convert(56, 10, 2); // returns 111000

b) Answer: Program to illustrate call by value and call by reference.


<!DOCTYPE html>
<html> <body>
<?php
function Increment1($num)
{
$num++;
printf("Number: $num </br>");
}
function Increment2(&$num)
{
$num++;
printf("Number: $num </br>");
}

// 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

C) Answer: Program to illustrate default value for parameters.


<!DOCTYPE html>
<html>
<body>

<?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>";
}

function search($List, $n, $key) : int


{
for($i=0; $i<$n; $i++) {
if($List[$i] == $key)
return $i;
}
return -1;
}

$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

Process Related (30 Marks) 60 %


Write appropriate code to generate desired output
1 30 %
in Web application.
2 Debug, Test and Execute the programs 20 %

3 Following Ethical Practice 10 %

Product Related (20 Marks) 40 %

4 Presentation of Output 20 %

5 Timely Submission of Practical Assignment 10 %

6 Able to answer the sample questions 10 %

Total (50 Marks) 100 %

List of Students / Team Members


1) ………………………………………………………………………………….
2) ………………………………………………………………………………….
3) ………………………………………………………………………………….
4) ………………………………………………………………………………….

Assessment
Date Signature of
Marks Obtained
Subject Teacher
Process Related Product Related
Total (50 Marks)
(30 Marks) (20 Marks)

You might also like