BIT301 ยท 2079 ยท 10 marks
What are variables? How can you define static variables? Write a PHP program to illustrate the use of Switch statement.
What are variables? How can you define static variables? Write a PHP program to illustrate the use of Switch statement.[10]
Quick answer
A variable is a named container used to store data values that can be used and manipulated throughout a program.
Variables, Static Variables & Switch Statement in PHP
A. Variables in PHP
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 |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.