Credit:Eli TamangEli Tamang

Tribhuwan University

Institute of Science and Technology

2082

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.
Why is PHP a loosely typed language? How is the for loop different from the foreach loop? Write a PHP program to demonstrate the use of both loops.[10]

Why is PHP a Loosely Typed Language? Difference Between for and foreach Loop with Program


Part A: PHP as a Loosely Typed Language

PHP is called a loosely typed language because it does not require the programmer to declare the data type of a variable. The type is automatically determined at runtime based on the value assigned to it.

Key Points:

  • In PHP, a variable can hold any type of value — integer, string, float, boolean — without explicit type declaration.
  • The same variable can change its type during execution.
  • PHP automatically converts data types as needed (called type juggling).

Example:

$x = 10;       // $x is an integer
$x = "Hello";  // now $x is a string
$x = 3.14;     // now $x is a float

In strongly typed languages like Java or C, you must declare int x = 10; and cannot later assign a string to x. PHP removes this restriction, making it loosely typed.


Part B: Difference Between for Loop and foreach Loop

Feature for Loop foreach Loop
Purpose Used when number of iterations is known Used to iterate over arrays/collections
Syntax Requires initialization, condition, increment Requires only the array variable
Index Control Programmer manually controls the index Automatically accesses each element
Use Case Counting, repetition-based tasks Traversing all elements of an array
Risk May cause infinite loop if condition is wrong Always terminates after last element

Part C: PHP Program Demonstrating Both Loops

<?php
  // Demonstrating for loop
  echo "Using for loop: <br>";
  for($i = 1; $i <= 5; $i++) {
      echo "Number: " . $i . "<br>";
  }

  echo "<br>";

  // Demonstrating foreach loop
  echo "Using foreach loop: <br>";
  $fruits = array("Apple", "Banana", "Mango", "Orange", "Grapes");
  foreach($fruits as $fruit) {
      echo "Fruit: " . $fruit . "<br>";
  }

  echo "<br>";

  // foreach with key-value pairs
  echo "Using foreach with key-value: <br>";
  $marks = array("Math" => 90, "Science" => 85, "English" => 78);
  foreach($marks as $subject => $score) {
      echo $subject . " = " . $score . "<br>";
  }
?>

Output:

Using for loop:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

Using foreach loop:
Fruit: Apple
Fruit: Banana
Fruit: Mango
Fruit: Orange
Fruit: Grapes

Using foreach with key-value:
Math = 90
Science = 85
English = 78

Conclusion

PHP's loosely typed nature makes it flexible and beginner-friendly. The for loop is best for counter-based iteration, while foreach is specifically designed for traversing arrays efficiently without manual index management.

2.
How are indexed arrays different from associative arrays? Write a PHP program to demonstrate the conversion between array and variables.[10]
3.
What are the response headers? Create a PHP login form that contains username, password and a login type field defined by dropdown menu. The login type options can be normal or admin. Now, write a function for implementing the login event. The event should be redirected to admin.php if the login credentials match and if the login type is admin otherwise it should be redirected to hello.php. Create a proper database connection based on your own assumption.[10]
Section B

Short Answers Questions

Attempt any Eight questions.
[8*5=40]
4.
Write a program that will read your name as input string. Access and display the individual characters in the name string. [5]
5.
How can you handle cookies and sessions? Illustrate with examples. [5]
6.
What is anonymous class? Write a program to demonstrate the use of anonymous classes. [5]
7.
What is HTTP authentication? How is it done? [5]
8.
What is the error handler? Write a program to demonstrate error handler function. [5]
9.
Write a program to parse a CSV file and display the content in the file. [5]
10.
Consider you have student records in a database table. Now create a PHP page with delete button action and a text field. The button should delete a record if it matches the name of the student entered in the text field. Use your own assumption for the database table. [5]
11.
Differentiate GET method from POST method with examples. [5]
12.
Write a program to create a class Employee with some attributes and methods. Create an object of the class and display some output using any method in the class. [5]