[go: up one dir, main page]

0% found this document useful (0 votes)
12 views5 pages

Unit 1 Solution 2025

The document outlines the advantages of PHP, including its open-source nature, large community support, and cross-platform compatibility. It also details various data types in PHP, syntax for loops, and functions for string manipulation, as well as examples of Fibonacci series programs and methods for array manipulation. Additionally, it explains indexed and associative arrays, variable-length argument lists, and provides code examples for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Unit 1 Solution 2025

The document outlines the advantages of PHP, including its open-source nature, large community support, and cross-platform compatibility. It also details various data types in PHP, syntax for loops, and functions for string manipulation, as well as examples of Fibonacci series programs and methods for array manipulation. Additionally, it explains indexed and associative arrays, variable-length argument lists, and provides code examples for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Q1) any four 4 a) Advantages of PHP:

 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.

b) Data Types Used in PHP:

 String: Represents a sequence of characters (e.g., "Hello, world!").


 Integer: Represents whole numbers (e.g., 10, -5, 0).
 Float (or Double): Represents floating-point numbers (numbers with decimal points,
e.g., 3.14, -0.001).
 Boolean: Represents either true or false.
 Array: Stores a collection of values.
 Object: Stores data and information that represents an instance of a class.
 NULL: Represents a variable with no value.
 Resource: Represents an external resource, such as a database connection.

c) Syntax of foreach Loop:

PHP
foreach (array as $value) {
// Code to be executed for each value
}

// Or, to access both keys and values:

foreach (array as $key => $value) {


// Code to be executed for each key-value pair
}

d) Multi-dimensional Array and Data Storage:

 Multi-dimensional Array: A multi-dimensional array is an array containing one or


more arrays as its elements. It's used to represent data with multiple levels of
organization (like tables or matrices).
 Storing Data:

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

Or using associative arrays.

PHP

$multiArray = array(
array("name" => "John", "age" => 30, "job" => "Engineer"),
array("name" => "Jane", "age" => 25, "job" => "Designer"),
array("name" => "Peter", "age" => 35, "job" => "Manager")
);

echo $multiArray[0]["name"]; // Output: John


echo $multiArray[1]["job"]; // Output: Designer

e) Difference Between Implode and Explode Functions:

 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.

f) Four String Functions in PHP:

 strlen(): Returns the length of a string.


o Example: strlen("Hello"); returns 5.
 strpos(): Finds the position of the first occurrence of a substring in a string.
o Example: strpos("Hello world", "world"); returns 6.
 strtolower(): Converts a string to lowercase.
o Example: strtolower("HELLO"); returns "hello".
 strtoupper(): Converts a string to uppercase.
o Example: strtoupper("hello"); returns "HELLO".
 trim(): Removes whitespace from the beginning and end of a string.
o Example: trim(" hello "); returns "hello"
 str_replace(): Replaces all occurrences of a search string with a replacement string.
o Example: str_replace("old","new","This is old"); returns "This is new"
Q 2) Any three Program to Print Fibonacci Series in PHP:

PHP
<?php

function fibonacci($n) {
$num1 = 0;
$num2 = 1;

echo "Fibonacci Series up to $n: ";

for ($i = 0; $i < $n; $i++) {


echo $num1 . " ";
$num3 = $num1 + $num2;
$num1 = $num2;
$num2 = $num3;
}
}

fibonacci(10); // Prints the first 10 Fibonacci numbers


?>

b) Use of PHP Methods:

 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

$colors = array("red", "green", "blue", "yellow");


array_splice($colors, 1, 2, array("purple", "orange"));
// $colors becomes array("red", "purple", "orange", "yellow")

 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

$cars = array("Volvo", "BMW", "Toyota");


sort($cars);
// $cars becomes array("BMW", "Toyota", "Volvo")

c) Program to Double a Number Using Pass by Reference:

PHP
<?php

function doubleNumber(&$num) {
$num *= 2;
}

$number = 5;
doubleNumber($number);
echo "Doubled number: " . $number; // Output: Doubled number: 10

?>

d) Indexed and Associative Arrays:

 Indexed Arrays:
o Arrays with numeric indexes.
o Indexes start from 0 by default.
o Example:

PHP

$fruits = array("apple", "banana", "orange");


echo $fruits[0]; // Output: apple

 Associative Arrays:
o Arrays with named keys (strings).
o Keys can be any string.
o Example:

PHP

$student = array("name" => "Alice", "age" => 20, "major" =>


"Computer Science");
echo $student["name"]; // Output: Alice

e) Variable-Length Argument Lists Using ... Token:


 The ... token (also known as the "spread" or "rest" operator) allows a function to
accept a variable number of arguments.
 These arguments are captured as an array within the function.
 Example:

PHP

<?php
function sum(...$numbers) {
$total = 0;
foreach ($numbers as $number) {
$total += $number;
}
return $total;
}

echo sum(1, 2, 3); // Output: 6


echo sum(10, 20, 30, 40); // Output: 100
?>

In this example, $numbers inside the sum function recieves all of the arguments
passed to the function as an array.

You might also like