1- variables and arrays in php
Upgrade to Pro to Watch
Unlock this lesson and many more by upgrading to our Pro plan. Get access to exclusive content, in-depth tutorials, and much more!
Upgrade NowPHP Variables and Arrays – Complete Beginner-to-Business Guide
Variables and arrays are the foundation of every PHP application, from small student projects to large enterprise systems like CRMs, e-commerce platforms, HR systems, and online booking applications. Understanding how data is stored and manipulated in PHP will give you the power to build dynamic, real-world applications.
1. What Are Variables in PHP?
A variable stores information that your application needs at runtime. It can represent a user’s name, a product’s price, a setting, an API response, or anything else.
$name = "Ahmed";
$age = 25;
$height = -3.02;
$isStudent = true;
Real-Life Example
- E-commerce: storing product price, stock, and description.
- Student systems: storing student name, grade, or status.
- Finance apps: storing balance, transaction amount, or fees.
2. Types of Arrays in PHP
Arrays allow you to store multiple values inside a single variable. PHP supports three main types:
- Indexed Arrays — numeric keys
- Associative Arrays — custom string keys
- Multidimensional Arrays — arrays inside arrays
3. Indexed Arrays
$colors = ["Red", "Green", "Blue"];
You can access items by index:
echo $colors[0]; // Red
Real Business Use Case
- Showing a list of product tags or categories.
- Storing a list of available languages for a website.
- List of discount codes or promotional banners.
4. Associative Arrays (Key → Value)
These are perfect when storing structured information:
$person = [
"name" => "Ali",
"country" => "Egypt",
"language" => "Arabic"
];
When to Use Associative Arrays
- User profiles (name, email, country)
- Product details (price, size, stock)
- Settings (theme, language, app mode)
5. Multidimensional Arrays
$users = [
["name" => "Ali", "age" => 25, "skills" => ["HTML", "CSS"]],
["name" => "Sara", "age" => 22]
];
Real Business Scenarios
- A list of users in a CRM system.
- Products with variations (sizes, colors, stock per branch).
- Quiz questions with options and answers.
6. Array Functions (With Purpose + Use Cases)
✔ Adding Items
- array_push($arr, $value) — adds an item to the END Useful for adding new notifications or new cart items.
- $arr[] = $value — fastest way to push items
- array_unshift($arr, $value) — adds an item to the BEGINNING Useful for showing newest comments at the top.
✔ Removing Items
- array_pop($arr) — removes the LAST item Useful for undo actions, removing last cart item, removing last log.
- array_shift($arr) — removes the FIRST item Useful for queue systems, message processing, background tasks.
✔ Counting Items
- count($arr) — number of elements Use in dashboards: number of users, orders, likes, etc.
✔ Searching in Arrays
- in_array($value, $arr) — check if value exists Check if a user already liked a post.
- array_search($value, $arr) — get the index of a value Locate a product in a list or find which item was selected.
✔ Merging Arrays
- array_merge($a, $b) Combine filters, settings, permissions, tags.
✔ Sorting Arrays
- sort() — sort values
- rsort() — reverse sort
- asort() — sort by value keeping keys
- ksort() — sort by key Useful for sorting reports alphabetically.
7. Complete Example (From The Lesson Code)
$name = "Ahmed";
$age = 25;
$height = -3.02;
$isStudent = [""]; // non-empty array = true
$isStudentText = $isStudent ? "Yes" : "No";
echo "Name: $name <br>";
echo "Age: $age <br>";
echo "Height: $height m <br>";
echo "Is Student: $isStudentText <br>";
// Arrays
$colors = ["Red", "Green", "Blue"];
$colors[1100] = "NewColor";
$person = ["name" => "Ali", "country" => "Egypt"];
$users = [
["name" => "Ali", "age" => 25],
["name" => "Sara", "age" => 22],
];
8. Summary
Variables and arrays are used in every type of application, including:
- E-commerce apps
- Student management systems
- AI dashboards
- Booking & reservation systems
- Financial tracking tools
Mastering variables and arrays will make building any dynamic PHP application much easier.
