Tribhuwan University

Institute of Science and Technology

2079

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 are variables? How can you define static variables? Write a PHP program to illustrate the use of Switch statement.[10]

What are Variables? Static Variables & Switch Statement in PHP


A. Variables in PHP

Variables are named containers used to store data values in a program. In PHP, all variables start with the $ sign followed by the variable name.

Rules for PHP Variables:

  • A variable starts with $ followed by the name (e.g., $name)
  • Variable name must begin with a letter or underscore (_)
  • Variable name cannot start with a number
  • Variable names are case-sensitive ($age and $Age are different)
  • PHP is a loosely typed language — no need to declare data type explicitly

Example:

<?php
  $name = "John";   // String variable
  $age = 25;        // Integer variable
  $price = 99.5;    // Float variable
?>

B. Static Variables

A static variable is a local variable that retains its value between function calls instead of being destroyed when the function ends.

  • Defined using the keyword static before the variable name
  • It is initialized only once (during the first function call)
  • On subsequent calls, it remembers its previous value
  • Useful for counters and tracking state across function calls

Syntax:

static $variable_name = value;

Example:

<?php
  function counter() {
      static $count = 0;
      $count++;
      echo "Count = $count <br>";
  }

  counter();  // Output: Count = 1
  counter();  // Output: Count = 2
  counter();  // Output: Count = 3
?>

Here, $count is not reset to 0 on each call because it is declared as static.


C. PHP Program to Illustrate Switch Statement

The switch statement is used to perform different actions based on different conditions. It is an alternative to multiple if...elseif...else statements.

Syntax:

switch (expression) {
    case value1:
        // code block
        break;
    case value2:
        // code block
        break;
    default:
        // code block
}

Program:

<?php
  $day = 3;

  switch ($day) {
      case 1:
          echo "Monday";
          break;
      case 2:
          echo "Tuesday";
          break;
      case 3:
          echo "Wednesday";
          break;
      case 4:
          echo "Thursday";
          break;
      case 5:
          echo "Friday";
          break;
      case 6:
          echo "Saturday";
          break;
      case 7:
          echo "Sunday";
          break;
      default:
          echo "Invalid day number";
  }
?>

Output:

Wednesday

Key Points about Switch:

  • The break keyword stops execution and exits the switch block
  • Without break, execution falls through to the next case
  • The default case executes when no matching case is found
  • Switch compares the expression with each case using loose comparison (==)

Conclusion: Variables are fundamental building blocks in PHP for storing data. Static variables provide persistence across function calls, and the switch statement offers a clean and readable way to handle multiple conditional branches.

2.
How can you store string type data in an array? Write a PHP program to illustrate use of extract and compact functions for conversion between arrays and variables.[10]
3.
Consider you have a login form having username, password fields, login and cancel buttons. Now, write a PHP program for implementing login event. The login should be redirected to home.php upon successful login button event. The cancel button should clear the form field values upon its submission.[10]
Section B

Short Answers Questions

Attempt any Eight questions.
[8*5=40]
4.
How can you query a database in PHP? [5]
5.
How can you retrieve the form data? Illustrate with example. [5]
6.
What is error handler? Write a PHP program to show use of error handler to format and print errors. [5]
7.
For the following JSON file named BIT.JSON, write a PHP program to read information from this JSON file: {Name: "Gunadi", Email: "Gunadi@gmail.com", Dob: "January 1", Age: 30} [5]
8.
How are comments written in PHP? Write a PHP program that will read your name and roll number and print the values. Label your program with appropriate comments. [5]
9.
Write a PHP program that will contain a concat string function. The function should have two arguments st1 and st2 of type string and should return st3 which is concatenation of st1 and st2. [5]
10.
Write a PHP program to extract subset of a string array using array_slice() function. [5]
11.
Describe an anonymous class using suitable example. [5]
12.
Write a PHP program for illustrating the starting and ending of session. [5]