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.
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
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]
--- A function is a block of reusable code that performs a specific task. In PHP, functions are defined using the function keyword. - Function names are case-insensitive - Function names must start with a letter or underscore - Parameters are optional - The...
Full solved answer →Parameters by reference
Describe variables and missing parameters with examples. Write a PHP program to implement functions containing parameters by reference.[10]
--- A variable in PHP is a named storage location that holds a value. PHP variables: - Start with a $ sign followed by the variable name - Are loosely typed (no need to declare data type) - Are case-sensitive Output: --- When a function is called without pa...
Full solved answer →Anonymous functions and variable functions
Differentiate anonymous function from variable function. [5]
An anonymous function (also called a closure) is a function that is defined without a name. It can be assigned to a variable, passed as an argument, or returned from another function. Syntax: - The function itself has no identifier/name. - It is defined usi...
Full solved answer →Static variables and scope
What are variables? How can you define static variables? Write a PHP program to illustrate the use of Switch statement.[10]
A variable is a named container used to store data values that can be used and manipulated throughout a program. - All variables begin with the $ sign followed by the variable name - Variable names must start with a letter or underscore () - Variable names ...
Full solved answer →Function definition and invocation
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]
A function is a reusable block of code that performs a specific task. It can accept input values (called parameters) and can send back a result (called a return value) to the calling code. Key benefits of using functions: - Code reusability - write once, ca...
Full solved answer →Make Unit 3 stick
Practice BIT301 with flashcards & quizzes