WPB Ut1
WPB Ut1
2 Marks Questions:
Ans: {Global Variable – Defined outside the function and can be accessed anywhere in the script.
Local Variable – Defined inside the function and can only be accessed within that function.}
<html>
<body>
<?php
$globalVar = “I am a global variable”; // Global variable
Function local() {
$localVar = “I am a local variable”; // Local variable
Echo $localVar;
}
local();
echo $globalVar;
?>
</body>
</html>
4 Marks Questions:
b) Associative Array –
i) Uses named keys instead of numeric indexes.
ii) Helpful for storing key-value pairs, such as user data.
iii) Example:
<?php
$person = [“name” => “John”, “age” => 25];
Echo $person[“name”]; // Output: John
?>
c) Multidimensional Array –
i) Contains arrays inside an array.
ii> Used for storing complex data structures, like a table.
iii) Example:
<?php
$students = [
[“John”, 25],
[“Alice”, 22]
];
Echo $students[0][0]; // Output: John
?>
3) State any four decisions making statements along with their syntax in PHP.
Ans: i) Decision-making statements in PHP help control the flow of execution based on
conditions.
ii) Here are four common decision-making statements with their syntax:
1. If Statement
a) Executes a block of code if the condition is true.
b) Syntax:
If (condition) {
// Code to execute if condition is true
}
c)Example:
<?php
$age = 18;
If ($age >= 18) {
Echo “You are eligible to vote.”;
}
?>
2. If-else Statement
a) Executes one block if the condition is true, otherwise executes another block.
b) Syntax:
If (condition) {
// Code if condition is true
} else {
// Code if condition is false
}
c) Example:
<?php
$marks = 45;
If ($marks >= 50) {
Echo “Pass”;
} else {
Echo “Fail”;
}
?>
3. If-elseif-else Statement
a) Used when there are multiple conditions to check.
b) Syntax:
If (condition1) {
// Code if condition1 is true
} elseif (condition2) {
// Code if condition2 is true
} else {
// Code if none of the conditions are true
}
c) Example:
<?php
$score = 75;
If ($score >= 90) {
Echo “Grade A”;
} elseif ($score >= 75) {
Echo “Grade B”;
} else {
Echo “Grade C”;
}
?>
4. Switch Statement
a) Used to execute different blocks of code based on a variable’s value.
b) Syntax:
Switch (expression) {
Case value1:
// Code to execute if expression matches value1
Break;
Case value2:
// Code to execute if expression matches value2
Break;
Default:
// Code if no case matches
}
c) Example:
<?php
$day = “Monday”;
Switch ($day) {
Case “Monday”:
Echo “Start of the week!”;
Break;
Case “Friday”:
Echo “Weekend is near!”;
Break;
Default:
Echo “It’s a regular day.”;
}
?>
$number = 5;
Echo “Factorial of $number is: “ . factorial($number);
?>
</body>
</html>