如何删除PHP int中的位标志?

8e2ybdfx  于 2023-09-29  发布在  PHP
关注(0)|答案(3)|浏览(101)

假设我有

$flags=JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;

如何从$flags中删除JSON_UNESCAPED_SLASHES?

7fhtutme

7fhtutme1#

这不是“C方法”,它只是应用bitwise operators

$flags=JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;

var_dump($flags & JSON_UNESCAPED_SLASHES); // flag should be set to 1

$flags &= ~JSON_UNESCAPED_SLASHES; // remove it  

var_dump($flags & JSON_UNESCAPED_SLASHES); // flag should be set to 0
kx1ctssn

kx1ctssn2#

使用the C method

$flags &= ~JSON_UNESCAPED_SLASHES;

seems to work.

zsbz8rwp

zsbz8rwp3#

如果你无法直接到达标志,因为它们是在第三方代码中添加的(我的情况):

<?php

function notMyFunction($flags)
{
    $finalFlags = $flags | FLAG_I_DONT_WANT;
    // ...
}

有这样一招:

<?php

notMyFunction(FLAG_I_DONT_WANT - 1);

相关问题