PHP
Overview
PHP is a popular general-purpose scripting language especially suited to web development; it powers a large share of the web from blogs to major sites. Code is embedded in or generated alongside HTML and executed server-side. Read this when building web backends in PHP, working with Composer packages, or setting up a PHP runtime.
Official sources
Install / setup
The official downloads page shows per-OS instructions. For Debian/Ubuntu it lists, verbatim:
# Update the package lists.
sudo apt update
# Install PHP.
sudo apt install -y php
Source: https://www.php.net/downloads (current stable versions listed there include 8.5, 8.4, 8.3, 8.2)
Core concepts
- Request lifecycle — PHP scripts typically run per HTTP request and exit; state does not persist across requests unless stored (session, DB, cache).
- Types and weak/strict typing — PHP is dynamically typed but supports type declarations and
declare(strict_types=1) for strict scalar typing.
- Classes, interfaces, traits, and enumerations — full OOP model; traits share method implementations across classes.
- Namespaces — organize and disambiguate classes/functions to avoid collisions.
- Superglobals and predefined variables —
$_GET, $_POST, $_SESSION, $_SERVER expose request and environment data.
- Composer — the de facto dependency manager and PSR-4 autoloader for PHP libraries.
- Errors, exceptions, generators, fibers, and attributes are part of the modern language reference.
Best practices
- Enable
declare(strict_types=1) and use type declarations to catch type errors early (see php.net Language Reference: Types).
- Follow the Security section of the docs: validate and escape all user-submitted data, use prepared statements for databases.
- Manage dependencies and autoloading with Composer rather than manual
require chains.
- Keep PHP on a supported version (php.net lists active versions) and apply security releases promptly.
Common pitfalls
- Concatenating user input into SQL → use parameterized/prepared statements (PDO or mysqli) to prevent injection.
- Echoing unescaped user data into HTML → escape output (
htmlspecialchars) to prevent XSS, per the docs' security guidance.
- Relying on loose comparison (
==) where type juggling surprises (e.g. "0" == false) → use strict comparison (===).
Examples
<?php
declare(strict_types=1);
function greet(string $name): string {
return "Hello, " . htmlspecialchars($name);
}
echo greet($_GET['name'] ?? 'world');
Further reading
Related skills
- ../ruby — dynamic scripting language comparison
- ../../stacks — PHP web frameworks (Laravel, Symfony) consume this language
1---2name: php3description: General-purpose server-side scripting language for the web; consult for PHP syntax, OOP, Composer, and install/setup.4domain: language5category: language6tags: [php, server-side, web, scripting, backend]7official_sources:8 - https://www.php.net/docs.php9 - https://github.com/php/php-src10verified: 2026-06-1611metadata:12 source: https://skillmd.com/skills/gabriel7silva/php13---1415# PHP1617## Overview18PHP is a popular general-purpose scripting language especially suited to web development; it powers a large share of the web from blogs to major sites. Code is embedded in or generated alongside HTML and executed server-side. Read this when building web backends in PHP, working with Composer packages, or setting up a PHP runtime.1920## Official sources21- Docs: https://www.php.net/docs.php22- Repo: https://github.com/php/php-src23- Install / download: https://www.php.net/downloads2425## Install / setup26The official downloads page shows per-OS instructions. For Debian/Ubuntu it lists, verbatim:2728```bash29# Update the package lists.30sudo apt update3132# Install PHP.33sudo apt install -y php34```3536Source: https://www.php.net/downloads (current stable versions listed there include 8.5, 8.4, 8.3, 8.2)3738## Core concepts39- Request lifecycle — PHP scripts typically run per HTTP request and exit; state does not persist across requests unless stored (session, DB, cache).40- Types and weak/strict typing — PHP is dynamically typed but supports type declarations and `declare(strict_types=1)` for strict scalar typing.41- Classes, interfaces, traits, and enumerations — full OOP model; traits share method implementations across classes.42- Namespaces — organize and disambiguate classes/functions to avoid collisions.43- Superglobals and predefined variables — `$_GET`, `$_POST`, `$_SESSION`, `$_SERVER` expose request and environment data.44- Composer — the de facto dependency manager and PSR-4 autoloader for PHP libraries.45- Errors, exceptions, generators, fibers, and attributes are part of the modern language reference.4647## Best practices48- Enable `declare(strict_types=1)` and use type declarations to catch type errors early (see php.net Language Reference: Types).49- Follow the Security section of the docs: validate and escape all user-submitted data, use prepared statements for databases.50- Manage dependencies and autoloading with Composer rather than manual `require` chains.51- Keep PHP on a supported version (php.net lists active versions) and apply security releases promptly.5253## Common pitfalls54- Concatenating user input into SQL → use parameterized/prepared statements (PDO or mysqli) to prevent injection.55- Echoing unescaped user data into HTML → escape output (`htmlspecialchars`) to prevent XSS, per the docs' security guidance.56- Relying on loose comparison (`==`) where type juggling surprises (e.g. `"0" == false`) → use strict comparison (`===`).5758## Examples59```php60<?php61declare(strict_types=1);6263function greet(string $name): string {64 return "Hello, " . htmlspecialchars($name);65}6667echo greet($_GET['name'] ?? 'world');68```6970## Further reading71- Language reference: https://www.php.net/docs.php72- Security considerations: https://www.php.net/docs.php (Security section)7374## Related skills75- ../ruby — dynamic scripting language comparison76- ../../stacks — PHP web frameworks (Laravel, Symfony) consume this language