Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save alexandreelise/17117c48ca0ecde294a876c7a0db2df0 to your computer and use it in GitHub Desktop.

Select an option

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
<?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]));
@alexandreelise
Copy link
Author

View more on https://3v4l.org/K8bUc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment