Mastering PHP: From Basics to Building Dynamic Web Applications

0 0 0 0 0

Chapter 4: Arrays and Functions

🔹 1. Introduction

Arrays and functions are two essential building blocks in PHP.

  • Arrays store multiple values in a single variable — ideal for data like lists, tables, and records.
  • Functions allow you to write reusable, modular blocks of code, improving code efficiency and readability.

This chapter introduces both — starting from basic arrays to building your own functions.


🔹 2. Types of Arrays in PHP

PHP supports three main types of arrays:

1. Indexed Arrays

$colors = ["Red", "Green", "Blue"];

echo $colors[0]; // Red

2. Associative Arrays

$person = [

  "name" => "Alice",

  "age" => 28

];

echo $person["name"]; // Alice

3. Multidimensional Arrays

$users = [

  ["Alice", "alice@gmail.com"],

  ["Bob", "bob@yahoo.com"]

];

echo $users[1][0]; // Bob


🔹 3. Looping Through Arrays

Indexed Array:

$fruits = ["Apple", "Banana", "Mango"];

foreach($fruits as $fruit){

  echo $fruit . "<br>";

}

Associative Array:

$person = ["name" => "John", "age" => 30];

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

  echo "$key: $value<br>";

}


🔹 4. Common Array Functions

Function

Description

count()

Returns number of elements

array_push()

Adds element(s) to end of array

array_pop()

Removes and returns last element

array_merge()

Merges two or more arrays

in_array()

Checks if value exists in array

array_keys()

Returns all keys of an array

sort()

Sorts indexed array

asort()

Sorts associative array by value

ksort()

Sorts associative array by key

Example:

$nums = [5, 3, 8];

sort($nums);

print_r($nums); // [3, 5, 8]


🔹 5. Defining and Using Functions

Basic Syntax:

function greet() {

  echo "Hello!";

}

greet(); // Output: Hello!

Function With Parameters:

function sayHello($name) {

  echo "Hi, $name!";

}

sayHello("Jane");

Default Parameters:

function welcome($name = "Guest") {

  echo "Welcome, $name!";

}

welcome(); // Welcome, Guest!

Return Values:

function add($a, $b){

  return $a + $b;

}

$result = add(3, 5);

echo $result; // 8


🔹 6. Function Scope: Local vs Global

Variables declared inside functions are local to that function.

Example:

$name = "Global";

 

function testScope(){

  $name = "Local";

  echo $name;

}

testScope(); // Local

echo $name;  // Global

Use global keyword:

$x = 5;

$y = 10;

 

function sum(){

  global $x, $y;

  echo $x + $y;

}

sum(); // 15


🔹 7. Anonymous Functions & Callbacks

Assigning functions to variables:

$greet = function($name) {

  return "Hello, $name!";

};

echo $greet("Jake");

Using callbacks in array functions:

$nums = [1, 2, 3];

$squared = array_map(function($n){ return $n*$n; }, $nums);

print_r($squared); // [1, 4, 9]


🔹 8. Summary Table: Arrays & Functions


Feature

Example

Indexed Array

$arr = [1,2,3];

Associative Array

$user = ["name" => "Tom"];

Loop through array

foreach($arr as $val)

Count elements

count($arr)

Define function

function sayHi(){}

Return value

return $val;

Default argument

function foo($x = 1)

Anonymous function

$fn = function($x){ return $x; };

Back

FAQs


1. What is PHP used for?

PHP is primarily used for creating dynamic web pages and server-side applications such as login systems, e-commerce platforms, and CMSs.

2. Is PHP still relevant in 2025?

Absolutely. PHP continues to power most of the web and is essential in WordPress, Laravel, and web hosting environments.

3. Do I need to install anything to run PHP?

Yes — you can install XAMPP, MAMP, Laragon, or PHP CLI for local development.

4. What databases work with PHP?

MySQL is the most commonly used with PHP, but it also supports PostgreSQL, SQLite, and others.

5. What’s the difference between PHP and JavaScript?

PHP is a server-side scripting language, while JavaScript is primarily client-side, running in the browser.

6. Can I use PHP with HTML?

Yes PHP is often embedded inside HTML to create dynamic pages

7. What is a PHP file extension?

PHP files have the .php extension and are executed on the server.

8. How do I send data from a form to PHP?

Use method="POST" or method="GET" in your form and access data in PHP using $_POST or $_GET.

9. Is PHP good for building APIs?

Yes — PHP can be used to build RESTful APIs, especially with frameworks like Laravel or Slim.

10. What are some popular PHP frameworks?

Laravel, Symfony, CodeIgniter, Zend, and Slim are among the most used PHP frameworks.