πŸ’‘ Jump to Important Questions ↓

Important Questions

Important Questions

Introduction

Asked in 2082Long Question10 Marks
1.
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]

Why is PHP a Loosely Typed Language? Difference Between for and foreach Loop with Program


Part A: PHP as a Loosely Typed Language

PHP is called a loosely typed language because it does not require the programmer to declare the data type of a variable. The type is automatically determined at runtime based on the value assigned to it.

Key Points:

  • In PHP, a variable can hold any type of value β€” integer, string, float, boolean β€” without explicit type declaration.
  • The same variable can change its type during execution.
  • PHP automatically converts data types as needed (called type juggling).

Example:

$x = 10;       // $x is an integer
$x = "Hello";  // now $x is a string
$x = 3.14;     // now $x is a float

In strongly typed languages like Java or C, you must declare int x = 10; and cannot later assign a string to x. PHP removes this restriction, making it loosely typed.


Part B: Difference Between for Loop and foreach Loop

Feature for Loop foreach Loop
Purpose Used when number of iterations is known Used to iterate over arrays/collections
Syntax Requires initialization, condition, increment Requires only the array variable
Index Control Programmer manually controls the index Automatically accesses each element
Use Case Counting, repetition-based tasks Traversing all elements of an array
Risk May cause infinite loop if condition is wrong Always terminates after last element

Part C: PHP Program Demonstrating Both Loops

<?php
  // Demonstrating for loop
  echo "Using for loop: <br>";
  for($i = 1; $i <= 5; $i++) {
      echo "Number: " . $i . "<br>";
  }

  echo "<br>";

  // Demonstrating foreach loop
  echo "Using foreach loop: <br>";
  $fruits = array("Apple", "Banana", "Mango", "Orange", "Grapes");
  foreach($fruits as $fruit) {
      echo "Fruit: " . $fruit . "<br>";
  }

  echo "<br>";

  // foreach with key-value pairs
  echo "Using foreach with key-value: <br>";
  $marks = array("Math" => 90, "Science" => 85, "English" => 78);
  foreach($marks as $subject => $score) {
      echo $subject . " = " . $score . "<br>";
  }
?>

Output:

Using for loop:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

Using foreach loop:
Fruit: Apple
Fruit: Banana
Fruit: Mango
Fruit: Orange
Fruit: Grapes

Using foreach with key-value:
Math = 90
Science = 85
English = 78

Conclusion

PHP's loosely typed nature makes it flexible and beginner-friendly. The for loop is best for counter-based iteration, while foreach is specifically designed for traversing arrays efficiently without manual index management.

Asked in 2081Short Question5 Marks
2.
Describe the date time functions with examples. [5]
Asked in 2081Long Question10 Marks
3.
What is server side scripting? How can you define and declare variables in PHP? Write PHP programs to demonstrate concepts of while and do while loops. [10]
Asked in 2080Long Question10 Marks
4.
How can you embed PHP code in web pages? Write a PHP program to illustrate the concepts of continue and break statements. [10]
Asked in 2079Short Question5 Marks
5.
How are comments written in PHP? Write a PHP program that will read your name and roll number and print the values. Label your program with appropriate comments. [5]
Asked in 2079Long Question10 Marks
6.
What are variables? How can you define static variables? Write a PHP program to illustrate the use of Switch statement. [10]
ModelShort Question5 Marks
7.
How can you define variable in PHP. Illustrate with example. [5]