Functions and Arrays in PHP

Chapter 5 · Tamil Nadu State Board

  • 6 min read
  • 5-question quiz

Every key point below shows the sentence from your textbook it comes from. How we check content

In short

This lesson explains how functions help organise and reuse PHP code, and how programmers define and call their own functions. It also teaches that arrays store many values under one variable name and describes indexed, associative, and multidimensional arrays. Knowing how to create and access these structures helps students work with PHP data.

Written by AI from this lesson. How we check it

Key points

  • A function is a block of code for a particular task, and it can help reuse code, test and debug, and organise programs into smaller parts.
    From your textbook
    “Functions allow us to reuse code, which can save us time and effort”
  • PHP provides built-in functions and functions that programmers define themselves.
    From your textbook
    “These are functions that are pre-defined in the PHP language”
  • A user-defined function uses the function keyword, followed by a valid function name and parentheses; parameters may be included.
    From your textbook
    “We can define a user defined function by using the ‘function’ keyword”
  • A function call executes the function, and arguments can be placed inside its parentheses.
    From your textbook
    “We can also pass arguments to a function by including them within the parentheses”
  • An array stores multiple values under one variable name, and its elements can have different data types.
    From your textbook
    “It can hold many values under a single variable name.”
  • An indexed array uses numeric indexes, starting at 0, and its elements can be accessed with an index in square brackets.
    From your textbook
    “The index is a number that starts at 0 for the first element”
  • Associative arrays use keys to identify values, while multidimensional arrays contain arrays as elements.
    From your textbook
    “a multidimensional array is an array that contains one or more arrays as its elements.”
Test yourself: 5 questions

Formulas in this chapter

  • Function definitionfunction functionName([parameter list])Explained here
  • Calling a functionfunctionName( ); (or) functionName(argument list);Explained here
  • Creating an indexed array$arrayVariable = [ element1, element2, element3, … elementN]; (or) $arrayVariable = array( element1, element2, element3, … elementN);Explained here
  • Creating an associative array$arrayVariable = ["key1" => "value1", "key2" => "value2"]; (or) $arrayVariable = array("key1" => "value1", "key2" => "value2");Explained here
  • Accessing an associative array value$arrayVariable[“key1”];Explained here

Functions in PHP

Definition

Function
A function is a block of code that performs a specific task.

Why functions are useful

  • Code reuse: Functions let you reuse code and save time and effort.
  • Testing and debugging: You can test and debug one function separately from the rest of the code.
  • Modularity: Functions divide code into smaller parts that are easier to maintain and modify.

Types of PHP functions

Built-in functionsUser-defined functions
Already defined in PHP. They can do tasks such as finding a string’s length or sorting an array.Created by the programmer to perform specific tasks.

Defining and Calling User-defined Functions

Use the function keyword, a function name, and parentheses to define a function. A function name must start with a letter or an underscore, and function names are NOT case-sensitive. A name must be a valid PHP identifier and must not be a reserved word.

Function definition

function functionName([parameter list])

function is the keyword, functionName is the function’s name, and the optional parameter list is a comma-separated list of variables. Put the code to execute inside curly braces.

Definition

Parameter
A parameter is a variable listed in a function definition.

Definition

Argument
An argument is a value supplied when the function is called.
Calling a function

functionName( ); (or) functionName(argument list);

Use the function name and parentheses to execute it; put any arguments inside the parentheses, separated by commas.

Worked example

Call a function with no parameters: function printGreeting() echo “Hello, world!”; printGreeting();

Try it first, then show the solution
  1. Define printGreeting with no parameters.
  2. Call it with printGreeting().

Answer: Output will be “Hello, world!”.

Worked example

Define greet($name) to display “Hello, $name!” and call greet(“Barath”).

Try it first, then show the solution
  1. $name is the parameter in the function definition.
  2. “Barath” is the argument in the function call.

Answer: Output will be “Hello, Barath!”.

Worked example

Call addNumbers($x, $y), which displays $x + $y, as addNumbers(10,40).

Try it first, then show the solution
  1. The function has two parameters, $x and $y.
  2. The call supplies the arguments 10 and 40.

Answer: Output will be “50”.

Arrays and Indexed Arrays in PHP

Definition

Array
An array is a special data type that holds many values under a single variable name, and its elements can be any type of data.

Three main types of PHP arrays

  • Indexed array
  • Associative array
  • Multi-dimensional array

Definition

Indexed array
An indexed array uses a numeric index to access its elements, starting at 0 for the first element and increasing by 1.

In PHP, an array does not have to contain elements of the same data type. Create an indexed array with a comma-separated list inside square brackets or by using the array() function.

Creating an indexed array

$arrayVariable = [ element1, element2, element3, … elementN]; (or) $arrayVariable = array( element1, element2, element3, … elementN);

Put the array elements in the brackets or parentheses, separated by commas.

Worked example

$fruits = array(“apple”, “banana”, “orange”, “mango”); Find the value of $fruits[0] and $fruits[3].

Try it first, then show the solution
  1. The first element has index 0.
  2. The fourth element has index 3.

Answer: $fruits[0] is “apple” and $fruits[3] is “mango”.

Remember: For an indexed array, the first element is at index 0, not 1.

Associative Arrays in PHP

Definition

Associative array
An associative array stores key-value pairs, where each key identifies its corresponding value.

The keys in an associative array are also called labels because they identify the values. Values can be of any data type. Create this array with key-value pairs inside square brackets or by using the array() function.

