Oihana PHP

isValidJsonEncodeFlags.php

Table of Contents

Functions

isValidJsonEncodeFlags()  : bool
Checks whether a given integer value is a valid combination of `json_encode()` option flags.

Functions

isValidJsonEncodeFlags()

Checks whether a given integer value is a valid combination of `json_encode()` option flags.

isValidJsonEncodeFlags(int $flags) : bool

The function compares the provided bitmask against the list of officially supported JSON_* constants in the current PHP version. If any unsupported bits are set, the function will return false.

Note: PHP itself does not validate unknown flag bits in json_encode() — they are simply ignored. This helper ensures stricter validation.

Parameters
$flags : int

One or more JSON_* constants combined with bitwise OR (|).

Tags
example
use function oihana\core\json\isValidJsonEncodeFlags;

var_dump( isValidJsonEncodeFlags( 0 ) );                                       // true (no options)
var_dump( isValidJsonEncodeFlags( JSON_PRETTY_PRINT) );                       // true
var_dump( isValidJsonEncodeFlags( JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ) ; // true
var_dump( isValidJsonEncodeFlags( JSON_PRETTY_PRINT | 123456 ) );             // false
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
bool

Returns true if the given flags are a valid combination of json_encode() options, false otherwise.


        
On this page

Search results