Bird
Raised Fist0
PHPprogramming~10 mins

Gettype and typeof checks in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the type of the variable $value using gettype.

PHP
<?php
$value = 123;
$type = [1]($value);
echo $type;
?>
Drag options to blanks, or click blank then click option'
Atype_of
Btypeof
Cgettype
Dtype
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'typeof' which is not a PHP function.
Trying to use 'type' as a function.
2fill in blank
medium

Complete the code to check if the variable $value is of type integer using gettype.

PHP
<?php
$value = 10;
if (gettype($value) === [1]) {
    echo 'Integer detected';
}
?>
Drag options to blanks, or click blank then click option'
A'integer'
B'int'
C'number'
D'digit'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' instead of 'integer'.
Using 'number' which is not returned by gettype.
3fill in blank
hard

Fix the error in the code to correctly check the type of $value using gettype.

PHP
<?php
$value = 3.14;
if ([1]($value) == 'double') {
    echo 'Float detected';
}
?>
Drag options to blanks, or click blank then click option'
Atypeof
Btype
Ctype_of
Dgettype
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'typeof' which is not a PHP function.
Using 'type' or 'type_of' which do not exist.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, only for words longer than 3 characters.

PHP
<?php
$words = ['apple', 'cat', 'banana', 'dog'];
$lengths = array();
foreach ($words as $word) {
    if (strlen($word) [1] 3) {
        $lengths[$word] = [2]($word);
    }
}
print_r($lengths);
?>
Drag options to blanks, or click blank then click option'
A>
Bstrlen
C<
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the condition.
Using 'count' instead of 'strlen' for string length.
5fill in blank
hard

Fill all three blanks to create an associative array with uppercase keys and values only if the value is a string.

PHP
<?php
$data = ['name' => 'John', 'age' => 30, 'city' => 'Paris'];
$result = array();
foreach ($data as [1] => [2]) {
    if (gettype([2]) === 'string') {
        $result[strtoupper([1])] = [2];
    }
}
print_r($result);
?>
Drag options to blanks, or click blank then click option'
Akey
Bvalue
C$value
D$key
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without '$'.
Mixing up key and value variable names.

Practice

(1/5)
1. What does the PHP function gettype() return when called on a variable?
easy
A. A string describing the variable's type
B. An integer representing the type code
C. A boolean indicating if the variable is set
D. An object describing the variable

Solution

  1. Step 1: Understand the purpose of gettype()

    The gettype() function returns the type of a variable as a string, such as "integer", "string", or "array".
  2. Step 2: Compare options with gettype() behavior

    The option stating that it returns a string describing the variable's type is correct.
  3. Final Answer:

    A string describing the variable's type -> Option A
  4. Quick Check:

    gettype() = string description [OK]
Hint: Remember: gettype() always returns a string type name [OK]
Common Mistakes:
  • Thinking gettype() returns a numeric code
  • Confusing gettype() with isset()
  • Assuming gettype() returns an object
2. Which of the following is the correct syntax to get the type of a variable $var using gettype() in PHP?
easy
A. gettype $var;
B. typeof $var;
C. typeof($var);
D. gettype($var);

Solution

  1. Step 1: Recall correct function syntax in PHP

    PHP functions require parentheses around arguments, so gettype($var); is correct syntax.
  2. Step 2: Check other options for syntax errors

    Syntax using typeof is invalid because it is not a PHP function. gettype $var; misses parentheses, which is invalid syntax.
  3. Final Answer:

    gettype($var); -> Option D
  4. Quick Check:

    Function call syntax = gettype($var) [OK]
Hint: Use parentheses with function calls in PHP [OK]
Common Mistakes:
  • Using typeof instead of gettype()
  • Omitting parentheses in function calls
  • Confusing PHP syntax with JavaScript
3. What will be the output of the following PHP code?
$x = 3.14;
echo gettype($x);
medium
A. integer
B. double
C. float
D. string

Solution

  1. Step 1: Identify the variable type

    The variable $x is assigned 3.14, which is a floating-point number.
  2. Step 2: Understand gettype() output for floats

    In PHP, gettype() returns "double" for floating-point numbers, not "float".
  3. Final Answer:

    double -> Option B
  4. Quick Check:

    gettype(3.14) = double [OK]
Hint: Remember: PHP calls floats "double" in gettype() output [OK]
Common Mistakes:
  • Expecting output to be "float" instead of "double"
  • Confusing integer and float types
  • Assuming gettype() returns "string" for numbers
4. Consider this PHP code snippet:
$value = true;
if (typeof($value) === 'boolean') {
echo 'Boolean type';
} else {
echo 'Not boolean';
}

What is the problem with this code?
medium
A. typeof is not a valid PHP function
B. Comparison operator === cannot be used with strings
C. Boolean values cannot be checked with typeof
D. The variable $value is not defined

Solution

  1. Step 1: Check if typeof is a PHP function

    PHP does not have a built-in typeof() function; it uses gettype() instead.
  2. Step 2: Analyze the impact of using typeof()

    Using typeof() causes a fatal error because the function does not exist.
  3. Final Answer:

    typeof is not a valid PHP function -> Option A
  4. Quick Check:

    typeof() invalid in PHP [OK]
Hint: Use gettype(), not typeof(), in PHP [OK]
Common Mistakes:
  • Assuming typeof() exists in PHP like in JavaScript
  • Thinking === cannot compare strings
  • Ignoring variable initialization
5. In PHP 8.0+, the typeof operator returns a Type object instead of a string. Given this code:
$var = [1, 2, 3];
$type = typeof $var;
echo $type->name;

What will be the output?
hard
A. Type
B. object
C. error
D. array

Solution

  1. Step 1: Verify typeof in PHP 8.0+

    PHP has no typeof operator or function in 8.0+ or any version; use gettype() instead.
  2. Step 2: Predict code execution

    The undefined typeof causes a fatal error (e.g., "Call to undefined function"). No echo output.
  3. Final Answer:

    error -> Option C
  4. Quick Check:

    typeof invalid in PHP [OK]
Hint: PHP has no typeof; use gettype() [OK]
Common Mistakes:
  • Believing typeof exists in PHP 8.0+
  • Confusing PHP with JavaScript's typeof
  • Expecting a Type object return
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard