🐘 PHP Beginner

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

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.