0
0
PHPprogramming~3 mins

Why DNF types (Disjunctive Normal Form) in PHP? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could tame wild, tangled conditions into neat, easy-to-understand groups?

The Scenario

Imagine you have to check many different conditions for a user input manually, like if the input is a number or a string, and if it meets several rules combined with ANDs and ORs.

Writing all these checks by hand can get very confusing and messy.

The Problem

Manually writing complex condition checks is slow and easy to mess up.

You might forget a case or mix up ANDs and ORs, causing bugs that are hard to find.

The Solution

DNF types let you organize conditions clearly as a list of OR groups, each with AND conditions inside.

This makes your code easier to read, check, and maintain.

Before vs After
Before
$valid = (is_int($x) && $x > 0) || (is_string($x) && strlen($x) > 3);
After
type DNF = array<array<string>>; // groups of conditions combined with AND inside OR groups
What It Enables

It lets you handle complex condition checks cleanly and safely, avoiding mistakes and saving time.

Real Life Example

Validating user input where multiple different formats are allowed, like a number in a range OR a string with certain length.

Key Takeaways

Manual condition checks get confusing fast.

DNF types organize conditions as OR of AND groups.

This makes code clearer and less error-prone.