Web Technology II · Unit 3
Functions and Variable Scope
Exam-focused notes for Functions and Variable Scope (Web Technology II, BIT301): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.
Functions and Variable Scope in short
Functions let us group code under a name and reuse it. This chapter covers how to define functions, pass values, and control where a variable can be seen.
What this unit covers
- Function definition and invocation
- Function parameters and return values
- Parameters by reference
- Global variables and scope
- Static variables and scope
- Anonymous functions and variable functions
- Missing parameters handling
Global variables and scope
2081 · 10 marks
How can you define and declare functions? Differentiate global variable from static variable. Write a PHP program to demonstrate the use of global and static variables.[10]
Parameters by reference
2080 · 10 marks
Describe variables and missing parameters with examples. Write a PHP program to implement functions containing parameters by reference.[10]
Anonymous functions and variable functions
2080 · 5 marks
Differentiate anonymous function from variable function. [5]
Static variables and scope
2079 · 10 marks
What are variables? How can you define static variables? Write a PHP program to illustrate the use of Switch statement.[10]
Show solved answerHide answer
Variables, Static Variables & Switch Statement in PHP
A. Variables in PHP
A variable is a named container used to store data values that can be used and manipulated throughout a program.
Key Rules for PHP Variables:
- All variables begin with the
$sign followed by the variable name - Variable names must start with a letter or underscore (
_) - Variable names cannot start with a number
- Variable names are case-sensitive (
$ageand$Ageare different) - PHP is a loosely typed language, so data types do not need to be declared explicitly
Example:
<?php
$name = "Alice"; // String variable
$age = 20; // Integer variable
$price = 49.99; // Float variable
echo $name;
echo $age;
?>
B. Static Variables
A static variable is a local variable that retains its value between function calls instead of being destroyed when the function exits.
Characteristics:
- Declared using the keyword
staticbefore the variable name - Initialized only once (on the first function call)
- On subsequent calls, it remembers its previous value
- Commonly used for counters and state tracking
Syntax:
static $variable_name = value;
Example:
<?php
function counter() {
static $count = 0; // initialized only once
$count++;
echo "Count = $count ";
}
counter(); // Output: Count = 1
counter(); // Output: Count = 2
counter(); // Output: Count = 3
?>
Here,
$countis not reset to 0 on each call because it is declared asstatic. Withoutstatic, the output would always beCount = 1.
C. PHP Program to Illustrate the Switch Statement
The switch statement evaluates an expression and executes the matching case block. It is a cleaner alternative to multiple if...elseif...else chains.
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:
| Feature | Description |
|---|---|
break | Stops execution and exits the switch block |
Without break | Execution falls through to the next case |
default | Executes when no matching case is found |
| Comparison | Uses loose comparison (==) between expression and cases |
Summary
| Concept | Description |
|---|---|
| Variable | Named container starting with $ to store data |
| Static Variable | Local variable that persists its value across function calls |
| Switch Statement | Multi-branch control structure based on matching case values |
Function definition and invocation
0 · 10 marks
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]
Make Unit 3 stick
Practice BIT301 with flashcards & quizzes