php
What it does
This guide teaches how to write PHP code more efficiently and modernly. It shows better ways to organize data, handle errors, and use recent language features to make your code clearer and safer.
When to use it
- βWhen learning to program in PHP.
- βWhen reviewing or improving existing PHP code.
- βWhen wanting to use the latest and most efficient PHP versions.
Skill link
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/super-code/php/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agentβs skills folder.
Example of what to ask your AI
βHow can I make my PHP code safer when handling data?β
5 new skills a week. 1 minute to read.
How it works
The guide compares 'bad' code examples with 'good' ones to illustrate best practices. It focuses on using specific language functions and modern features to simplify common tasks.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
PHP: Idiomatic Efficiency Reference
When to Use
- Use this skill when the task matches this description: Language-specific super-code guidelines for php.
Table of Contents
- Arrays & Collections
- Type Safety
- Error Handling
- String Handling
- OOP & Modern PHP
- Functions & Closures
- Anti-patterns specific to PHP
1. Arrays & Collections {#arrays}
// β Manual accumulation
$result = [];
foreach ($items as $item) {
if ($item->isActive()) {
$result[] = strtoupper($item->getName());
}
}
// β
$result = array_map(
fn($i) => strtoupper($i->getName()),
array_filter($items, fn($i) => $i->isActive())
);
// β Manual key-value grouping
$grouped = [];
foreach ($items as $item) {
$grouped[$item->getCategory()][] = $item;
}
// β
(PHP 8.1+) β or use the loop above; PHP lacks a built-in groupBy
// The foreach is actually idiomatic PHP for grouping. No need to force array_* here.
// β Checking isset then accessing
if (isset($data['key'])) {
$value = $data['key'];
} else {
$value = 'default';
}
// β
$value = $data['key'] ?? 'default';
// β array_push for single element
array_push($items, $newItem);
// β
$items[] = $newItem;
Use array_map/array_filter for transforms. The foreach loop is fine when array functions would be less readable.
2. Type Safety {#types}
// β No type declarations
function process($items) {
return $items;
}
// β
(PHP 8.0+)
function process(array $items): array {
return $items;
}
// β Union type for nullable
function find(string $key): string|null { ... }
// β
function find(string $key): ?string { ... }
// β Loose comparison
if ($value == '0') { ... } // true for 0, '', false, null
// β
if ($value === '0') { ... }
// β Type checking with gettype()
if (gettype($x) === 'integer') { ... }
// β
if (is_int($x)) { ... }
// or with union types, avoid checks entirely
Enable declare(strict_types=1) at the top of every file.
3. Error Handling {#errors}
// β Suppressing errors with @
$data = @file_get_contents($path);
// β
$data = file_get_contents($path);
if ($data === false) {
throw new RuntimeException("Failed to read: $path");
}
// β Catching \Exception and swallowing
try { process(); }
catch (\Exception $e) { /* silence */ }
// β
try {
process();
} catch (SpecificException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new AppException('Processing failed', previous: $e);
}
// β Returning mixed types for error indication
function divide(int $a, int $b): int|false {
if ($b === 0) return false;
return intdiv($a, $b);
}
// β
β throw exception for exceptional cases
function divide(int $a, int $b): int {
if ($b === 0) throw new \DivisionByZeroError();
return intdiv($a, $b);
}
4. String Handling {#strings}
// β Concatenation for variable interpolation
$msg = 'Hello, ' . $name . '! You have ' . $count . ' messages.';
// β
$msg = "Hello, {$name}! You have {$count} messages.";
// β Manual string contains check
if (strpos($haystack, $needle) !== false) { ... }
// β
(PHP 8.0+)
if (str_contains($haystack, $needle)) { ... }
// β substr for prefix/suffix check
if (substr($str, 0, 4) === 'http') { ... }
if (substr($str, -4) === '.php') { ... }
// β
(PHP 8.0+)
if (str_starts_with($str, 'http')) { ... }
if (str_ends_with($str, '.php')) { ... }
5. OOP & Modern PHP {#oop}
// β Manual constructor property assignment
class User {
private string $name;
private int $age;
public function __construct(string $name, int $age) {
$this->name = $name;
$this->age = $age;
}
}
// β
(PHP 8.0+)
class User {
public function __construct(
private readonly string $name,
private readonly int $age,
) {}
}
// β Constants as class properties
class Status {
const ACTIVE = 'active';
const INACTIVE = 'inactive';
}
// β
(PHP 8.1+)
enum Status: string {
case Active = 'active';
case Inactive = 'inactive';
}
// β instanceof chains
if ($shape instanceof Circle) { ... }
elseif ($shape instanceof Rectangle) { ... }
// β
(PHP 8.0+)
$area = match(true) {
$shape instanceof Circle => $shape->radius ** 2 * M_PI,
β¦
FAQ
What are 'Arrays & Collections'?
These are ways to organize and manipulate lists of data in PHP. The guide shows how to use built-in functions instead of writing manual loops to make the code shorter and more readable.
What is 'Type Safety'?
It's ensuring that variables and functions use the correct data types (like numbers or text). This helps prevent unexpected errors in the program.
Why avoid '@' for error suppression?
The '@' symbol hides problems that should be fixed. It's better to let the error appear and handle it properly to understand what happened and fix the code.
Details
Original description by the author
Language-specific super-code guidelines for php.
Popularity
- GitHub stars
- 48k
- Ranking position
- #5895
- License
- MIT
- Updated
- 10/15/2026
Comments
- β¦
More skills from this repository
00-andruia-consultant
48kHelps your agent start or improve AI projects. It analyzes code, suggests tools, and creates action plans in Spanish.
007
48kLets your agent perform security audits and threat modeling. It's useful for protecting projects against attacks and vulnerabilities.
10-andruia-skill-smith
48kHelps your agent create or improve AI tools. It defines how tools work, writes documentation, and validates the structure.
20-andruia-niche-intelligence
48kLets your agent create a summary about a specific industry. It's useful for understanding the context, rules, usage patterns, and risks of a project, using reliable sources.
Similar skills
diffs
394kLets your agent produce a real, shareable diff of edits as a viewer link or image file, instead of a hand-written summary.
coding-agent
394kHelps your agent hand a larger coding job to Codex, Claude Code, or OpenCode as a background worker, then check on its progress.
gh-issues
394kHelps your agent find GitHub issues, pick the ones to work on, and open pull requests with fixes, including follow-up on review comments.