All skills
asyrafhussin avatar

/php-best-practices

@e7ea05d

PHP 8.x modern patterns, PSR standards, and SOLID principles. Use when reviewing PHP code, checking type safety, auditing code quality, or ensuring PHP best practices. Triggers on "review PHP", "check PHP code", "audit PHP", or "PHP best practices".

Use this Skill: https://skilld.dev/gh/asyrafhussin/agent-skills/php-best-practices

This session only. Nothing lands on disk.

rulesperf-string-functions.md

≈502 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Native String Functions

Use PHP's built-in string functions instead of regex for simple operations. String functions are faster and more readable.

Bad Example

<?php

declare(strict_types=1);

// Regex for simple checks - overkill
if (preg_match('/^https/', $url)) {
    // starts with https
}

if (preg_match('/\.pdf$/', $filename)) {
    // ends with .pdf
}

if (preg_match('/admin/', $role)) {
    // contains admin
}

// Regex for simple replacements
$clean = preg_replace('/\s+/', ' ', $text);
$slug = preg_replace('/[^a-z0-9]/', '-', strtolower($title));

Good Example

<?php

declare(strict_types=1);

// str_starts_with (PHP 8.0+)
if (str_starts_with($url, 'https')) {
    // starts with https
}

// str_ends_with (PHP 8.0+)
if (str_ends_with($filename, '.pdf')) {
    // ends with .pdf
}

// str_contains (PHP 8.0+)
if (str_contains($role, 'admin')) {
    // contains admin
}

// String functions for simple operations
$trimmed = trim($input);
$lower = strtolower($name);
$upper = strtoupper($code);
$replaced = str_replace('old', 'new', $text);
$parts = explode(',', $csv);
$joined = implode(', ', $items);

// substr for extraction
$extension = substr($filename, strrpos($filename, '.') + 1);

// Use regex only for complex patterns
$isEmail = filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
$matches = preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $date, $parts);

Why

  • Performance: String functions are 2-10x faster than regex for simple operations
  • Readability: str_contains($s, 'admin') is clearer than preg_match('/admin/', $s)
  • No Escaping: No need to escape regex special characters
  • Type Safety: String functions have strict parameter types
  • PHP 8.0+: str_starts_with, str_ends_with, str_contains replace common regex patterns

Source: SKILL.md on GitHub

No alerts17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    This skill is a comprehensive and safe resource for PHP 8.x best practices. It provides structured guidance on type safety, modern PHP syntax, PSR standards, and secure coding practices (such as prepared statements and password hashing). The agent is instructed to use standard environment detection commands (php -v, grep) to tailor its advice. No malicious patterns or security risks were identified.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer6mo

    39 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at e7ea05d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 7 months ago
Other metadata
metadata
{
  "author": "php-community",
  "version": "2.1.0",
  "phpVersion": "8.0 - 8.5"
}

README badge

README badge for asyrafhussin/agent-skills/php-best-practices