Creating an associative array

$arrayVariable = ["key1" => "value1", "key2" => "value2"]; (or) $arrayVariable = array("key1" => "value1", "key2" => "value2");

Each key is followed by => and its value. Separate key-value pairs with commas.

Accessing an associative array value

$arrayVariable[“key1”];

Use the key inside square brackets to access its corresponding value.

Worked example

$ages = array(“Arun” => 25, “Balu” => 30, “Naveen” => 28); Find $ages[“Balu”].

Try it first, then show the solution
  1. Use “Balu” as the key inside square brackets.
  2. Look for the value paired with “Balu”.

Answer: $ages[“Balu”] is 30.

Remember: Use a numeric index for an indexed array and a key for an associative array.

Multidimensional Arrays in PHP

Definition

Multidimensional array
A multidimensional array is an array that has one or more arrays as its elements.

An array inside another array is also called a nested array. Use two sets of square brackets to reach an element. The first set selects the row, and the second set selects the column.

Worked example

Find the value printed by echo $arr[0][1]; for $arr = array( array(1, 2, 3), );

Try it first, then show the solution
  1. The first index, 0, selects the first array.
  2. The second index, 1, selects the second element in that array.

Answer: 2

Worked example

Find the value printed by echo $arr[1][2]; for $arr = array( array(1, 2, 3), array(4, 5, 6), array(7, 8, 9) );

Try it first, then show the solution
  1. The first index, 1, selects the second array.
  2. The second index, 2, selects the third element in that array.

Answer: 6

Remember: For a multidimensional array, the first index selects the row and the second index selects the column.

Functions in PHP

Definition

Function
A function is a block of code that performs a specific task.

Types of functions

Built-in functionUser-defined function
A pre-defined function is also called a built-in function.A programmer writes a user-defined function.

Function syntax and use

  • A user-defined function starts with the keyword function.
  • Define a function using this form: function functionname() { // code to be executed }
  • Call a function by writing its name followed by parentheses, such as functionName();

Arrays in PHP

Definition

Array
An array is a special data type that can hold many values under a single variable name, and an array element can be any type of data.

Definition

Associative array
An associative array is a data structure in which keys and values can be stored.

Array facts

  • Array names must begin with the $ symbol.
  • After $, an array name begins with an uppercase or lowercase letter (a-z or A-Z), or the _ symbol.

Array terms

Indexed arrayAssociative array
Its elements are accessed using an array index in square brackets.It stores keys and values.

Questions students ask

Short, direct answers to what students search for in this chapter.

What are the advantages of functions in PHP?

Functions support code reuse, testing and debugging, and modularity. Reusing a function can save time and effort. Testing and debugging can be done on an individual function. Modularity breaks code into smaller parts that are easier to maintain and modify.

What is the difference between a parameter and an argument in PHP?

A parameter is a variable written in a function definition. An argument is a value supplied when calling the function. For example, in greet($name), $name is a parameter; in greet(“Barath”), “Barath” is the argument.

How do you access an element in an indexed array in PHP?

Use the element’s numeric index inside square brackets after the array variable. The first element has index 0, and each later element’s index increases by 1. For example, $fruits[0] accesses the first element of $fruits.

How do you access a value in an associative array in PHP?

Use the value’s key inside square brackets after the array variable. For example, if $ages pairs “Balu” with 30, then $ages[“Balu”] accesses 30. The key identifies the value in the key-value pair.

What is a multidimensional array in PHP?

A multidimensional array is an array that contains one or more arrays as its elements. An array inside another array is also called a nested array. Its elements can be accessed with two sets of square brackets: the first selects the row, and the second selects the column.

How do you access an element in a multidimensional array in PHP?

Use two sets of square brackets after the array name. The first index selects the row, and the second index selects the column. For example, $arr[1][2] selects the element in row 1 and column 2.

Show 3 more questions

What is a function in PHP?

A function is a block of code that performs a specific task. A user-defined function is written by the programmer. In PHP, a user-defined function starts with the keyword function, and a function can be called by writing its name followed by parentheses.

What is an array in PHP?

An array is a special data type that holds many values under a single variable name. An array element can be any type of data. Array names begin with the $ symbol, followed by a letter or the _ symbol.

What is an associative array in PHP?

An associative array is a data structure in which keys and values can be stored. It is one of the array terms used in PHP. Array names begin with the $ symbol, followed by an uppercase or lowercase letter or the _ symbol.

Practice

Question 1 of 5Score 0

What is one benefit of using functions in PHP?

Exam questions

Practice questions with model answers. These are not from an official paper.

What is a function, and give two advantages of using functions in PHP.2 marks

A function is a block of code that performs a specific task. Functions support code reuse and make testing and debugging easier.

Name the two types of functions in PHP and explain each briefly.2 marks

Built-in functions are predefined in PHP and perform tasks. User-defined functions are created by the programmer to perform specific tasks in PHP code.

How is an indexed array different from an associative array?2 marks

An indexed array uses numeric indexes to access its elements, starting at 0. An associative array uses keys to identify and access its values.

Explain how a multidimensional array is accessed.2 marks

A multidimensional array contains arrays as its elements. Its elements can be accessed using two sets of square brackets: the first identifies the row and the second identifies the column.

Watch out

  • Do not confuse parameters, which appear in a function definition, with arguments, which are supplied in a function call.
  • Do not assume indexed arrays start at 1; their first index is 0.

Still stuck on this lesson?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →