Unit 1 Solution 2025
Unit 1 Solution 2025
Open Source and Free: PHP is open-source, meaning it's free to use, distribute, and
modify. This reduces development costs.
Large Community and Support: A vast community of developers provides
extensive documentation, tutorials, and support forums, making it easy to find help.
Cross-Platform Compatibility: PHP runs on various operating systems (Windows,
Linux, macOS) and web servers (Apache, Nginx), offering flexibility.
Database Integration: PHP seamlessly integrates with numerous databases,
including MySQL, PostgreSQL, and Oracle, making it ideal for dynamic websites.
Relatively Easy to Learn: PHP's syntax is relatively straightforward, making it
accessible to beginners.
Large number of frameworks: Many frameworks like Laravel, Symfony, and
CodeIgniter exist to speed up development.
Efficient and Fast: PHP is generally efficient and can handle a large number of
requests, especially when optimized.
PHP
foreach (array as $value) {
// Code to be executed for each value
}
PHP
$multiArray = array(
array("John", 30, "Engineer"),
array("Jane", 25, "Designer"),
array("Peter", 35, "Manager")
);
// Accessing data:
echo $multiArray[0][0]; // Output: John
echo $multiArray[1][2]; // Output: Designer
PHP
$multiArray = array(
array("name" => "John", "age" => 30, "job" => "Engineer"),
array("name" => "Jane", "age" => 25, "job" => "Designer"),
array("name" => "Peter", "age" => 35, "job" => "Manager")
);
Explode():
o Splits a string into an array using a specified delimiter.
o Syntax: explode(delimiter, string)
o Example: explode(",", "apple,banana,orange"); returns
array("apple", "banana", "orange").
Implode():
o Joins array elements into a string using a specified delimiter.
o Syntax: implode(delimiter, array)
o Example: implode("-", array("red", "green", "blue")); returns
"red-green-blue".
In short, Explode converts a string to an array, and Implode converts an array to a
string.
PHP
<?php
function fibonacci($n) {
$num1 = 0;
$num2 = 1;
I) array_splice():
o Modifies an array by removing a portion and replacing it with something else.
o Syntax: array_splice(array, offset, length, replacement)
o Example:
PHP
II) ksort():
o Sorts an associative array by keys in ascending order.
o Syntax: ksort(array)
o Example:
PHP
$age = array("Peter" => "35", "Ben" => "37", "Joe" => "43");
ksort($age);
// $age becomes array("Ben" => "37", "Joe" => "43", "Peter" =>
"35")
III) var_dump():
o Displays structured information about one or more variables, including their
type and value. Useful for debugging.
o Syntax: var_dump(variable)
o Example:
PHP
$myVar = array(1, "hello", true);
var_dump($myVar);
IV) sort():
o Sorts an indexed array in ascending order. The keys are re-indexed.
o Syntax: sort(array)
o Example:
PHP
PHP
<?php
function doubleNumber(&$num) {
$num *= 2;
}
$number = 5;
doubleNumber($number);
echo "Doubled number: " . $number; // Output: Doubled number: 10
?>
Indexed Arrays:
o Arrays with numeric indexes.
o Indexes start from 0 by default.
o Example:
PHP
Associative Arrays:
o Arrays with named keys (strings).
o Keys can be any string.
o Example:
PHP
PHP
<?php
function sum(...$numbers) {
$total = 0;
foreach ($numbers as $number) {
$total += $number;
}
return $total;
}
In this example, $numbers inside the sum function recieves all of the arguments
passed to the function as an array.