Q1.
Explain PHP data types, variables, constants, comments, and super global variables with suitable
examples.
1. Data Types: PHP is a loosely typed language. It supports:
String: "Hello World"
Integer: 100
Float: 10.5
Boolean: true or false
Array: array("A", "B")
2. Variables & Constants:
Variables: Declared with a $ sign. (e.g., $name = "Ashmit";)
Constants: Defined using define() or const. They cannot be changed once defined. (e.g.,
define("COLLEGE", "Tecnia");)
3. Comments:
Single-line: // or #
Multi-line: /* ... */
4. Super Global Variables: These are built-in variables available in all scopes:
$_GET / $_POST: Collects form data.
$_SESSION: Stores user information across pages.
$_SERVER: Contains server and execution environment info.
Q2.
Write a PHP program demonstrating operators and expressions.
PHP
<?php
$a = 20; $b = 10;
// Arithmetic Operators
echo "Addition: " . ($a + $b);
// Relational Operators
if ($a > $b) { echo "A is greater than B"; }
// Logical Operators
if ($a == 20 && $b == 10) { echo "Both conditions are True"; }
?>
Q3.
Develop a PHP program using decision making statements.
PHP
<?php
$time = 10;
// If-Elseif-Else
if ($time < 12) { echo "Good Morning"; }
elseif ($time < 17) { echo "Good Afternoon"; }
else { echo "Good Evening"; }
// Switch Case
$color = "blue";
switch ($color) {
case "red": echo "Color is Red"; break;
case "blue": echo "Color is Blue"; break;
default: echo "Color not found";
?>
Q4.
Write PHP programs using loops (for, while, do-while, foreach).
PHP
<?php
// For Loop - Displaying numbers 1 to 5
for ($i = 1; $i <= 5; $i++) { echo $i . " "; }
// Foreach Loop - Iterating an array
$apps = array("VS Code", "XAMPP", "Browser");
foreach ($apps as $val) { echo $val . " "; }
?>
Q5.
Create and explain Indexed Arrays and Associative Arrays.
Indexed Array: Uses a numeric index.
o Example: $subjects = array("C", "Java", "PHP");
Associative Array: Uses named keys.
o Example: $student = array("Name"=>"Ashmit", "Age"=>19);
Q6.
Multi-dimensional Array and five predefined array functions.
PHP
<?php
$matrix = array(
array(1, 2),
array(3, 4)
);
// Predefined Functions:
// 1. count($matrix) - Returns length
// 2. array_push($arr, $val) - Adds to end
// 3. array_pop($arr) - Removes last element
// 4. sort($arr) - Sorts array
// 5. in_array("val", $arr) - Checks if value exists
?>
Q7.
Explain Regular Expressions in PHP and advantages of PHP.
Regular Expressions (RegEx): Patterns used for pattern-matching and string manipulation. Common
functions include preg_match() and preg_replace().
Advantages of PHP:
1. Open Source: Free to use and distribute.
2. Platform Independent: Runs on Windows, Linux, and Unix.
3. Easy Integration: Works seamlessly with MySQL and Apache.
4. Large Community: Extensive documentation and support for BCA students.