Learn why this warning appears in your PHP code and how to solve it.

TheĀ Warning "json_encode() expects parameter 2 to be long, string given" happens usually because you're providing instead of the number of a constant, a string as second argument for json_encode. This happens normally because you're using an inexistent constant as second argument, for example using the JSON_PRETTY_PRINT in PHP 5.3:

<?php

echo json_encode(array(), JSON_PRETTY_PRINT);

This in PHP 5.3 will cause the warning because the constant will be interpreted as a literal string because this constant was introduced since PHP 5.4. The same would happen with many constants that aren't available at the mentioned version of PHP. In this article, we'll show you 2 ways to prevent this warning from appearing.

A. Use constant integer value

The warning as mentioned, happens when the constant that you provide as second argument in the json_encode function doesn't exist, so PHP converts the constant name to a literal string:

<?php

// In PHP 5.3 is like providing:
echo json_encode(array(), "JSON_PRETTY_PRINT");
// which is invalid

So you will need to know the integer value of the constant that you want to use in PHP 5.3:

JSON_HEX_TAG => 1
JSON_HEX_AMP => 2
JSON_HEX_APOS => 4
JSON_HEX_QUOT => 8
JSON_FORCE_OBJECT => 16
JSON_NUMERIC_CHECK => 32
JSON_UNESCAPED_SLASHES => 64
JSON_PRETTY_PRINT => 128
JSON_UNESCAPED_UNICODE => 256

JSON_ERROR_DEPTH => 1
JSON_ERROR_STATE_MISMATCH => 2
JSON_ERROR_CTRL_CHAR => 3

JSON_ERROR_SYNTAX => 4

JSON_ERROR_UTF8 => 5
JSON_OBJECT_AS_ARRAY => 1

JSON_BIGINT_AS_STRING => 2

Then, just provide it as second argument in the json_encode method, for example with JSON_PRETTY_PRINT:

<?php

echo json_encode(array(), 128);

The previous snippet wouldn't show the warning anymore and so on with the other constants that you want to use.

B. Define JSON constants by yourself

If you are in a PHP 5.3.0 environment, where you are unable to access JSON constants, you may want to define all of them somewhere in your project before using them, so the constants will exist and therefore the warning won't appear anymore:

<?php
   // json_encode() options
   define('JSON_HEX_TAG',                1);    // Since PHP 5.3.0
   define('JSON_HEX_AMP',                2);    // Since PHP 5.3.0
   define('JSON_HEX_APOS',               4);    // Since PHP 5.3.0
   define('JSON_HEX_QUOT',               8);    // Since PHP 5.3.0
   define('JSON_FORCE_OBJECT',           16);   // Since PHP 5.3.0
   define('JSON_NUMERIC_CHECK',          32);   // Since PHP 5.3.3
   define('JSON_UNESCAPED_SLASHES',      64);   // Since PHP 5.4.0
   define('JSON_PRETTY_PRINT',           128);  // Since PHP 5.4.0
   define('JSON_UNESCAPED_UNICODE',      256);  // Since PHP 5.4.0

   // json_decode() options
   define('JSON_OBJECT_AS_ARRAY',        1);    // Since PHP 5.4.0
   define('JSON_BIGINT_AS_STRING',       2);    // Since PHP 5.4.0
   define('JSON_PARSE_JAVASCRIPT',       4);    // upgrade.php

   // json_last_error() error codes
   define('JSON_ERROR_NONE',             0);    // Since PHP 5.3.0
   define('JSON_ERROR_DEPTH',            1);    // Since PHP 5.3.0
   define('JSON_ERROR_STATE_MISMATCH',   2);    // Since PHP 5.3.0
   define('JSON_ERROR_CTRL_CHAR',        3);    // Since PHP 5.3.0
   define('JSON_ERROR_SYNTAX',           4);    // Since PHP 5.3.0
   define('JSON_ERROR_UTF8',             5);    // Since PHP 5.3.3
   define('JSON_ERROR_RECURSION',        6);    // Since PHP 5.5.0
   define('JSON_ERROR_INF_OR_NAN',       7);    // Since PHP 5.5.0
   define('JSON_ERROR_UNSUPPORTED_TYPE', 8);    // Since PHP 5.5.0
?>

After defining them, you will be able to use your code as usual:

<?php

echo json_encode(array(), JSON_PRETTY_PRINT);

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors