TN Online TestSamacheer Kalvi practice

Functions and Arrays in PHP - Study Notes

Share this chapter: Telegram

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

Key Concepts and Definitions

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

Exam Tips

Solved MCQs → Practice test →

More for this chapter

Book Back Questions11 textbook MCQs · solved Additional MCQs15 extra MCQs · solved Practice TestInteractive · instant score Book Back TestTest yourself on the textbook set Additional MCQ TestTest yourself on the extra set Formula SheetAll key formulas

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)