AI writes PHP with confidence, but not always with truth.
Sometimes it invents features, borrows patterns from other languages, or fills gaps with assumptions that feel plausible but have no grounding in PHP itself.
These moments aren’t bugs
— they’re signals.
They reveal how the model reasons, what it prioritizes, and where its internal map of PHP becomes blurry.
This page explores why hallucinations happen, how to recognize them early, and how to guide AI back into clarity.
AI doesn’t "know" PHP. It predicts patterns based on statistical relationships in its training data.
Hallucinations happen when:
In other words:
AI is not lying
— it’s completing a pattern
that looks right to the model.
AI tends to invent things that feel plausible
but don’t exist in the language.
Invented features
These are real in other ecosystems
— just not in PHP.
Invented syntax
Invented behaviors
These hallucinations are consistent, predictable, and easy to spot once we know the patterns.
AI often carries assumptions that come from older PHP
or from other languages entirely.
Assumption:
arrays are the default data structure
AI overuses arrays because they’re flexible and common in older PHP codebases.
Assumption:
static methods are convenient
AI leans on static methods because they’re easy to call
— but they break modern architecture.
Assumption:
docblocks are required for typing
AI sometimes duplicates types in docblocks because it learned from mixed‑era code.
Assumption:
exceptions are optional
AI may ignore error handling because many examples in the wild do.
Assumption:
everything needs a factory
AI sometimes over‑engineers because it learned from frameworks with heavy ceremony.
These assumptions aren’t malicious
— they’re inherited.
Hallucinations have a particular “feel.” They often appear when the code:
A simple test:
"Does this look like something I’ve seen in real PHP code?"
If the answer is no, it’s probably a hallucination.
AI responds best to calm, structural guidance.
Examples:
"Rewrite this using PHP 8+ features only."
"Avoid patterns from other languages."
"Use real PHP constructs only — no invented syntax."
"Replace this with a simple, explicit implementation."
"Use a DTO instead of an invented feature."
We’re not correcting the code
— we’re correcting the mental model the AI is using.
We can reduce hallucinations dramatically by giving AI:
Examples:
"Use PHP 8.2 idioms only."
"No patterns from JavaScript or Python."
"No invented features or syntax."
"Use real, documented PHP constructs only."
"Prefer clarity over cleverness."
AI stays grounded when the request is grounded.
Sometimes a hallucination reveals:
AI occasionally invents something that sparks a better idea
— not because it’s correct, but because it’s suggestive.
Use these moments as inspiration, not implementation.
AI hallucinates because it predicts patterns,
not truth.
In PHP, this leads to:
We can recognize hallucinations by their foreignness,
cleverness, or inconsistency.
We correct them by guiding the model’s mental frame,
not by patching the code.
AI drafts.
We provide the grounding.
Together, we create clarity.
This page describes common behaviors observed in AI models when generating PHP code.
These patterns are not guarantees, but they are consistent enough to be useful when reviewing or guiding AI output.