TN Online TestSamacheer Kalvi practice

Functions and Arrays in PHP: One Mark Questions, Book Back Answers and Notes

12th Standard · Computer Applications · #5

Functions let you write a task once and call it by name; arrays let one variable hold many values. The notes explain how to define and call user-defined functions, how parameters receive arguments, common built-in functions, indexed, associative and multidimensional arrays, and looping through an array with foreach.

Study this chapter

Asked in previous board exams

6 questions from this chapter came in 6 past public exams. Open any of them to see the answer and explanation.

  1. March 2026
    A __________ is a block of code that performs a specific task.
    See answer in the solved paper →
  2. March 2025
    How many types of arrays are there in PHP ?
    See answer in the solved paper →
  3. March 2024
    A user-defined function in PHP starts with the keyword __________.
    See answer in the solved paper →
  4. March 2023
    Associative arrays are a ________ pair data structure.
    See answer in the solved paper →
  5. May 2022
    ________ stores more than one value of the same data type in single array variable.
    See answer in the solved paper →
  6. March 2020
    How many types of functions are there in PHP ?
    See answer in the solved paper →

About Functions and Arrays in PHP

Medium ~90 min study

Suppose a program has to calculate a student's total in ten different places. Writing the same lines ten times makes the program long, and a mistake has to be fixed ten times. A function lets you write those lines once, give them a name and call that name wherever you need it.

Arrays solve a different problem. Instead of creating $mark1, $mark2, $mark3 and so on, you store all the marks in a single variable, $marks, and pick each one out by its position or by a name. Functions and arrays are often used together: you pass an array of marks to a function, and the function works out the total or the average.

Exam questions from this chapter ask about the syntax for defining a function, the difference between parameters and arguments, the => symbol in associative arrays, and how the three kinds of arrays differ. Short programs, such as finding the sum of array elements with foreach, are common in practicals too.

What you'll learn

  • Define a user-defined function in PHP and call it from a script.
  • Tell the parameters in a function definition from the arguments in a function call.
  • Create an indexed array using array() or square brackets and change its elements.
  • Create an associative array with string keys and read values by key.
  • Build a multidimensional array and access an element using two indexes.
  • Use a foreach loop to go through the elements of an array.

Before you start

  • PHP tags, statements and how a script runs on the server.
  • Variables and basic data types such as strings and integers.
  • if statements and loops such as for and while.

Topics covered in this chapter

User-Defined Functions Functions you write yourself with the function keyword. They run only when called by name in the script.
Built-In Functions Functions that come with PHP, such as strlen() for string length and sort() for arranging array elements.
Parameters and Arguments Parameters are the variables named in the function definition. Arguments are the actual values supplied when the function is called.
Indexed Arrays Arrays whose elements are numbered from 0, created with array() or square brackets and read with $name[index].
Associative Arrays Arrays that use named keys joined to values with =>, such as "age" => 17, and read with $name["key"].
Multidimensional Arrays Arrays that contain other arrays, used for table-like data and accessed with two indexes, first the row and then the column.

Functions and Arrays in PHP explained

Functions and Arrays in PHP

Why functions are used

A function is a block of statements that does one task and can be used again and again. Functions make code reusable, shorter and easier to debug, because each task sits in its own block. PHP has a large number of built-in functions, such as strlen() to find the length of a string and sort() to sort an array. You can also write your own, called user-defined functions.

Defining and calling a function

A user-defined function starts with the keyword function, then the function name, then brackets that may hold parameters, and finally the statements inside curly braces:

function add($a, $b) { echo $a + $b; }

The function runs only when it is called, for example add(10, 20);, which prints 30. A function name must start with a letter or an underscore, and function names are not case-sensitive, so ADD(10, 20) calls the same function. In the definition, $a and $b are parameters; the values 10 and 20 given in the call are arguments. When the call is made, control passes to the function, its statements run, and control returns to the line after the call. Variables created inside a function are local to that function.

Indexed arrays

An array stores many values in one variable. In PHP the elements of one array can be of different types, for example a number and a string together. An indexed array uses numbers as indexes, starting from 0. You can create one with $subjects = array("Tamil", "English", "Maths"); or with square brackets, $subjects = ["Tamil", "English", "Maths"];. Here $subjects[0] is "Tamil" and $subjects[2] is "Maths". The same brackets are used to change a value: $subjects[1] = "Physics";.

Associative arrays

An associative array uses named keys instead of numbers. Each key is linked to its value with the => symbol: $student = array("name" => "Kavin", "age" => 17, "city" => "Madurai");. You then read a value by its key, as in $student["age"], which gives 17. Because the key describes the value, the code is easier to understand than remembering that the age is at position 1.

Multidimensional arrays and foreach

A multidimensional array is an array whose elements are themselves arrays. It suits data laid out like a table, with rows and columns. You reach an element with two sets of brackets: in $marks[1][2], the first index picks the row (the inner array) and the second picks the column (the element inside it). The foreach loop visits each element of an array in turn without a counter variable. foreach ($subjects as $s) gives each value, and foreach ($student as $key => $value) gives each key with its value.

Common mistakes to avoid

  • Thinking function names are case-sensitive like variable names. In PHP, add() and ADD() call the same function, though it is better to use one spelling.
  • Mixing up parameters and arguments. Parameters are in the definition; arguments are the values in the call.
  • Starting an indexed array at 1. The first element is at index 0, so a three-element array ends at index 2.
  • Assuming all elements of a PHP array must be of one type. PHP allows mixed types in the same array.
  • Using = or -> instead of => between a key and its value in an associative array.

Test yourself on these with the practice test, then check the worked reasoning in the solved MCQs.

Frequently asked questions

How do I define a function in PHP?

Write the keyword function, then the name, then brackets holding any parameters, then the statements inside curly braces. For example: function greet($name) { echo "Hello " . $name; }. The name must start with a letter or underscore. Call it with greet("Priya");.

What is the difference between parameters and arguments?

Parameters are the variables listed in the function definition, like $a and $b in function add($a, $b). Arguments are the actual values passed when you call it, like 5 and 7 in add(5, 7). Each argument is copied into the matching parameter.

Are PHP function names case-sensitive?

No. A function defined as total() can be called as TOTAL() or Total() without an error. Variable names, however, are case-sensitive. Use the same spelling throughout so your code is easy to read.

How do I create an indexed array in PHP?

Use the array() function, $colours = array("red", "green", "blue");, or square brackets, $colours = ["red", "green", "blue"];. PHP numbers the elements from 0, so $colours[0] is red and $colours[2] is blue.

How is an associative array different from an indexed array?

An indexed array uses numbers starting from 0 to identify its elements. An associative array uses keys that you choose, usually strings, and joins each key to its value with =>. For example, $student["name"] reads more clearly than $student[0].

How do I access a value in a multidimensional array?

Use two sets of square brackets. The first index selects the inner array (the row) and the second selects the element in it (the column). For example, $marks[0][1] is the second element of the first inner array.

Last updated 15 September 2026

More chapters in Computer Applications

View all
1 Multimedia 2 An Introduction to Adobe PageMaker 3 Introduction to Database Management System 4 PHP: Hypertext Preprocessor 6 Conditional Statements in PHP 7 Loops in PHP 8 Forms and Files 9 Connecting PHP and MYSQL 10 Introduction to Computer Networks 11 Network Examples and Protocols 12 Domain Name System (DNS) 13 Network Cabling 14 Open Source Concepts 15 E-Commerce 16 Electronic Payment Systems 17 E-Commerce Security Systems 18 Electronic Data Interchange (EDI)