0% complete

PHP Basics · Lesson 6 of 8

Booleans

Use true and false to represent conditions clearly.

Lesson

Boolean values are exactly true or false and are commonly produced by comparisons. Keep boolean conditions explicit; truthy values such as non-empty strings are not the same thing as the boolean value true. Strict comparisons help distinguish values of different types. Here a strict comparison returns a boolean that can be used directly in an if statement or stored as a feature flag. Further guidance Inspect a value before relying on an implicit conversion. Keep input validation separate from casting, use strict comparisons when type matters, and choose an explicit representation for values such as money or optional data. Try normal, empty, and unexpected inputs so the result is deliberate. Practice task: Create a fresh example for Booleans. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.

Code examples

Booleans
PHP
$isPublished = 1 === 1;
var_dump($isPublished);
Expected output
bool(true)

Key points

  • Use true and false to represent conditions clearly.
  • Validate external data before converting or using it.
  • Use strict comparisons when both value and type matter.

Your learning progress

Finish this lesson

Optional practice

PHP playground

This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.

Execution is disabled. No code is sent to a server.