Day 6-7: PHP Functions and Arrays
Functions in PHP
Functions are blocks of code that can be repeatedly called and executed. They help in organizing code into modular, reusable components.
1. Defining a Function
A function is defined using the function
keyword, followed by the function name, parentheses, and curly braces containing the code to be executed.
[mycode]
<?php
// Define a function named sayHello
function sayHello() {
echo “Hello, World!”;
}
// Call the function
sayHello(); // Output: Hello, World!
?>
[/mycode]
2. Function with Parameters
Functions can accept parameters, which are variables passed to the function when it is called.
[mycode]
<?php
// Define a function that takes a name as a parameter
function greet($name) {
echo “Hello, $name!”;
}
// Call the function with different arguments
greet(“Amit”); // Output: Hello, Amit!
greet(“Biswas”); // Output: Hello, Biswas!
?>
[/mycode]
3. Function with Return Value
Functions can return a value using the return
statement.
[mycode]
<?php
// Define a function that adds two numbers and returns the result
function add($a, $b) {
return $a + $b;
}
// Call the function and store the result
$sum = add(10, 20);
echo “Sum: $sum”; // Output: Sum: 30
?>
[/mycode]
Arrays in PHP
Arrays are used to store multiple values in a single variable. PHP supports different types of arrays, including indexed arrays, associative arrays, and multidimensional arrays.
1. Indexed Arrays
Indexed arrays use numeric indexes to access their elements.
[mycode]
<?php
// Define an indexed array
$fruits = array(“Apple”, “Banana”, “Cherry”);
// Access elements using their index
echo $fruits[0]; // Output: Apple
echo $fruits[1]; // Output: Banana
echo $fruits[2]; // Output: Cherry
// Loop through the array using a foreach loop
foreach ($fruits as $fruit) {
echo $fruit . “<br>”;
}
?>
[/mycode]
2. Associative Arrays
Associative arrays use named keys to access their elements.
[mycode]
<?php
// Define an associative array
$person = array(“name” => “Amit”, “age” => 25, “city” => “Kolkata”);
// Access elements using their keys
echo “Name: ” . $person[“name”] . “<br>”; // Output: Name: Amit
echo “Age: ” . $person[“age”] . “<br>”; // Output: Age: 25
echo “City: ” . $person[“city”] . “<br>”; // Output: City: Kolkata
// Loop through the array using a foreach loop
foreach ($person as $key => $value) {
echo “$key: $value <br>”;
}
?>
[/mycode]
3. Multidimensional Arrays
Multidimensional arrays contain one or more arrays.
[mycode]
<?php
// Define a multidimensional array
$students = array(
array(“name” => “Amit”, “age” => 25),
array(“name” => “Biswas”, “age” => 23),
array(“name” => “Rahul”, “age” => 22)
);
// Access elements using their indexes
echo $students[0][“name”]; // Output: Amit
echo $students[1][“name”]; // Output: Biswas
echo $students[2][“name”]; // Output: Rahul
// Loop through the multidimensional array
foreach ($students as $student) {
echo “Name: ” . $student[“name”] . “, Age: ” . $student[“age”] . “<br>”;
}
?>
[/mycode]
Example: Combining Functions and Arrays
Let’s create a function that calculates the average age of a group of people using an array.
[mycode]
<?php
// Define an array of people with their ages
$people = array(
array(“name” => “Amit”, “age” => 25),
array(“name” => “Biswas”, “age” => 23),
array(“name” => “Rahul”, “age” => 22),
array(“name” => “Sita”, “age” => 28)
);
// Define a function to calculate the average age
function calculateAverageAge($people) {
$totalAge = 0;
$count = count($people);
// Loop through the array to sum the ages
foreach ($people as $person) {
$totalAge += $person[“age”];
}
// Calculate the average age
$averageAge = $totalAge / $count;
return $averageAge;
}
// Call the function and display the average age
$averageAge = calculateAverageAge($people);
echo “Average Age: ” . $averageAge; // Output: Average Age: 24.5
?>
[/mycode]
Next Steps
Practice creating and using functions, as well as working with different types of arrays. In the next section, we’ll cover PHP form handling and working with data from forms.