Bird
0
0

Which of the following is the correct way to cast a variable $var to a boolean in PHP?

easy📝 Conceptual Q1 of 15
PHP - Type Handling
Which of the following is the correct way to cast a variable $var to a boolean in PHP?
Abool($var)
B(bool) $var
Ccast bool $var
D$var as bool
Step-by-Step Solution
Solution:
  1. Step 1: Understand PHP casting syntax

    PHP uses parentheses with the type name before the variable to cast types, e.g., (bool) $var.
  2. Step 2: Evaluate each option

    (bool) $var uses correct syntax. bool($var) looks like a function call, which is invalid. cast bool $var and D are not valid PHP syntax.
  3. Final Answer:

    (bool) $var -> Option B
  4. Quick Check:

    Type casting boolean = (bool) $var [OK]
Quick Trick: Use (type) before variable to cast in PHP [OK]
Common Mistakes:
  • Using function-like syntax for casting
  • Writing 'as' keyword for casting
  • Omitting parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes