1. Home
  2. /
  3. PHP
  4. /
  5. Theme Toggle and Counter

Theme Toggle and Counter - PHP Typing CST Test

Loading…

Theme Toggle and Counter — PHP Code

Demonstrates a simple counter and theme toggle using PHP sessions, simulating a web-based interactive behavior.

<?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>";

PHP Language Guide

PHP is a widely-used, open-source, server-side scripting language designed for building dynamic web applications, powering over 75% of websites. Known for its simplicity, massive ecosystem, and deep integration with databases and the web, PHP remains a core backend technology behind platforms like WordPress, Facebook (early), and Wikipedia.

Primary Use Cases

  • ▸Web applications and APIs
  • ▸CMS and blog systems (WordPress)
  • ▸E-commerce platforms
  • ▸REST and GraphQL backends
  • ▸Automation scripts and cron jobs
  • ▸Enterprise backend systems using Laravel/Symfony

Notable Features

  • ▸Simple and beginner-friendly syntax
  • ▸Massive ecosystem and hosting support
  • ▸Modern OOP and strong typing (PHP 7+)
  • ▸Composer dependency manager
  • ▸JIT compiler in PHP 8
  • ▸Integration with MySQL, Redis, and more

Origin & Creator

Created by Rasmus Lerdorf in 1995 as ‘Personal Home Page Tools’. It evolved into PHP 3/4 with Zend Engine, then PHP 5 introduced OOP, PHP 7 delivered massive performance improvements, and PHP 8 added JIT compilation, attributes, union types, enums, fibers, and major language modernization.

Industrial Note

PHP dominates the CMS ecosystem-WordPress, Drupal, Joomla. It powers Laravel and Symfony large-scale enterprise apps, e-commerce platforms like Magento, and millions of shared-hosting-based businesses. Used by Meta (legacy), Slack internal tools, Mailchimp, Etsy, and Wikipedia.

More PHP Typing Exercises

PHP Form Handling ExamplePHP Associative Array ExamplePHP Simple Function ExamplePHP Date and Time ExamplePHP Include and Require ExamplePHP File Handling ExamplePHP Simple Login SimulationPHP GET Parameters ExamplePHP Session Counter Example

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinJavaC#RubyMqlCqlN1qlCypherGremlin