Semester
Subject
Year
Tribhuwan University
2079
Bachelor Level / Third Year / Fifth Semester / Science
(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.
Long Answers Questions
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:
$ followed by the name (e.g., $name)_)$age and $Age are different)Example:
<?php
$name = "John"; // String variable
$age = 25; // Integer variable
$price = 99.5; // Float variable
?>
A static variable is a local variable that retains its value between function calls instead of being destroyed when the function ends.
static before the variable nameSyntax:
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.
The switch statement is used to perform different actions based on different conditions. It is an alternative to multiple
if...elseif...elsestatements.
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:
break keyword stops execution and exits the switch blockbreak, execution falls through to the next casedefault case executes when no matching case is found==)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.
Short Answers Questions