Tribhuwan University

Institute of Science and Technology

Model

Bachelor Level / Third Year / Fifth Semester / Science

Bachelors in Information Technology (BIT301)

(Web Technology II)

Full Marks: 60

Pass Marks: 24

Time: 3 Hours

Candidates are required to give their answers in their own words as for as practicable.

The figures in the margin indicate full marks.

Section A

Long Answers Questions

Attempt any TWO questions.
[2*10=20]
1.
What is a function? How can you define and call a function in PHP? Write a PHP program to implement a function with parameters and return value.[10]

What is a Function in PHP?

A function is a reusable block of code that performs a specific task, can accept parameters as input, and can return a value as output.


Definition

A function in PHP is a self-contained module of code that:

  • Groups a set of statements together to perform a particular task
  • Can be called (invoked) multiple times from anywhere in the program
  • Helps in code reusability, modularity, and readability
  • Reduces code redundancy (avoids writing same code again and again)

Types of Functions in PHP

  • Built-in functions — Pre-defined functions provided by PHP (e.g., strlen(), array_push())
  • User-defined functions — Functions created by the programmer as per requirement

How to Define a Function in PHP

Syntax:

function functionName($parameter1, $parameter2) {
    // body of the function
    return $value;
}

Rules for defining a function:

  • A function is defined using the keyword function
  • Function name must start with a letter or underscore (not a number)
  • Function names are case-insensitive
  • Parameters are placed inside parentheses ()
  • The return statement sends a value back to the caller

How to Call a Function in PHP

A function is called by writing its name followed by parentheses with arguments (if any):

$result = functionName($arg1, $arg2);
  • When a function is called, the control transfers to the function body
  • After execution, the control returns back to the calling statement
  • Arguments are the actual values passed during the function call

PHP Program: Function with Parameters and Return Value

<?php
// Function definition with parameters and return value
function calculateArea($length, $width) {
    $area = $length * $width;
    return $area;
}

// Function call with arguments
$result = calculateArea(10, 5);
echo "Area of rectangle = " . $result;

// Another function with parameters and return value
function addNumbers($a, $b) {
    $sum = $a + $b;
    return $sum;
}

$total = addNumbers(25, 35);
echo "<br>Sum = " . $total;
?>

Output:

Area of rectangle = 50
Sum = 60

Explanation of the Program

  • calculateArea($length, $width) — A user-defined function that takes two parameters (length and width)
  • Inside the function, it calculates the area and uses return to send the result back
  • calculateArea(10, 5) — The function is called with arguments 10 and 5
  • The returned value is stored in $result and displayed using echo

Conclusion

Functions are fundamental building blocks in PHP that promote code reusability and modular programming. By using parameters, we pass data into functions, and by using return values, we get processed results back — making our code clean, organized, and efficient.

2.
How can you retrieve form data? Write a PHP program to create a form data that will take a Fahrenheit value as input and displays equivalent Celsius value on submit. The Celsius = (5/9)*(Fahrenheit-32).[10]
3.
What are casting operators? Differentiate for from foreach statement. Write PHP program to illustrate use of for and foreach.[10]
Section B

Short Answers Questions

Attempt any Eight questions.
[8*5=40]
4.
Write a PHP program to parse a CSV file. [5]
5.
Define error handling, error reporting and error suppression. [5]
6.
Write a PHP program to connect database and retrieve data from a table and display in a page. [5]
7.
How can you define class and object in PHP. Illustrate with example. [5]
8.
Write a PHP program that will read two input strings in variable fname and lname and display concatenation of fname and lname. [5]
9.
How can you define variable in PHP. Illustrate with example. [5]
10.
How conversion between array and variables is done? [5]
11.
Write a PHP program to illustrate string patterns using regular expression. [5]
12.
What is session? Write a PHP program for maintaining user data with sessions. [5]