🐘 PHP Beginner

What is the difference between intval(), floatval(), and strval()?

Why Interviewers Ask This

This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex PHP topics. It also reveals how well you can explain technical ideas to non-experts.

Answer

These are PHP type-conversion functions. intval($var) converts a variable to an integer — a string like "42abc" becomes 42, "abc" becomes 0, and a float like 4.9 becomes 4 (truncated toward zero). floatval($var) (also doubleval()) converts to a float. strval($var) converts to a string — true becomes "1", false becomes "", null becomes "". Alternatively, PHP supports direct casting: (int)$var, (float)$var, (string)$var, (bool)$var, (array)$var, (object)$var. The settype($var, "integer") function modifies the variable in place. Always validate and sanitize user input before type conversion rather than relying on implicit coercion.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a PHP codebase.