StreetGeek Academy Β· PHP Foundations

🧩 Module 5: Arrays and Superglobals

Objective: Learn how to store and manipulate multiple values in PHP using arrays, and understand
how PHP’s superglobal variables allow data exchange between forms, URLs, sessions, and cookies.

πŸ”Ή 1. What Are Arrays?

An array is a special variable that can hold multiple values under one name.

Instead of:

<?php
$student1 = "Yogi";
$student2 = "Brandon";
$student3 = "Nicole";
?>

You can use:

<?php
$students = ["Yogi", "Brandon", "Nicole"];
?>

πŸ”Ή 2. Types of Arrays in PHP

🧩 Indexed Arrays

Each value has a numeric index starting from 0.

<?php
$colors = ["Red", "Blue", "Green"];
echo $colors[0]; // Red

// Alternate syntax:
$colors = array("Red", "Blue", "Green");

// Add new items dynamically:
$colors[] = "Yellow";
?>

🧩 Associative Arrays

Each value has a named key instead of a number.

<?php
$user = [
    "name" => "Yogi",
    "age"  => 30,
    "role" => "Instructor"
];

echo $user["name"]; // Yogi

foreach ($user as $key => $value) {
    echo "$key: $value<br>";
}
?>

🧩 Multidimensional Arrays

Arrays inside arrays β€” used for structured data like tables or JSON-like objects.

<?php
$students = [
    ["name" => "Yogi",   "age" => 30],
    ["name" => "Nicole", "age" => 28],
    ["name" => "Brandon","age" => 32]
];

echo $students[1]["name"]; // Nicole
?>

πŸ”Ή 3. Common Array Functions

Function Description Example
count() Returns number of elements count($colors)
array_push() Adds one or more elements to the end array_push($colors, "Orange")
array_merge() Combines arrays array_merge($a, $b)
sort() Sorts values ascending sort($colors)
rsort() Sorts values descending rsort($colors)
in_array() Checks if a value exists in_array("Blue", $colors)
array_keys() Returns all keys array_keys($user)

πŸ”Ή 4. Introduction to PHP Superglobals

Superglobals are built-in PHP variables that are always accessible, anywhere in your script β€” no
global keyword needed.

Superglobal Description
$_GET Collects data from the URL
$_POST Collects data from HTML forms
$_REQUEST Collects data from both GET and POST
$_SERVER Server and execution environment info
$_SESSION Stores user session data
$_COOKIE Stores small data on user’s computer
$_FILES Handles file uploads

Example: $_GET

<?php
echo "Welcome, " . $_GET['name'];
?>

βœ… Visit in browser:

http://localhost/php-basics/get.php?name=Yogi
β†’ Output: Welcome, Yogi

Example: $_POST

Used with HTML forms.

<!DOCTYPE html>
<html>
<body>
<form method="POST" action="welcome.php">
  Name: <input type="text" name="username"><br>
  <input type="submit" value="Submit">
</form>
</body>
</html>
<?php // welcome.php
$name = $_POST['username'];
echo "Welcome, $name!";
?>

βœ… After submitting the form, the browser displays: Welcome, Yogi!

Example: $_SERVER

<?php
echo $_SERVER['PHP_SELF'] . "<br>";
echo $_SERVER['SERVER_NAME'] . "<br>";
echo $_SERVER['HTTP_USER_AGENT'] . "<br>";
?>

βœ… Prints your server path, domain, and browser details.

🧩 Hands-On Practice

Exercise 1: Working with Arrays

Create arrays.php:

<?php
$fruits = ["Apple", "Banana", "Cherry"];
array_push($fruits, "Mango");

echo "Fruits List:<br>";
foreach ($fruits as $fruit) {
    echo "- $fruit<br>";
}

echo "<br>Total Fruits: " . count($fruits);
?>

βœ… Output shows all fruits and the count.

Exercise 2: Associative Array

Create profile.php:

<?php
$person = [
    "name"     => "Yogi",
    "city"     => "Richmond",
    "language" => "PHP"
];

foreach ($person as $key => $value) {
    echo ucfirst($key) . ": $value<br>";
}
?>

βœ… Displays each key and value formatted neatly.

Exercise 3: Using $_GET

Create hello.php:

<?php
$name = $_GET['name'];
echo "Hello, $name!";
?>

βœ… Visit in browser:

http://localhost/php-basics/hello.php?name=StreetGeek

Exercise 4: Simple Form with $_POST

Create two files: form.html and form-handler.php.

<!-- form.html -->
<form method="POST" action="form-handler.php">
    Enter your email: <input type="email" name="email">
    <input type="submit" value="Submit">
</form>
<?php // form-handler.php
$email = $_POST['email'];
echo "You entered: $email";
?>

βœ… After submitting, the page shows your input.

Exercise 5: Using $_SERVER

Create server-info.php:

<?php
echo "Current Script: " . $_SERVER['PHP_SELF'] . "<br>";
echo "Host: " . $_SERVER['SERVER_NAME'] . "<br>";
echo "User Agent: " . $_SERVER['HTTP_USER_AGENT'];
?>

βœ… Displays server and browser info.

🎯 Mini Project – Contact Form Display

Goal: Use $_POST to display submitted data dynamically.

Create contact.php:

<?php
// contact.php
?>
<form method="POST" action="">
    Name: <input type="text" name="name"><br>
    Email: <input type="email" name="email"><br>
    Message: <textarea name="message"></textarea><br>
    <input type="submit" value="Send">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    echo "<h3>Submitted Information:</h3>";
    echo "Name: " . $_POST['name'] . "<br>";
    echo "Email: " . $_POST['email'] . "<br>";
    echo "Message: " . $_POST['message'];
}
?>

βœ… Type into the form and see your input echoed back.

🧾 Module 5 Quiz

# Question Options Correct
1 What type of array uses named keys? a) Indexed Β· b) Associative Β· c) Multidimensional b
2 Which function adds new items to an array? a) append() Β· b) push() Β· c) array_push() c
3 What does $_GET do? a) Collects form data Β· b) Collects data from URL Β· c) Stores cookies b
4 Which superglobal gives server information? a) $_SESSION Β· b) $_SERVER Β· c) $_COOKIE b
5 What does count() return? a) The sum Β· b) The length Β· c) The number of elements in an array c

πŸ’ͺ Challenge Task – Student Directory

Objective: Combine arrays, loops, and HTML to display student info dynamically.

Create students.php:

<?php
$students = [
    ["name" => "Yogi",    "course" => "PHP Foundations"],
    ["name" => "Nicole",  "course" => "WordPress Dev"],
    ["name" => "Brandon", "course" => "JavaScript Basics"]
];
?>

<h2>Student Directory</h2>
<ul>
<?php foreach ($students as $student): ?>
    <li><?php echo $student["name"]; ?> β€” <?php echo $student["course"]; ?></li>
<?php endforeach; ?>
</ul>

βœ… Output:

Yogi β€” PHP Foundations
Nicole β€” WordPress Dev
Brandon β€” JavaScript Basics

🧾 Submission Checklist

  • βœ… arrays.php β†’ displays list and count
  • βœ… profile.php β†’ associative array formatted output
  • βœ… form.html + form-handler.php β†’ working form
  • βœ… contact.php β†’ POST form display
  • βœ… students.php β†’ dynamic student directory
  • βœ… Quiz answers filled

🏁 Next Step: In Module 6, you’ll dive into functions β€” how to write reusable, efficient blocks
of PHP code that make your scripts modular, clean, and powerful.