📖 Scenario: Imagine you are creating a simple PHP program to track a user's favorite color. Sometimes, the user might not have chosen a color yet, so the value should be empty or null.
🎯 Goal: You will learn how to use the null type in PHP to represent a variable that has no value. You will create a variable, assign null to it, check if it is null, and then print a message.
📋 What You'll Learn
Create a variable with the value null
Create a variable to hold a default color
Use an if statement to check if the first variable is null
Print the correct message based on the check
💡 Why This Matters
🌍 Real World
In real applications, <code>null</code> is used to represent missing or unknown data, like a user not choosing a preference yet.
💼 Career
Understanding <code>null</code> helps avoid errors and handle optional data safely in PHP programming jobs.
Progress0 / 4 steps
1
Create a variable with null
Create a variable called $favoriteColor and assign it the value null.
PHP
Hint
Use = null; to assign the null value.
2
Create a default color variable
Create a variable called $defaultColor and assign it the string 'blue'.
PHP
Hint
Use single quotes around the word blue.
3
Check if $favoriteColor is null
Write an if statement that checks if $favoriteColor is null using is_null() function.
PHP
Hint
Use is_null($favoriteColor) inside the if condition.
4
Print the color or default message
Inside the if block, print "Favorite color is not set, using default: " followed by the value of $defaultColor. Otherwise, print "Favorite color is: " followed by the value of $favoriteColor.
PHP
Hint
Use print() to show the messages exactly as given.
Practice
(1/5)
1. What does the null type represent in PHP?
easy
A. A variable with no value assigned
B. A variable holding the number zero
C. A variable holding an empty string
D. A variable holding a boolean false
Solution
Step 1: Understand the meaning of null in PHP
Null means the variable does not have any value assigned to it.
Step 2: Differentiate null from other values
Zero, empty string, and false are actual values, but null means no value.
Final Answer:
A variable with no value assigned -> Option A
Quick Check:
Null means no value = D [OK]
Hint: Null means no value, not zero or empty [OK]
Common Mistakes:
Confusing null with zero
Thinking null is an empty string
Assuming null is false
2. Which of the following is the correct way to assign a null value to a variable in PHP?
easy
A. $var = 'null';
B. $var == null;
C. $var := null;
D. $var = null;
Solution
Step 1: Identify correct assignment syntax in PHP
Assignment uses a single equals sign = in PHP.
Step 2: Check how to assign null
Null is assigned as null without quotes, so $var = null; is correct.
Final Answer:
$var = null; -> Option D
Quick Check:
Assign null with = null; = C [OK]
Hint: Use single = and null without quotes [OK]
Common Mistakes:
Using 'null' as a string
Using == instead of = for assignment
Using := which is invalid in PHP
3. What will be the output of the following PHP code?