BIT301 · 2082 · 10 marks
Why is PHP a loosely typed language? How is the for loop different from the foreach loop? Write a PHP program to demonstrate the use of both loops.
Why is PHP a loosely typed language? How is the for loop different from the foreach loop? Write a PHP program to demonstrate the use of both loops.[10]
Quick answer
PHP is called a loosely typed (or dynamically typed) language because: Variables do not require explicit data type declaration; The data type of a variable is automatically determined at runtime based on the value assigned to it; The same variable can hold different types of values at different points...
PHP: Loosely Typed Language, for vs foreach Loops
Part A: Why is PHP a Loosely Typed Language?
PHP is called a loosely typed (or dynamically typed) language because:
- Variables do not require explicit data type declaration
- The data type of a variable is automatically determined at runtime based on the value assigned to it
- The same variable can hold different types of values at different points in the program
- PHP performs automatic type conversion (known as type juggling) when needed in expressions
Example Illustrating Loose Typing:
<?php
$x = 10; // $x is integer
$x = "Hello"; // $x is now a string (no error)
$x = 3.14; // $x is now a float
// Type juggling example
$result = "5" + 10; // PHP automatically converts "5" to integer
echo $result; // Output: 15
?>
Contrast: In strongly typed languages like Java or C, you must declare
int x = 10;and assigning a string toxlater causes a compile-time error. PHP removes this restriction entirely.
Part B: Difference Between for Loop and foreach Loop
| Feature | for Loop | foreach Loop |
|---|---|---|
| Purpose | Used when the number of iterations is known in advance | Used to iterate over arrays or collections |
| Syntax | Requires initialization, condition, and increment | Requires only the array and a loop variable |
| Index Control | Programmer manually manages the index | Index/key is handled automatically |
| Use Case | Counter-based or numeric repetition tasks | Traversing all elements of an array |
| Risk | Can cause infinite loop if condition is wrong | Always terminates after the last element |
| Key-Value Access | Not directly supported | Supports key => value pair access |
Part C: PHP Program Demonstrating Both Loops
<?php
// -----------------------------------------------
// 1. Demonstrating for loop
// -----------------------------------------------
echo "Using for loop: ";
for ($i = 1; $i <= 5; $i++) {
echo "Number: " . $i . " ";
}
echo " ";
// -----------------------------------------------
// 2. Demonstrating foreach loop (indexed array)
// -----------------------------------------------
echo "Using foreach loop (indexed array): ";
$fruits = array("Apple", "Banana", "Mango", "Orange", "Grapes");
foreach ($fruits as $fruit) {
echo "Fruit: " . $fruit . " ";
}
echo " ";
// -----------------------------------------------
// 3. Demonstrating foreach loop (associative array)
// -----------------------------------------------
echo "Using foreach loop (key => value): ";
$marks = array("Math" => 90, "Science" => 85, "English" => 78);
foreach ($marks as $subject => $score) {
echo $subject . " : " . $score . " ";
}
?>
Expected Output:
Using for loop:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Using foreach loop (indexed array):
Fruit: Apple
Fruit: Banana
Fruit: Mango
Fruit: Orange
Fruit: Grapes
Using foreach loop (key => value):
Math : 90
Science : 85
English : 78
Summary
| Point | Detail |
|---|---|
| Loosely Typed | No type declaration needed; type determined at runtime; type juggling occurs automatically |
| for loop | Best for counter-based iteration where the number of repetitions is known |
| foreach loop | Best for traversing arrays; supports both indexed and associative arrays cleanly |
PHP's loosely typed nature makes development faster and more flexible, while the foreach loop simplifies array traversal by eliminating the need for manual index management.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.