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 | |
| function getNumberOfDaysInYear(int $year): int | |
| { | |
| // Creates a Unix timestamp for January 1st of the given year to check if it's a leap year | |
| $isLeapYear = date('L', mktime(0, 0, 0, 1, 1, $year)); | |
| return $isLeapYear ? 366 : 365; | |
| } |
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 | |
| /** | |
| * Deterministic function to extract random items from an array. | |
| * | |
| * The same seed will result in the same items being extracted every time. | |
| */ | |
| function extractRandomItems(array $array, int $count, int $seed): array | |
| { | |
| mt_srand($seed); // Set seed for deterministic randomness |
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 | |
| // PHP has a number of built-in functions that allow you to perform | |
| // linguistic operations on strings with knowledge of phonetics, | |
| // as well as the English language rules and pronunciations. | |
| // Calculate the metaphone key of a string | |
| echo metaphone('test'); // TST | |
| // Calculate the soundex key of a string |
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 | |
| use App\Helpers\TextFormatter; | |
| $array = ['Apple', 'Banana', 'Orange']; | |
| // Awesome! Very readable and concise | |
| array_map([TextFormatter::class, 'capitalize'], $array); | |
| // Verbose, but classic, also adds cognitive load by introducing a new function |
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 | |
| // As Composer writes to stderr, we need to capture the output manually | |
| $command = 'composer global require hyde/cli'; | |
| // read from the stderr stream | |
| $descriptorspec = [ | |
| 2 => ['pipe', 'w'], | |
| ]; |
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 | |
| // Set the user agent as required by the GitHub API | |
| ini_set('user_agent', 'github.com/your-username'); | |
| // Now you can make requests to the GitHub API | |
| $response = file_get_contents('https://api.github.com/zen'); | |
| die($response); // Speak like a human. |
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 | |
| /** @return array{major: int, minor: int, patch: int} */ | |
| function parseSemver(string $semver): array | |
| { | |
| return array_combine(['major', 'minor', 'patch'], | |
| array_map('intval', explode('.', $semver)) | |
| ); | |
| } |
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 | |
| function printSerializedArray(array $data): string | |
| { | |
| $string = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); | |
| // Convert JSON to PHP array syntax | |
| $string = str_replace(['{', '}'], ['[', ']'], $string); | |
| $string = str_replace('": "', "' => '", $string); | |
| $string = str_replace('": [', "' => [", $string); |
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 | |
| /** | |
| * Get all of the given array without the specified array of values. | |
| * | |
| * @param array $array | |
| * @param array|string|int|float $values | |
| * @return array | |
| */ | |
| public static function without($array, $values) |
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 | |
| function weightedElement(array $array, float $factor = 1.5): mixed { | |
| $totalWeight = 0; | |
| $weightedArray = []; | |
| // Reverse the array | |
| $array = array_reverse($array); | |
| // Calculate the total weight of the elements |