CHAPTER 06
Beginner
PHP Variables, Arrays, and Functions
Updated: May 14, 2026
25 min read
# CHAPTER 6
PHP Variables, Arrays, and Functions
1. Introduction
As your backend applications grow, managing data one variable at a time becomes impossible. Imagine trying to store the names of 1,000 registered users in 1,000 separate variables! Furthermore, writing the exact same code repeatedly to validate emails violates the core rule of programming: DRY (Don't Repeat Yourself). In this chapter, we will master Arrays to manage large datasets and Functions to create reusable blocks of logic.2. Learning Objectives
By the end of this chapter, you will be able to:- Create and manipulate Indexed Arrays.
- Build and traverse Associative Arrays (key-value pairs).
- Write custom PHP Functions with parameters and return values.
- Understand variable scope within functions.
3. Beginner-Friendly Explanation
An Array is like a medicine cabinet with many shelves. Instead of buying 10 separate small boxes (variables) to hold your pills, you buy one large cabinet (the Array) and put all the pills on different shelves. You can easily loop through the cabinet to find what you need. A Function is like a blender. You build the blender once. Whenever you want a smoothie, you don't rebuild the blender; you just toss in strawberries (Inputs/Parameters), press the button, and it hands you a smoothie (Output/Return Value).4. Indexed Arrays
An indexed array stores a list of items. The "index" is the numerical position of the item, starting at0.
php
5. Associative Arrays (Key-Value Pairs)
Instead of relying on a numerical index (0, 1, 2), associative arrays use named "Keys." This is the most common way data is formatted when pulled from a MySQL database!
php
6. Functions (Reusable Logic)
To write a function, you use thefunction keyword, give it a name, define its parameters (inputs), and return an output.
php
7. Variable Scope
A major concept in functions is Scope. A variable created *outside* a function cannot automatically be used *inside* the function.
php
*To fix this, you either pass the discount in as a parameter, or use the global keyword.*
8. Backend Workflow Example: User Verification
Let's combine arrays and functions to simulate checking if a user is banned.
php
9. Best Practices
-
Single Responsibility Principle: A function should do exactly one thing. If your function is called
calculateTax(), it should only calculate math. It should *not* also print HTML to the screen. Return the math, and let a different part of your code handle the printing.
10. Common Mistakes
-
echovsreturnin Functions: Beginners oftenechothe result inside the function. This is bad practice because it forces the function to print text immediately, even if you just wanted to save the result to a variable. Alwaysreturnthe value from the function, andechoit outside the function.
11. Exercises
-
1.
Create an Associative Array representing a Book (Title, Author, Pages). Write a
foreachloop to print all the details to the screen.
12. Coding Challenges
-
Challenge: Write a function called
celsiusToFahrenheit($temp). It should take a temperature in Celsius, perform the math($temp * 9/5) + 32, and return the Fahrenheit value. Call the function with the value20and echo the result.
13. MCQs with Answers
Question 1
In PHP, what type of array uses named keys (like "email" => "test@test.com") instead of numerical indexes (0, 1, 2)?
Question 2
What keyword is used inside a PHP function to send a value back to the main script?
14. Interview Questions
- Q: Explain the difference between an Indexed Array and an Associative Array. Which one is more commonly used when fetching a single user row from a database?
- Q: What is "Variable Scope" in PHP, and why can't a function automatically read a variable declared at the top of your file?