How to Convert a String to a Number in PHP: A Step-by-Step Guide

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 won't strings add up?

\n\n

Every developer who has written PHP at least once has encountered a situation where instead of the expected number, a strange result appears. You try to add two variables obtained from a form or database, and PHP outputs something like 1520 instead of 35. Sound familiar? This is a classic problem: converting a string to a number in PHP.

\n\n

PHP is a dynamically typed language. This is both a blessing and a curse. On one hand, it tries to guess what you mean and automatically converts types. On the other hand, such "guesswork" often leads to hard-to-find bugs. Today we will cover all the ways to convert a string to a number in PHP — from simple to advanced — and learn how to do it correctly.

\n\n

Short answer: 4 ways in 30 seconds

\n\n

If you need a quick solution — here is a cheat sheet. But don't rush to close the article: each method has its own features and pitfalls.

\n\n
<?php\n// Method 1: Explicit type casting\n$number = (int) "42";          // int(42)\n$float  = (float) "3.14";     // float(3.14)\n\n// Method 2: intval() function\n$number = intval("42");       // int(42)\n\n// Method 3: floatval() / doubleval() function\n$float  = floatval("3.14");   // float(3.14)\n\n// Method 4: Using settype()\n$str = "42";\nsettype($str, "integer");     // now $str = int(42)\n?>
\n\n

All four methods work, but they behave differently in edge cases. Let's examine each one in detail.

\n\n

1. Explicit type casting: (int) and (float)

\n\n

The fastest and most obvious way to convert a string to a number in PHP is to use type casting operators. They work like a "forced switch" of the variable's type.

\n\n

Examples with (int)

\n\n
<?php\necho (int) "42";           // 42\necho (int) "3.99";         // 3 (fractional part is truncated!)\necho (int) "42 apples";    // 42 (number at the beginning)\necho (int) "apple 42";    // 0 (cannot recognize a number)\necho (int) "";             // 0\necho (int) null;           // 0\n?>
\n\n

Important: PHP tries to find a number at the beginning of the string. If the string starts with letters, the result will be 0. This behavior often causes errors, especially when working with user input.

\n\n

Examples with (float)

\n\n
<?php\necho (float) "3.14";       // 3.14\necho (float) "3,14";       // 3.0 (comma does not work!)\necho (float) "2.99 euro";  // 2.99\necho (float) "abc";        // 0\n?>
\n\n

Pro tip: Remember that PHP uses a dot as the decimal separator. If users enter numbers with a comma (as is common in Russia), you need to first replace the comma with a dot using str_replace(',', '.', $str).

\n\n

2. intval() and floatval() functions

\n\n

These functions are a more "intelligent" version of explicit casting. They do the same thing but allow you to specify the number base for intval().

\n\n
<?php\n// intval() — conversion to integer\necho intval("42");            // 42\necho intval("1010", 2);       // 10 (binary system!)\necho intval("FF", 16);        // 255 (hexadecimal)\necho intval("42.99");         // 42 (fractional part is truncated)\n\n// floatval() — conversion to floating point number\necho floatval("3.14");        // 3.14\necho floatval("2.71828");     // 2.71828\n?>
\n\n

When to use: intval() is indispensable if you are working with numbers in different number systems (e.g., parsing HEX color codes). For regular numbers, there is practically no difference from (int).

\n\n

3. settype() function: in-place conversion

\n\n

Unlike the previous methods, settype() does not return a new value, but

Blogs

Book Recommendations