Created
January 31, 2026 21:28
-
-
Save alexandreelise/17117c48ca0ecde294a876c7a0db2df0 to your computer and use it in GitHub Desktop.
One-liner to restrict int to just 0 and 1 from many variations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // TL;DR : $output = (intval($x,2) & 1); | |
| $a = '123'; | |
| $b = '42'; | |
| $c= '033'; | |
| $d ='0x007'; | |
| $e = '010'; | |
| $f = '0'; | |
| $g= '1'; | |
| $h = ''; | |
| $i = ' '; | |
| $j = "\0"; | |
| $k = PHP_INT_MAX; | |
| $l = PHP_INT_MIN; | |
| $m = PHP_FLOAT_MAX; | |
| $n = PHP_FLOAT_MIN; | |
| echo implode(PHP_EOL, array_map(fn($item) => 'input : ' . $item . str_repeat('.', strlen($m) - ($item === "\0" ? 0 : strlen($item))) . ' output: ' . (intval($item,2) & 1), [$a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n])); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
View more on https://3v4l.org/K8bUc