0
0
PHPprogramming~30 mins

DNF types (Disjunctive Normal Form) in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
DNF types (Disjunctive Normal Form) in PHP
📖 Scenario: Imagine you are building a simple rule checker for a game. The rules are written in a way that combines conditions with AND and OR. This is called Disjunctive Normal Form (DNF). You want to represent these rules in PHP and check if a player's attributes match any of the rules.
🎯 Goal: You will create a PHP program that stores rules in DNF format, sets a player's attributes, checks which rules the player matches, and prints the matching rules.
📋 What You'll Learn
Create an array called $rules that holds multiple rules in DNF format.
Create an associative array called $player with specific attributes.
Write a loop to check each rule against the player's attributes using DNF logic.
Print the indexes of the rules that the player matches.
💡 Why This Matters
🌍 Real World
DNF is used in logic and computer science to simplify complex conditions. This project shows how to represent and check such conditions in code.
💼 Career
Understanding DNF and condition checking is useful for roles in software testing, rule engines, game development, and any job involving complex decision logic.
Progress0 / 4 steps
1
Create the rules array in DNF format
Create an array called $rules with two rules. Each rule is an array of clauses. Each clause is an associative array with keys 'color' and 'size'. Use these exact values: First rule: one clause with 'color' => 'red' and 'size' => 'large'. Second rule: two clauses, first with 'color' => 'blue' and 'size' => 'small', second with 'color' => 'green' and 'size' => 'medium'.
PHP
Need a hint?

Remember, DNF means OR of ANDs. Each rule is an OR clause, each clause inside is an AND condition.

2
Create the player attributes array
Create an associative array called $player with these exact keys and values: 'color' => 'blue', 'size' => 'small'.
PHP
Need a hint?

Use an associative array with keys 'color' and 'size' exactly as shown.

3
Check which rules the player matches
Write a loop using foreach with variables $index and $rule to iterate over $rules. Inside, write another loop with variables $clauseIndex and $clause to iterate over $rule. For each $clause, check if all keys and values match exactly with $player. If any clause matches, add $index to an array called $matchedRules. Initialize $matchedRules as an empty array before the loops.
PHP
Need a hint?

Check each clause fully matches the player. If yes, add the rule index to $matchedRules and stop checking that rule.

4
Print the matching rule indexes
Write a print_r statement to display the $matchedRules array.
PHP
Need a hint?

Use print_r($matchedRules); to show the array of matched rule indexes.