Functions and Arrays in PHP - Study Notes
Chapter Summary
This chapter covers two tools every PHP program uses: functions and arrays. A function lets you write a piece of code once and call it wherever you need it, which keeps programs modular and makes testing and debugging easier. An array lets one variable name hold many values. The chapter explains three kinds: indexed arrays, associative arrays and multidimensional arrays.
Learning Objectives
- Explain why functions make PHP programs modular and easier to maintain.
- Tell the difference between built-in functions and user-defined functions.
- Write and call a user-defined function with parameters and arguments.
- Describe indexed, associative and multidimensional arrays.
- Create arrays, add values to them and read values back.
Key Concepts and Definitions
- Function: A named block of code that does one particular job and can be called again and again.
- User-Defined Function: A function written by the programmer for a task that PHP's own functions do not cover.
- Built-In Function: A ready-made function that comes with PHP, such as strlen() for strings or count() for arrays.
- Parameter: A variable named in the function definition that receives a value when the function is called.
- Argument: The actual value passed to the function in the call.
- Array: A data type that stores many values, of any type, under a single variable name.
- Indexed Array: An array whose elements are identified by numbers, starting from index 0.
- Associative Array: An array in which each value is stored against a named key that you choose.
- Multidimensional Array: An array whose elements are themselves arrays, such as a table of rows and columns.
Worked Methods
Declaring and Invoking a User-Defined Function
Write the keyword function, then the function name, the parameters in brackets, and the body in curly braces. To run it, call the function by name and pass the arguments.
Example function definition:
function calculateTotal($firstNumber, $secondNumber) { return $firstNumber + $secondNumber; }
Example function invocation:
$result = calculateTotal(15, 35); // $result now holds 50
Creating and Querying an Associative Array
In an associative array each value has a string key as its label. Create it with the array() function, using => to pair each key with its value.
Example initialization:
$studentDetails = array("Name" => "Balu", "Age" => 30);
Example retrieval:
$studentName = $studentDetails["Name"]; // gives Balu
Common Exam Traps
- Case sensitivity: Function names in PHP, both built-in and user-defined, are not case-sensitive, so printGreeting() and PRINTGREETING() call the same function. Variable names, however, are case-sensitive.
- Index starts at 0: The first element of an indexed array is at index 0. In a three-element array the valid indexes are 0, 1 and 2. Asking for index 3 does not give the third element; PHP shows an undefined index (undefined array key) warning and returns NULL.
- Quotes around string keys: Write $studentDetails["Name"], not $studentDetails[Name]. Without quotes PHP looks for a constant called Name; older versions only warn, but PHP 8 stops with an error.
Exam Tips
- Always begin a user-defined function with the keyword function when you write code in the exam.
- Use count() to find the number of elements in an array, for example count($marks).
- In a two-dimensional array such as $table[1][2], the first index picks the row (the outer array) and the second picks the column (the inner array).