DEV Community

Cover image for PHP Punishes You for Writing Good Code
Serge Matveenko
Serge Matveenko

Posted on

PHP Punishes You for Writing Good Code

Most programming languages guide you toward good code.

PHP… doesn’t.

In fact, PHP often feels like it actively punishes you for trying to write clean, safe, maintainable software — while rewarding shortcuts that age horribly in production.

This isn’t about syntax preferences or “PHP vs X” flamewars.
It’s about direction of least resistance.

In good languages, writing good code is the easiest path.
In bad languages, writing bad code is the easiest path.

PHP is a masterclass in the second category.


Direction of Least Resistance

Every language nudges you in a certain direction.

Some push you toward correctness.
Some push you toward chaos.

The language you use shapes your habits — and often your codebase’s long-term fate.

Rust: Least resistance → correctness

Rust actively fights you when your design is unsafe.

If you:

  • forget ownership rules,
  • share mutable state incorrectly,
  • misuse lifetimes,
  • ignore concurrency safety,

…it refuses to compile.

Rust doesn’t trust you — and that’s a feature.

Yes, it’s harder to write code at first.
But the language is aligned with your intent.

Good design is the shortest path to success.


Python: Least resistance → readability

Python is permissive — but not anarchic.

You can write awful Python, but you must work at it.

Idiomatic Python naturally pushes you toward:

  • simple structures,
  • predictable flow,
  • readable naming,
  • explicit behavior.

Example:

# Python: obvious, readable, boring (perfect)
users = [u for u in users if u.is_active]
Enter fullscreen mode Exit fullscreen mode

Trying to outsmart Python usually makes your code uglier, not shorter — which nudges you back toward clarity.


PHP: Least resistance → disaster

PHP’s “happy path” is often:

  • global state
  • weak typing
  • silent failures
  • inconsistent APIs
  • magical behavior
  • legacy defaults

You can write good PHP.

But PHP makes you work for it.


PHP Makes Bad Code Easy

Let’s look at what happens when you do the lazy thing in PHP.

1. Everything is globally accessible

$_GET['id']
$_POST['password']
$_SESSION['user']
Enter fullscreen mode Exit fullscreen mode

No structure.
No boundaries.
No encapsulation.

The language encourages direct access to state from anywhere.

Good design requires:

  • request objects
  • input validation layers
  • parameter objects
  • DTOs

Bad design requires… nothing.


2. Type safety is optional (and mostly ignored)

PHP has types.

The ecosystem largely ignores them.

By default:

function add($a, $b) {
    return $a + $b;
}
Enter fullscreen mode Exit fullscreen mode

What are $a and $b?

Integers?
Floats?
Strings?
Objects with __toString()?
Null?

Good PHP requires:

function add(int $a, int $b): int {
    return $a + $b;
}
Enter fullscreen mode Exit fullscreen mode

But now:

  • every call site must cooperate,
  • legacy code breaks,
  • frameworks fight you,
  • plugins betray you,
  • runtime errors appear instead of coercion.

PHP punishes you for being strict.


3. Errors default to… ignoring errors

$result = file_get_contents($url);
Enter fullscreen mode Exit fullscreen mode

What if:

  • the file doesn’t exist?
  • the request fails?
  • permissions are wrong?
  • timeout occurs?

The function can fail silently.

No exception.
No structured error.
Just false.

Now every clean implementation requires:

if ($result === false) {
    throw new RuntimeException("Failed to fetch file");
}
Enter fullscreen mode Exit fullscreen mode

Meanwhile, the “dirty” version works… until it doesn't.


4. Frameworks simulate what the language doesn’t provide

Why do PHP frameworks feel so heavy?

Because they fake:

  • type systems
  • DI containers
  • routing DSLs
  • validation layers
  • request lifecycles
  • security models

They are duct tape on a language that refuses to grow a spine.

And when you follow best practices?
You fight:

  • configuration hell
  • annotation magic
  • runtime surprises
  • reflection hacks

The “right” way is the hardest way.


Cargo Cult vs First-Class Safety

Rust and Python make correctness normal.

PHP makes correctness custom.

In PHP:

  • testing is extra
  • type safety is optional
  • architecture is emulated
  • tooling is external
  • contracts are implied
  • discipline is manual

In other words:

PHP works great if you don’t care deeply about software engineering.

And becomes expensive when you do.


So Why Is PHP Popular?

Because:

✅ You can deploy something in 10 minutes
✅ You don’t need a build system
✅ You don’t need to understand types
✅ You don’t need architecture
✅ You don’t need experience
✅ You don’t need discipline

And:

❌ You pay for all of that later.

Popularity correlates strongly with:

  • low barrier to entry,
  • cheap prototypes,
  • instant gratification.

Not with:

  • long-term maintainability,
  • safety,
  • correctness.

The Most Important Question

Maybe this is the real reason some languages are popular
— and others are trusted.

PHP optimizes for:

  • shipping now
  • understanding later
  • fixing in production

Languages like Rust optimize for:

  • thinking now
  • shipping safely
  • trusting code later

Final Take

PHP does not force you to write bad code.

It just gently rewards you for it.

And the better you try to be…
the more friction you feel.

Top comments (1)

Collapse
 
xwero profile image
david duymelinck

This is obviously rage bait. So instead of focusing on PHP I want to concentrate on the quote.

In good languages, writing good code is the easiest path.
In bad languages, writing bad code is the easiest path.

All the masterful paintings started with a blank canvas and separate paint colors.
Sculptures started out as rock and a chisel.
Houses start as land.

There is nothing wrong creating something out of almost nothing. It shows true mastery if it is done right.

You can use a pass between mountains to build a road.
You can use a cave as a home.

But does it give you the same fulfillment after you completed the task?
Is it helpful, no doubt about it.

The problem is not the language, the problem is the lack of vision.