health-check domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/yogisv5/sitesbyyogi.com/wp-includes/functions.php on line 6260
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.
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"];
?>
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";
?>
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>";
}
?>
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
?>
| 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) |
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 |
$_GET<?php
echo "Welcome, " . $_GET['name'];
?>
β Visit in browser:
http://localhost/php-basics/get.php?name=Yogi
β Output: Welcome, Yogi
$_POSTUsed 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!
$_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.
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.
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.
$_GETCreate hello.php:
<?php
$name = $_GET['name'];
echo "Hello, $name!";
?>
β Visit in browser:
http://localhost/php-basics/hello.php?name=StreetGeek
$_POSTCreate 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.
$_SERVERCreate 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.
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.
| # | 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 |
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
arrays.php β displays list and countprofile.php β associative array formatted outputform.html + form-handler.php β working formcontact.php β POST form displaystudents.php β dynamic student directory
π 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.