Introduction: Why Strings and Numbers in PHP is an Eternal Drama
\nImagine: you're writing a delivery calculator, and the user enters the quantity of goods as a string "5". You try to add 10 — and get "510" instead of 15. Sound familiar? Welcome to the world of type conversion in PHP!
The question "How do I convert a string to a number in PHP?" is one of the most popular on Stack Overflow (over 2 million views). And for good reason: PHP is known for its implicit typing, which both simplifies life and creates pitfalls.
\nIn this guide, we'll cover 5 reliable ways to convert a string to a number, debunk myths, and learn how to avoid bugs. Let's go!
\n\nTheory: What Happens Under the Hood in PHP?
\nPHP is a dynamically typed language. A variable can be a string, and a second later, a number. But there's a catch:
\n- \n
- Implicit type casting — PHP decides how to interpret the data itself (and often gets it wrong). \n
- Explicit type casting — you take control into your own hands. \n
When you write "5" + 10, PHP automatically converts the string "5" to a number. But try "5 apples" + 10 — and you get 15 (PHP silently discards " apples"). And "apples 5" + 10 gives 10 (the string is treated as zero). Chaos? Yes. But we'll bring order.
Method 1: Type Casting Operator (int) and (float)
\nThe fastest and simplest way is to use casting:
\n<?php\n$string = "42.8";\n$int = (int)$string; // 42 (integer part)\n$float = (float)$string; // 42.8\n\necho $int; // 42\necho $float; // 42.8\n?>\nImportant: (int) discards the fractional part without rounding. For rounding, use round() after conversion.
When to use: when you are sure the string contains only a number and you need a quick cast.
\n\nMethod 2: Functions intval() and floatval()
\nThese are the "big brothers" of casting — more readable and flexible:
\n<?php\n$string = "123.45abc";\n\n$int = intval($string); // 123\n$float = floatval($string); // 123.45\n\n// With a specified base (only for intval)\n$hexString = "1A";\n$hexInt = intval($hexString, 16); // 26 (converts hexadecimal string)\n\necho $int; // 123\necho $float; // 123.45\necho $hexInt; // 26\n?>\nFeature: intval() can work with different number bases (binary, octal, hexadecimal). floatval() works only with decimal.
When to use: when you need to handle "garbage" at the end of a string or work with non-standard number bases.
\n\nMethod 3: Function settype() — Conversion with true/false Return
\nWant not only to convert but also to check the success of the operation? Use settype():
<?php\n$string = "99.9";\n$result = settype($string, "float"); // true\n\necho $string; // 99.9 (now it's a float)\necho $result; // 1 (true)\n\n// If the string is non-numeric\n$badString = "hello";\n$result = settype($badString, "integer"); // false\n\necho $badString; // 0 (converted to zero)\necho $result; // (empty, false)\n?>\nImportant: settype() modifies the variable itself, not returning a new value. Useful when you need to guarantee the type has changed.
When to use: when you need to verify the success of the conversion (e.g., with user input).
\n\nMethod 4: Arithmetic Tricks (and Why It's Better Not to Use Them)
\nYes, you can write $number = $string + 0 — PHP will convert the string to a number itself. Or $number = $st