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.

rulessec-output-escaping.md

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

Output Escaping

Always escape output based on the context where it will be used to prevent XSS and injection attacks.

Bad Example

<?php

declare(strict_types=1);

// Raw user data in HTML - XSS vulnerable
echo "<h1>Welcome, {$user->name}</h1>";
echo "<p>{$user->bio}</p>";

// Raw data in HTML attributes
echo "<input value='{$searchTerm}'>";

// Raw data in JavaScript context
echo "<script>var name = '{$user->name}';</script>";

// Raw data in URL
echo "<a href='/search?q={$query}'>Search</a>";

Good Example

<?php

declare(strict_types=1);

// HTML context - htmlspecialchars with ENT_QUOTES
$safeName = htmlspecialchars($user->name, ENT_QUOTES, 'UTF-8');
echo "<h1>Welcome, {$safeName}</h1>";

// HTML attributes - same escaping
$safeSearch = htmlspecialchars($searchTerm, ENT_QUOTES, 'UTF-8');
echo "<input value=\"{$safeSearch}\">";

// JavaScript context - json_encode
$jsonName = json_encode($user->name, JSON_HEX_TAG | JSON_HEX_AMP);
echo "<script>var name = {$jsonName};</script>";

// URL context - urlencode
$safeQuery = urlencode($query);
echo "<a href=\"/search?q={$safeQuery}\">Search</a>";

// Helper function for consistent escaping
function e(string $value): string
{
    return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}

echo "<p>" . e($user->bio) . "</p>";

// In templating engines (Blade, Twig) - auto-escaping is default
// Blade: {{ $user->name }}         - escaped
// Blade: {!! $trustedHtml !!}      - raw (only for trusted content)
// Twig:  {{ user.name }}           - escaped
// Twig:  {{ user.name|raw }}       - raw (only for trusted content)

Why

  • Prevents XSS: Malicious scripts can't execute through escaped output
  • Context-Aware: HTML, JavaScript, URL, and CSS each need different escaping
  • ENT_QUOTES: Escapes both single and double quotes
  • UTF-8: Always specify encoding to prevent multi-byte attacks
  • Template Engines: Use frameworks with auto-escaping (Blade, Twig)

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