Learn PHP with Real Code Examples
Updated Nov 17, 2025
Code Sample Descriptions
1
PHP Theme Toggle and Counter
<?php
session_start();
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
}
if (!isset($_SESSION['isDark'])) {
$_SESSION['isDark'] = false;
}
$action = $_GET['action'] ?? '';
switch ($action) {
case 'increment': $_SESSION['count']++; break;
case 'decrement': $_SESSION['count']--; break;
case 'reset': $_SESSION['count'] = 0; break;
case 'toggleTheme': $_SESSION['isDark'] = !$_SESSION['isDark']; break;
}
$theme = $_SESSION['isDark'] ? 'Dark' : 'Light';
$count = $_SESSION['count'];
echo "<h2>Counter: $count</h2>\n";
echo "<p>Theme: $theme</p>\n";
echo "<a href='?action=increment'>+</a> ";
echo "<a href='?action=decrement'>-</a> ";
echo "<a href='?action=reset'>Reset</a> ";
echo "<a href='?action=toggleTheme'>Toggle Theme</a>";
Demonstrates a simple counter and theme toggle using PHP sessions, simulating a web-based interactive behavior.
2
PHP Form Handling Example
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = htmlspecialchars($_POST['name']);
echo "<p>Hello, $name!</p>";
}
?>
<form method='post'>
<input type='text' name='name' placeholder='Enter your name'>
<input type='submit' value='Submit'>
</form>
Processes a simple form submission and displays input values safely.
3
PHP Associative Array Example
<?php
$ages = ['Alice' => 25, 'Bob' => 30];
foreach ($ages as $name => $age) {
echo "$name is $age years old.<br>";
}
Demonstrates creating and iterating over an associative array.
4
PHP Simple Function Example
<?php
function factorial($n) {
if ($n <= 1) return 1;
return $n * factorial($n - 1);
}
echo 'Factorial of 5 is ' . factorial(5);
Defines a function to calculate factorial recursively.
5
PHP Date and Time Example
<?php
echo 'Current Date: ' . date('Y-m-d H:i:s');
Displays the current date and time using PHP's date function.
6
PHP Include and Require Example
<?php
include 'header.php';
echo '<p>Main content here.</p>';
require 'footer.php';
Demonstrates including another PHP file.
7
PHP File Handling Example
<?php
$file = 'data.txt';
file_put_contents($file, 'Hello PHP\n', FILE_APPEND);
echo nl2br(file_get_contents($file));
Writes and reads from a text file using PHP.
8
PHP Simple Login Simulation
<?php
$correctUser = 'admin';
$correctPass = '1234';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user = $_POST['user'];
$pass = $_POST['pass'];
if ($user === $correctUser && $pass === $correctPass) {
echo 'Login successful';
} else {
echo 'Invalid credentials';
}
}
?>
<form method='post'>
<input name='user' placeholder='Username'>
<input name='pass' placeholder='Password' type='password'>
<input type='submit'>
</form>
Simulates a basic login check using hard-coded credentials.
9
PHP GET Parameters Example
<?php
$name = $_GET['name'] ?? 'Guest';
echo "<p>Hello, $name!</p>";
?>
<a href='?name=Alice'>Say Hello to Alice</a>
Retrieves and displays query string parameters from the URL.
10
PHP Session Counter Example
<?php
session_start();
if (!isset($_SESSION['visits'])) {
$_SESSION['visits'] = 0;
}
$_SESSION['visits']++;
echo 'Page visits this session: ' . $_SESSION['visits'];
Tracks page visits per session using PHP sessions.