How to Convert a String to a Number in PHP: 5 Working Methods

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Strings and Numbers in PHP is an Eternal Drama

\n

Imagine: 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!

\n

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.

\n

In 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\n

Theory: What Happens Under the Hood in PHP?

\n

PHP 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
\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.

\n\n

Method 1: Type Casting Operator (int) and (float)

\n

The 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?>
\n

Important: (int) discards the fractional part without rounding. For rounding, use round() after conversion.

\n

When to use: when you are sure the string contains only a number and you need a quick cast.

\n\n

Method 2: Functions intval() and floatval()

\n

These 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?>
\n

Feature: intval() can work with different number bases (binary, octal, hexadecimal). floatval() works only with decimal.

\n

When to use: when you need to handle "garbage" at the end of a string or work with non-standard number bases.

\n\n

Method 3: Function settype() — Conversion with true/false Return

\n

Want not only to convert but also to check the success of the operation? Use settype():

\n
<?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?>
\n

Important: settype() modifies the variable itself, not returning a new value. Useful when you need to guarantee the type has changed.

\n

When to use: when you need to verify the success of the conversion (e.g., with user input).

\n\n

Method 4: Arithmetic Tricks (and Why It's Better Not to Use Them)

\n

Yes, you can write $number = $string + 0 — PHP will convert the string to a number itself. Or $number = $st

Blogs

Book Recommendations