Short overview on Unit-III
- Get link
- X
- Other Apps
📘 Unit – III: Working with PHP Arrays and Functions
3a. Steps to Use Different Types of Array in a Given Application
🔹 What is an Array?
-
An array stores multiple values in a single variable.
-
Declared using
array()or short syntax[].
Types of Arrays in PHP
1. Indexed Array
-
Elements are stored with numeric indexes (0, 1, 2…).
-
Example:
<?php
$fruits = array("Apple", "Banana", "Mango");
echo $fruits[0]; // Apple
?>
✅ Steps to use in application:
-
Declare array with values.
-
Access elements by index.
-
Use
fororforeachloop to process values.
Example (display all fruits):
foreach($fruits as $f){
echo $f."<br>";
}
2. Associative Array
-
Uses key-value pairs instead of numeric index.
-
Example:
<?php
$marks = array("Jay" => 85, "Deep" => 90, "Amit" => 78);
echo $marks["Deep"]; // 90
?>
✅ Steps to use:
-
Declare associative array with keys.
-
Access value using its key.
-
Loop with
foreach($array as $key => $value).
Example:
foreach($marks as $name => $score){
echo "$name scored $score <br>";
}
3. Multidimensional Array
-
Array of arrays (table-like data).
-
Example:
<?php
$students = array(
array("Jay", 21, "A"),
array("Deep", 22, "B"),
array("Amit", 20, "A")
);
echo $students[1][0]; // Deep
?>
✅ Steps to use:
-
Create nested arrays.
-
Access values using row & column index.
-
Use nested loops to display data.
Example (display all students):
foreach($students as $stu){
echo $stu[0]." - Age: ".$stu[1]." - Grade: ".$stu[2]."<br>";
}
📌 Application Example: Student Marks Application
-
Indexed Array: Store subject names.
-
Associative Array: Store student names with marks.
-
Multidimensional Array: Store multiple students with their marks in different subjects.
3b. Steps to Create User-Defined Functions & Work with Built-in Functions
🔹 Functions in PHP
-
A function is a block of code that can be reused.
-
Two types:
-
User-defined functions
-
Built-in functions
-
1. User-Defined Functions
✅ Steps to create:
-
Declare function using
functionkeyword. -
Write code inside it.
-
Call function when needed.
Example 1: Simple Function
<?php
function greet(){
echo "Welcome to PHP Functions!";
}
greet(); // Calling function
?>
Example 2: Function with Parameters
function add($a, $b){
return $a + $b;
}
echo add(10, 20); // Output: 30
Example 3: Function with Default Argument
function greetUser($name="Guest"){
echo "Hello, $name!";
}
greetUser("Jay"); // Hello, Jay!
greetUser(); // Hello, Guest!
2. Built-in Functions in PHP
PHP has 1000+ built-in functions. Categories:
(a) String Functions
-
strlen($str)→ length of string -
strtoupper($str)→ converts to uppercase -
strtolower($str)→ converts to lowercase -
substr($str, start, length)→ substring
Example:
$name = "php tutorial";
echo strtoupper($name); // PHP TUTORIAL
(b) Array Functions
-
count($arr)→ number of elements -
sort($arr)→ sort ascending -
rsort($arr)→ sort descending -
array_merge($a1, $a2)→ combine arrays
Example:
$fruits = ["Mango", "Apple", "Banana"];
sort($fruits);
print_r($fruits);
(c) Math Functions
-
abs(-7)→ 7 -
pow(2,3)→ 8 -
sqrt(16)→ 4 -
rand(1,100)→ random number
(d) Date & Time Functions
-
date("Y-m-d")→ 2025-08-18 -
date("H:i:s")→ 14:55:22
Example:
echo "Today is ".date("l"); // Outputs day of week
(e) Other Useful Functions
-
isset($var)→ checks if variable exists -
empty($var)→ checks if empty -
include()/require()→ include external PHP file
📌 Application Example: Online Student Report
-
Store marks in arrays.
-
Create user-defined function
calculateAverage($marks). -
Use built-in functions like
count()for number of subjects,max()to find highest marks. -
Display report using arrays + functions.
Example:
<?php
function calculateAverage($marks){
return array_sum($marks) / count($marks);
}
$student = array("Math"=>80, "Science"=>90, "English"=>85);
echo "Average: ".calculateAverage($student);
echo "<br> Highest Marks: ".max($student);
?>
✅ That covers Unit–III: Arrays & Functions in PHP.
Would you like me to also prepare MCQs + 3–4 marks questions with answers + diagram-based short notes PDF for Unit–III (like we did for Unit–II)?
✅ 1. Multiple Choice Questions (MCQ)
Q1. Which function returns the total number of elements in an array?
a) size()
b) count()
c) length()
d) sizeof()
👉 Answer: b) count()
Q2. Which type of array uses keys instead of numeric indexes?
a) Indexed Array
b) Associative Array
c) Multidimensional Array
d) None
👉 Answer: b) Associative Array
Q3. Which of the following is correct for defining a PHP function?
a) function myFunc{}
b) def myFunc()
c) function myFunc(){}
d) func myFunc(){}
👉 Answer: c) function myFunc(){}
Q4. Which built-in function is used to merge two arrays?
a) array_combine()
b) array_merge()
c) merge_array()
d) join_array()
👉 Answer: b) array_merge()
Q5. PHP arrays start with which index by default?
a) 0
b) 1
c) -1
d) Random
👉 Answer: a) 0
Q6. Which of these is a multidimensional array?
a) $a = array(10,20,30);
b) $a = array("x"=>1,"y"=>2);
c) $a = array(array(1,2), array(3,4));
d) $a = array(“A”, “B”, “C”);
👉 Answer: c) array(array(1,2), array(3,4))
Q7. Which function is used to check if a variable is empty?
a) isset()
b) check()
c) empty()
d) is_null()
👉 Answer: c) empty()
Q8. Which function returns the length of a string?
a) strlen()
b) length()
c) strsize()
d) count()
👉 Answer: a) strlen()
Q9. Which of the following functions returns the largest number from an array?
a) max()
b) largest()
c) big()
d) maximum()
👉 Answer: a) max()
Q10. Which loop is most commonly used to traverse arrays in PHP?
a) for
b) foreach
c) while
d) do-while
👉 Answer: b) foreach
Q11. What is the default return value of a function in PHP if no return is specified?
a) 0
b) NULL
c) False
d) Undefined
👉 Answer: b) NULL
Q12. Which function is used to generate a random number in PHP?
a) rand()
b) random()
c) shuffle()
d) gen_random()
👉 Answer: a) rand()
Q13. Which symbol is used to define variables in PHP?
a) #
b) @
c) $
d) &
👉 Answer: c) $
Q14. Which built-in function is used to convert a string to lowercase?
a) strtolower()
b) strlower()
c) lower()
d) toLower()
👉 Answer: a) strtolower()
Q15. PHP function names are:
a) Case-sensitive
b) Not case-sensitive
c) Always uppercase
d) Always lowercase
👉 Answer: b) Not case-sensitive
✅ 2. 3–4 Marks Questions with Answers
Q1. Explain the difference between Indexed, Associative, and Multidimensional arrays in PHP with examples.
👉 Indexed arrays use numeric indexes, Associative arrays use named keys, and Multidimensional arrays are arrays inside arrays. Example:
$indexed = ["Red", "Green", "Blue"];
$assoc = ["Math"=>90, "Science"=>85];
$multi = [["Amit",21], ["Deep",22]];
Q2. Write the steps to create and call a user-defined function in PHP.
👉 Steps:
-
Use
functionkeyword. -
Write function name and code block.
-
Call function using its name.
Example:
function greet($name){
echo "Hello $name!";
}
greet("Jay");
Q3. Describe any four string functions in PHP with examples.
-
strlen("PHP")→ 3 -
strtoupper("php")→ "PHP" -
strtolower("HELLO")→ "hello" -
substr("HelloWorld",0,5)→ "Hello"
Q4. Differentiate between User-defined and Built-in functions in PHP.
-
User-defined: Written by programmer, reusable, e.g.,
function add($a,$b){ return $a+$b; } -
Built-in: Predefined in PHP, e.g.,
count(), strlen(), rand()
Q5. Explain with example how to use foreach loop to traverse an associative array.
👉 Example:
$marks = ["Jay"=>85,"Deep"=>90];
foreach($marks as $name=>$score){
echo "$name scored $score <br>";
}
Q6. Write steps to calculate average marks using arrays and functions.
👉 Steps:
-
Store marks in associative array.
-
Create function to calculate average using
array_sum()andcount(). -
Display result.
Example:
function avg($marks){
return array_sum($marks)/count($marks);
}
Q7. Explain the use of isset() and empty() with examples.
-
isset($var)→ checks if variable exists. -
empty($var)→ checks if variable has no value.
Example:
$x = "";
echo isset($x); // true
echo empty($x); // true- Get link
- X
- Other Apps
Comments
Post a Comment