Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

String To Int

<?php

// more than 18 is sufficient
$json = '12345123451234512345';

var_dump(json_decode($json));  // a float
var_dump(json_decode($json, flags: JSON_BIGINT_AS_STRING));
// a string
var_dump(json_encode(json_decode($json, flags: JSON_BIGINT_AS_STRING)));
// string(22) ""12345123451234512345""
// with extra ""

Converting a string to an integer is a job for (int), or intval().

One classic gotcha happens with JSON decoding: the integer in the JSON string might be bigger than the largest PHP unsigned integer.

For that, there is the JSON_BIGINT_AS_STRING option to json_decode(), which keeps the number as a string instead of casting it to a float, for future processing in PHP, or not. PHP might hand out this string to a database that is able to handle it. When that integer is handed again to another JSON, via json_encode(), it will stay typed as a string: there is no flags JSON_STRING_AS_BIGINT.

No error is emitted in this process, by default.

See Also

PHP Features

Last updated: 11 October 2026