Introduction: A Small Detail, a Big Difference
\n\nIt would seem, what could be simpler than turning a number into a string? PHP is a dynamically typed language, and it handles contextual conversion perfectly well on its own. But the devil, as always, is in the details. On Stack Overflow, the question "Converting an integer to a string in PHP" has gathered thousands of views and dozens of heated discussions. Because behind the apparent simplicity lies a whole arsenal of methods, each with its own cost — execution time and memory usage.
\n\nIn this article, we will not just list the ways to do integer to string php. We will conduct our own measurements, dissect the inner workings of the Zend Engine, and give practical advice that will be useful for both beginners and experienced developers optimizing high-load projects. Get your IDEs ready — it's going to be intense!
\n\nClassic Methods: From Simple to Complex
\n\nPHP has at least four main ways to turn a number into a string. Let's break down each one, starting with the most obvious.
\n\n1. Implicit Casting
\n\nThe simplest way is to trust PHP. When you use a number in a string context (for example, during concatenation with a dot), the interpreter itself performs the conversion of integer to string:
\n\n$number = 42;\n$text = "Answer: " . $number; // $text = "Answer: 42"\n\n\nThis is convenient, but you don't control the process. If you just want to get a string, not concatenate it with something, this approach is not suitable.
\n\n2. Explicit Type Casting
\n\nThe classic casting in php is the (string) operator:
$number = 123;\n$string = (string) $number; // "123"\n\n\nThis method is considered "canonical." It explicitly tells the compiler: "Take this variable and interpret it as a string." Subjectively, it is the most readable option.
\n\n3. The strval() Function
\n\nThe specialized function strval() does exactly the same thing as casting, but in the form of a function call:
$number = 456;\n$string = strval($number); // "456"\n\n\nSome developers prefer functions for code consistency, but we will soon see if there is a difference in speed.
\n\n4. The settype() Function
\n\nAn exotic method that changes the type of the original variable rather than creating a new one:
\n\n$number = 789;\nsettype($number, "string"); // $number is now "789"\n\n\nThis method is dangerous because it modifies the original data. It is rarely used, but it's worth knowing about.
\n\nBenchmarks: Who is Faster?
\n\nThat's the theory, but in the world of php code optimization, numbers decide. We ran a simple test: we executed each method 1,000,000 times and measured the time using microtime(true). Here are the results (in order of increasing speed):
- \n
- settype() — the slowest. Modifying a variable "in place" requires additional checks and allocations. Forget about it. \n
- strval() — good, but slightly slower than casting. The function call has a small overhead. \n
- (string) $var — the winner among explicit methods. It works at the opcode level
CAST, without unnecessary overhead. \n - Concatenation with an empty string (
$var . '') — a surprise! In some versions of PHP (7.4 and below), this method even outperformed casting. In PHP 8.x, it became equal to(string). \n
// Example for your own test\n$iterations = 1000000;\n$number = 12345;\n\n$start = microtime(true);\nfor ($i = 0; $i < $iterations; $i++) {\n $str = (string) $number;\n}\necho "Cast: " . (microtime(true) - $start) . " sec\\n";\n\n$start = microtime(true);\nfor ($i = 0; $i < $iterations; $i++) {\n $str