Skip to Content
Learn
Booleans and Comparison Operators
Identical and Not Identical Operators

In this exercise we’re going to learn a few more comparison operators and see how we can use them to compare more than just number values.

The identical operator (===) will return TRUE if the left operand is the same as the right operand and FALSE if it’s not:

$num = 5; $num === 5; // Evaluates to: TRUE 10 === 10; // Evaluates to: TRUE $num === 20; // Evaluates to: FALSE

When we think about comparing two values, we’ll need to think like a computer. Are "hello" and "Hello" the same?

$greeting = "hello"; $greeting === "hello"; // Evaluates to: TRUE "hello" === "hel" . "lo"; // Evaluates to: TRUE $greeting === "HELLO"; // Evaluates to: FALSE

The not identical operator (!==) will return TRUE if the two operators are different and FALSE if they’re the same:

$num = 5; $num !== 5; // Evaluates to: FALSE 10 !== 10; // Evaluates to: FALSE $num !== 20; // Evaluates to: TRUE $greeting = "hello"; "hello" !== "hello"; // Evaluates to: FALSE $greeting !== "HELLO"; // Evaluates to: TRUE

When looking through PHP code, you may encounter another operator—the equal operator (==). Like the identical operator, the equal operator will return TRUE if the left operand is the same as the right operand and FALSE if it’s not. But the equal operator is less strict than the identical operator and can have some hard to predict results, so we prefer to only use the identical operator.

Awesome. Let’s practice!

Instructions

1.

Write a function agreeOrDisagree() that takes in two strings, and returns "You agree!" if the two strings are the same and "You disagree!" if the two strings are different.

2.

Test your function! Invoke it once with a value that will result in the if block running and once with a value that won’t.

3.

You’re going to write a function to check if it’s time for a user to renew their subscription.

Write a function checkRenewalMonth() that takes in a user’s renewal month as a string (e.g. "January").

Your function should get the current month using the PHP built-in date() function (see the hint for help with this). It should compare the current month to the renewal month passed in. If the renewal month is not the current month, the function should return the string "Welcome!". Otherwise it should return the string "Time to renew".

4.

Test your function! Invoke it once with a value that will result in the if block running and once with a value that won’t.

Folder Icon

Take this course for free

Already have an account?