Bird
0
0

You want to check if a variable $data is an integer or a float in PHP. Which code snippet correctly uses gettype() to do this?

hard📝 Application Q8 of 15
PHP - Type Handling
You want to check if a variable $data is an integer or a float in PHP. Which code snippet correctly uses gettype() to do this?
Aif (gettype($data) == "number") { echo "Number"; }
Bif (gettype($data) == "integer" || gettype($data) == "double") { echo "Number"; }
Cif (gettype($data) == "int" || gettype($data) == "float") { echo "Number"; }
Dif (typeof($data) == "int" || typeof($data) == "float") { echo "Number"; }
Step-by-Step Solution
Solution:
  1. Step 1: Recall gettype() returns "integer" and "double" for int and float

    In PHP, floats are called "double" by gettype().
  2. Step 2: Check each option's type strings

    if (gettype($data) == "integer" || gettype($data) == "double") { echo "Number"; } uses correct strings "integer" and "double". Others use invalid or non-PHP strings.
  3. Final Answer:

    if (gettype($data) == "integer" || gettype($data) == "double") { echo "Number"; } -> Option B
  4. Quick Check:

    Floats = "double" in gettype() [OK]
Quick Trick: Floats show as "double" in gettype(), not "float" [OK]
Common Mistakes:
  • Using typeof() in PHP
  • Checking for "float" instead of "double"
  • Using "number" as type string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes