php
Helps your agent write code in PHP. It's useful for creating web applications and systems. It allows building the logic behind websites and services.
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.
Example of what to ask your AI
“How can I make my PHP code safer when handling data?”
Want more? Open the sections below.
How to use
The easiest way: copy the message below and paste it into Claude, ChatGPT, Cursor or any AI. It reads the skill and follows the instructions.
Skill link
Ready-to-paste message
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.
Installation
Easiest way: copy the skill link and paste it into Claude, ChatGPT or Cursor asking it to follow the instructions.
In the terminal (Claude Code, Codex, Cursor), in your project folder:
npx skills add sickn33/agentic-awesome-skills --skill phpOr download just the file:
curl -L https://raw.githubusercontent.com/sickn33/agentic-awesome-skills/main/plugins/agentic-awesome-skills-claude/skills/super-code/php/SKILL.md -o SKILL.mdTechnical 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
- #4281
- License
- MIT
- Updated
- 10/15/2026
Comments
- …
More skills from this repository
00-andruia-consultant
Helps your agent start or improve AI projects. It analyzes code, suggests tools, and creates action plans in Spanish.
sickn33/agentic-awesome-skills
007
Lets your agent perform security audits and threat modeling. It's useful for protecting projects against attacks and vulnerabilities.
sickn33/agentic-awesome-skills
10-andruia-skill-smith
Helps your agent create or improve AI tools. It defines how tools work, writes documentation, and validates the structure.
sickn33/agentic-awesome-skills
20-andruia-niche-intelligence
Lets 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.
sickn33/agentic-awesome-skills
Similar skills
benchmark-optimization-loop
Has your agent improve something's speed in a measured way. It tests multiple versions and chooses the fastest one that still works correctly.
affaan-m/ECC
change-impact
Helps your agent analyze the impact of code changes before making them. It compares the planned vs. actual results afterward, covering paths, data, tests, and documentation. Useful for risky or system-wide changes.
affaan-m/ECC
click-path-audit
Lets your agent trace all user buttons and interactions to find bugs. It finds issues where functions seem to work alone but fail together. Useful when bugs persist even after debugging.
affaan-m/ECC