2079 1

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]

Web Technology IIChapter 3: Functions and Variable ScopeStatic variables and scope

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 ($age and $Age are 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 static before 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, $count is not reset to 0 on each call because it is declared as static. Without static, the output would always be Count = 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:

FeatureDescription
breakStops execution and exits the switch block
Without breakExecution falls through to the next case
defaultExecutes when no matching case is found
ComparisonUses loose comparison (==) between expression and cases

Summary

ConceptDescription
VariableNamed container starting with $ to store data
Static VariableLocal variable that persists its value across function calls
Switch StatementMulti-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.