Services_JSON::_encode( mixed $var ): mixed
- Source
wp-includes/class-json.php:335
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
- Scaling
- Scales with input
- Instructions
- 10–63
- Plugin surface
- None
- Called by
- 3
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 490.
Nothing here hands control to plugin code.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10–33 | _deprecated_function() |
| always | 20–37 | _deprecated_function(), array_map() |
!$c | 21–33 | _deprecated_function(), ->strlen8() |
!$elements && ::Services_JSON() | 27–43 | _deprecated_function(), ::Services_JSON() |
| always | 28–45 | _deprecated_function(), join() |
| always | 30–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() |
| always | 41–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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 490 | 10–63 | 48 | |
| 8.5 | 490 | 10–63 | 48 | |
| 8.4 | 490 | 10–63 | 48 | |
| 8.3 | 490 | 10–63 | 48 | |
| 8.2 | 490 | 10–63 | 48 | 2 more instructions than PHP 8.1 |
| 8.1 | 488 | 10–63 | 48 | |
| 7.4 | 488 | 10–63 | 48 |
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
- _deprecated_function()Marks a function as deprecated and inform when it has been used.
- bin2hex()
- Services_JSON::strlen8()Calculates length of string in bytes
- Services_JSON::utf82utf16()convert a string from one UTF-8 char to one UTF-16 char
- Services_JSON::isError()
- Services_JSON_Error::__construct()PHP5 constructor.
- Services_JSON::_encode()PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format
Used by · 3
- Services_JSON::_encode()PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format
- Services_JSON::encodeUnsafe()encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!)
- Services_JSON::name_value()array-walking function for use in generating JSON-formatted name-value pairs
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.