6.3 · Best practices & pitfalls

Level 6 · In practice

6.3Best practices & pitfalls

Goal: turn technical mastery of PHP into reliable, safe and maintainable production habits.
Estimated time: 14 min · If you only have 5 min: read “The essentials in 30s” + the diagram.

The essentials in 30 seconds

  • Golden rule: clean code, systematic distrust of inputs, structure and delivery — safety pays, never raw speed.
  • Do: === and strict_types, prepared statements + escaping, MVC + Composer, password_hash + .env, PHPUnit, measure then deliver.
  • Avoid: SQL concatenation, raw display, plain-text secrets, client-side-only validation, neglected tests, exposed errors.

Riad thread — three habits that pay

After one year, Riad draws his conclusions: what counted most was not a framework but three habits — comparing strictly and typing, preparing every query and escaping every display, and delivering all the way with tests and deployment. And his biggest narrowly avoided pitfall: a plain-text stored password during a rush week.

Pick a single habit to anchor in your ritual, then move to the next.

What makes the difference

The difference between tinkering and professional practice is not the tools but the reflexes: writing clean code, never trusting inputs, structuring, testing, and delivering.

✓ Do

  • Write modern PHP: === by default, typing (declare(strict_types=1)), single-responsibility functions.
  • Never trust inputs: server-side validation, prepared statements, escape on display.
  • Structure (MVC, logic/display separation) and leverage Composer (autoloading, battle-tested libraries).
  • Hash passwords (password_hash), protect secrets (environment variables), HTTPS, authorisation.
  • Write tests (PHPUnit) on critical logic; measure before optimising.
  • Rely on frameworks (default protections) and deliver all the way (deployment, CI/CD).

✗ Avoid

  • Concatenating data into SQL (injection) or displaying raw data (XSS).
  • Storing a plain-text password or a secret in the Git repo.
  • Relying on client-side validation (bypassable); mixing logic and display (spaghetti).
  • Neglecting tests (“it works”) and letting silent regressions through.
  • Premature optimisation instead of measuring and targeting; over-engineering.
  • Displaying detailed errors in production (information disclosure).
Common misinterpretation: “ticking every box each day”. No. These are habits to install one by one: pick one (e.g. prepared statements everywhere), anchor it in your ritual, then move to the next.
Text version of the diagram (SEO / screen readers / translation): Balance to hold: 1) Code side — strict_types, === by default, single-responsibility functions. 2) Data side — server validation, prepared statements, htmlspecialchars, CSRF. 3) Production side — password_hash + .env + HTTPS + authorisation, PHPUnit, measure before optimising, frameworks and CI/CD. Lesson: clean, safe, maintainable and delivered applications beat fast code never deployed.

Ready-to-say script — checking your practice before delivering (30 s)

“strict_types and ===? Inputs validated server-side, SQL prepared, display escaped, CSRF? password_hash, secrets in .env, HTTPS, authorisation? PHPUnit green, errors logged? If any no, I fix before delivering.”

Copy-paste template — safe sign-up (hashing + prepared)

prepare("INSERT INTO users (email, hash) VALUES (:email, :hash)");
$stmt->execute(["email" => $email, "hash" => $hash]);
function verifier(string $saisi, string $hash): bool {
    return password_verify($saisi, $hash);
}

VSE / small budget

One single habit per quarter is enough: prepared statements first, the rest after.

SME Constantine

Adapt rituals to peaks and travel: a code-review and test slot held every week.

Remote / hybrid

The #1 discipline at a distance: written reviews and green tests, even for small fixes.

Intercultural

Check local practices rather than assume: an unusual authorisation rule must raise flags everywhere.

Caution: General reminder: passwords, secrets and user data engage your organisation — hash with password_hash, put secrets in environment variables and have the designated contact validate; this guide gives no legal advice.

48h challenge — apply tomorrow

Screen your main page: zero SQL concatenation, zero raw display, zero versioned secret. Fix the first gap found.

Section exit checklist
  • I know the reflexes to adopt and the pitfalls to avoid.
  • I checked my 3 critical points (SQL, display, secrets).
  • I did the 48h challenge or scheduled its date.
Self-assessment 1-5: consistency of good reflexes?pitfalls avoided?
Check your understanding

Before delivering, Riad checks his practice. Which reflex protects him best?

💡

Golden rule: a good PHP developer is not the one who codes fastest, but the one whose applications are clean, safe, maintainable and actually delivered to production.