wppaste
WordPress

Services_JSON::_encode( mixed $var ): mixed

Source
wp-includes/class-json.php:335
PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format

Compatibility

Deprecated in WordPress 5.3.0. Use the PHP native JSON extension instead.

WordPress
core
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$varmixed
any number, boolean, string, array, or object to be encoded.
see argument 1 to Services_JSON() above for array-parsing behavior.
if var is a string, note that encode() always expects it to be in ASCII or UTF-8 format!

Return value

mixed
JSON string representation of input var or an error if a problem occurs

Performance profile

How much work a call to Services_JSON::_encode() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
10–63

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 490.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
3

3 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()one call below Services_JSON::_encode()

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 16 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost Services_JSON::_encode() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always10–33_deprecated_function()
always20–37_deprecated_function(), array_map()
!$c21–33_deprecated_function(), ->strlen8()
!$elements && ::Services_JSON()27–43_deprecated_function(), ::Services_JSON()
always28–45_deprecated_function(), join()
always30–46_deprecated_function(), method_exists(), ->toJSON(), method_exists(), ->_encode()
method_exists()33–49_deprecated_function(), method_exists(), ->toJSON(), method_exists()
method_exists()37–53_deprecated_function(), method_exists(), ->toJSON(), method_exists(), array_map()
$var && is_array($var) && !$elements && ::Services_JSON()40–54_deprecated_function(), array_keys(), range(), ::Services_JSON()
!$properties && ::Services_JSON()40–56_deprecated_function(), get_object_vars(), array_keys(), array_values(), ::Services_JSON()
$var && is_array($var)41–56_deprecated_function(), array_keys(), range(), join()
always41–58_deprecated_function(), get_object_vars(), array_keys(), array_values(), join()
4 further outcomes, up to 63 instructions
!method_exists() && !$properties && ::Services_JSON()45–61_deprecated_function(), method_exists(), get_object_vars(), array_keys(), array_values(), ::Services_JSON()
!method_exists()46–63_deprecated_function(), method_exists(), get_object_vars(), array_keys(), array_values(), join()
$var && is_array($var) && !$properties && ::Services_JSON()47–61_deprecated_function(), array_keys(), range(), array_keys(), array_values(), ::Services_JSON()
$var && is_array($var)48–63_deprecated_function(), array_keys(), range(), array_keys(), array_values(), join()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev49010–6348
8.549010–6348
8.449010–6348
8.349010–6348
8.249010–63482 more instructions than PHP 8.1
8.148810–6348
7.448810–6348

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 7

Used by · 3

Source code

    function _encode($var)     {        _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );         switch (gettype($var)) {            case 'boolean':                return $var ? 'true' : 'false';             case 'NULL':                return 'null';             case 'integer':                return (int) $var;             case 'double':            case 'float':                return  (float) $var;             case 'string':                // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT                $ascii = '';                $strlen_var = $this->strlen8($var);                /*                * Iterate over every character in the string,                * escaping with a slash or encoding to UTF-8 where necessary                */                for ($c = 0; $c < $strlen_var; ++$c) {                     $ord_var_c = ord($var[$c]);                     switch (true) {                        case $ord_var_c == 0x08:                            $ascii .= '\b';                            break;                        case $ord_var_c == 0x09:                            $ascii .= '\t';                            break;                        case $ord_var_c == 0x0A:                            $ascii .= '\n';                            break;                        case $ord_var_c == 0x0C:                            $ascii .= '\f';                            break;                        case $ord_var_c == 0x0D:                            $ascii .= '\r';                            break;                         case $ord_var_c == 0x22:                        case $ord_var_c == 0x2F:                        case $ord_var_c == 0x5C:                            // double quote, slash, slosh                            $ascii .= '\\'.$var[$c];                            break;                         case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):                            // characters U-00000000 - U-0000007F (same as ASCII)                            $ascii .= $var[$c];                            break;                         case (($ord_var_c & 0xE0) == 0xC0):                            // characters U-00000080 - U-000007FF, mask 110XXXXX                            // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8                            if ($c+1 >= $strlen_var) {                                $c += 1;                                $ascii .= '?';                                break;                            }                                                        $char = pack('C*', $ord_var_c, ord($var[$c + 1]));                            $c += 1;                            $utf16 = $this->utf82utf16($char);                            $ascii .= sprintf('\u%04s', bin2hex($utf16));                            break;                         case (($ord_var_c & 0xF0) == 0xE0):                            if ($c+2 >= $strlen_var) {                                $c += 2;                                $ascii .= '?';                                break;

Changelog

Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/class-json.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.