3v4l.org

run code in 500+ PHP versions simultaneously
<?php function ct_select( int $returnLeft, string $left, string $right ): string { $length = mb_strlen($left, '8bit'); if (mb_strlen($right, '8bit') !== $length) { throw new Exception('ct_select() expects two strings of equal length'); } // Mask byte $mask = (-$returnLeft) & 0xff; // X $x = (string) ($left ^ $right); // Output = Right XOR (X AND Mask) $output = ''; for ($i = 0; $i < $length; $i++) { $rightCharCode = unpack('C', $right[$i])[1]; $xCharCode = unpack('C', $x[$i])[1]; $output .= pack( 'C', $rightCharCode ^ ($xCharCode & $mask) ); } return $output; } var_dump( ct_select(true, 'apple', 'tiger'), ct_select(false, 'apple', 'tiger'), );
Output for 8.2.31 - 8.2.34, 8.3.0 - 8.3.35, 8.4.1 - 8.4.26, 8.5.0 - 8.5.11
string(5) "apple" string(5) "tiger"

preferences:
29.41 ms | 1136 KiB | 8 Q