Comparison

=== does not convert types, == does.

== compares after converting types. The string "0", the number 0 and false easily turn out "equal" in such a comparison. For a flag from a form that is already a hole: empty and the string zero suddenly mean the same thing.

=== compares both the value and the type. "0" === 0 is false. Input from a request almost always needs === or explicit parsing. !== is strict inequality. Get used to the pair === / !== in conditions.

<=> gives -1, 0 or 1 and is handy in sorting. In an ordinary if it only confuses. The ternary ?: treats many values as "empty": "0", 0, "", null, an empty array. The ?? operator fires only for a missing key and null. For a default value use ??, not ?:.

In the task you have to tell identical text from text that is equal only after conversion. Return the strings yes and no, not a boolean: printing a boolean in PHP gives 1 and an empty string, which are easy to mistake for garbage.

Practice

  1. Which operator is stricter

    You need to make sure the variable holds exactly the number 0, not the string "0". What do you compare with?

    • ==
    • ===
    • =
    • ?:
  2. The zero string

    Is the expression "0" == false true? Answer yes or no.

  3. Two operators

    Select the true statements.

    • ?? replaces only null and a missing key
    • ?: treats "0" as empty
    • === converts a string to a number
    • == never converts types