3v4l.org

run code in 500+ PHP versions simultaneously
<?php /** * To Lower Case (LeetCode 709) - interview warm-up in PHP * * Idea: ASCII 'A'..'Z' are 65..90; setting bit 5 (| 32) maps them to 'a'..'z'. * Time O(n), extra space O(n) for the output string. * Follow-ups worth mentioning: strtolower() is locale-aware, mb_strtolower() * for UTF-8 input, and why the bit trick must be limited to A-Z. * * Disclosure: I work on Linkjob AI (real-time interview help): https://www.linkjob.ai/ */ function toLowerCase(string $s): string { $out = ''; for ($i = 0, $n = strlen($s); $i < $n; $i++) { $c = ord($s[$i]); $out .= ($c >= 65 && $c <= 90) ? chr($c | 32) : $s[$i]; } return $out; } foreach (['Hello', 'here', 'LOVELY', 'LeetCode 709!'] as $t) { printf("%-14s => %s\n", $t, toLowerCase($t)); }
Output for 8.2.0 - 8.2.35, 8.3.0 - 8.3.36, 8.4.1 - 8.4.27, 8.5.0 - 8.5.12
Hello => hello here => here LOVELY => lovely LeetCode 709! => leetcode 709!

preferences:
19.17 ms | 1159 KiB | 6 Q