We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
这里只列出部分代码片段,完整的请查阅:https://github.com/30-seconds/30_seconds_of_knowledge/blob/master/src/assets/snippets/php/ 。
function average(...$items) { $count = count($items); return $count === 0 ? 0 : array_sum($items) / $count; } average(1, 2, 3); // 2
function deepFlatten($items) { $result = []; foreach ($items as $item) { if (!is_array($item)) { $result[] = $item; } else { $result = array_merge($result, deepFlatten($item)); } } return $result; } deepFlatten([1, [2], [[3], 4], 5]); // [1, 2, 3, 4, 5]
function endsWith($haystack, $needle) { return strrpos($haystack, $needle) === (strlen($haystack) - strlen($needle)); } endsWith('Hi, this is me', 'me'); // true
function factorial($n) { if ($n <= 1) { return 1; } return $n * factorial($n - 1); } factorial(6); // 720
function fibonacci($n) { $sequence = [0, 1]; for ($i = 2; $i < $n; $i++) { $sequence[$i] = $sequence[$i-1] + $sequence[$i-2]; } return $sequence; } fibonacci(6); // [0, 1, 1, 2, 3, 5]
function gcd(...$numbers) { if (count($numbers) > 2) { return array_reduce($numbers, 'gcd'); } $r = $numbers[0] % $numbers[1]; return $r === 0 ? abs($numbers[1]) : gcd($numbers[1], $r); } gcd(8, 36); // 4 gcd(12, 8, 32); // 4
function head($items) { return reset($items); } head([1, 2, 3]); // 1
function isPrime($number) { $boundary = floor(sqrt($number)); for ($i = 2; $i <= $boundary; $i++) { if ($number % $i === 0) { return false; } } return $number >= 2; } isPrime(3); // true
function last($items) { return end($items); } last([1, 2, 3]); // 3
function lcm(...$numbers) { $ans = $numbers[0]; for ($i = 1, $max = count($numbers); $i < $max; $i++) { $ans = (($numbers[$i] * $ans) / gcd($numbers[$i], $ans)); } return $ans; } lcm(12, 7); // 84 lcm(1, 3, 4, 5); // 60
function palindrome($string) { return strrev($string) === (string) $string; } palindrome('racecar'); // true palindrome(2221222); // true
function startsWith($haystack, $needle) { return strpos($haystack, $needle) === 0; } startsWith('Hi, this is me', 'Hi'); // true
The text was updated successfully, but these errors were encountered:
add issue #5
e655f16
No branches or pull requests
`average` - 求平均数
`deepFlatten` - 混维数组平展成一维数组
`endsWith` - 是否以某个特定词结尾
`factorial` - 阶乘计算
`fibonacci` - 斐波那契数列
`gcd` - 最大公约数计算
`head` - 取得首元素
`isPrime` - 质数判定
`last` - 取得尾元素
`lcm` - 最小公倍数计算
`palindrome` - 回文判断
`startsWith` - 是否以某个特定词开始
The text was updated successfully, but these errors were encountered: