Bird
0
0

Which of the following is the correct syntax to cast a variable $value to a float in PHP?

easy📝 Syntax Q12 of 15
PHP - Type Handling
Which of the following is the correct syntax to cast a variable $value to a float in PHP?
A$floatValue = (float) $value;
B$floatValue = cast float $value;
C$floatValue = float($value);
D$floatValue = (float)$value[];
Step-by-Step Solution
Solution:
  1. Step 1: Recall PHP type casting syntax

    PHP uses (type) before a variable to cast it, so (float) $value is correct.
  2. Step 2: Check each option

    $floatValue = float($value); uses a function-like syntax which is invalid. $floatValue = cast float $value; uses invalid syntax. $floatValue = (float)$value[]; tries to cast an array element incorrectly.
  3. Final Answer:

    $floatValue = (float) $value; -> Option A
  4. Quick Check:

    Correct cast syntax is (float) $value [OK]
Quick Trick: Use (type) before variable for casting in PHP [OK]
Common Mistakes:
  • Using function call syntax like float($value)
  • Writing cast without parentheses
  • Adding invalid array brackets after cast

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes