0% complete

PHP Basics · Lesson 7 of 8

NULL

Use NULL to represent an intentionally absent value.

Lesson

NULL is a single value meaning that a variable has no value. It differs from false, zero, and an empty string. Use null coalescing when a missing value should receive a default, and check explicitly when null itself has meaning in your domain. The null coalescing operator reads the default only when the left side is absent or null. 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 NULL. 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

NULL
PHP
$displayName = null;
echo $displayName ?? 'Guest';
Expected output
Guest

Key points

  • Use NULL to represent an intentionally absent value.
  • 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.