An Interest In:
Web News this Week
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
- April 13, 2024
November 21, 2021 07:46 am GMT
Original Link: https://dev.to/shoaiyb/php-for-fun-2kc9
I'll Be Writing PHP Functions In Here Just For Fun!
STR_SPRINTF
<?phpfunction _sprintf(string $format, mixed ...$values): string { foreach($values as $key => $value) { $format = str_replace('%' . $key, $value, $format); } return $format;}?>
<?phpecho _sprintf('I am %0 and so %1 now!', 'tired', 'hungry'); // returns: I am tired and so hungry now!?>
_SEARCH
<?phpfunction _search(string $search, array $in): array|string { $_search = strtolower($search); foreach($in as $key => $value) { $_value = strtolower($value); if(str_contains($_value, $_search)) { $result[$key] = $value; } } if(isset($result) && is_array($result)) { return $result; } else { return 'Ohh no! No search result have been found!'; }}?>
<?phpprint_r(_search('Old', ['New Post', 'Old Post', 'New Page', 'Old Page']));?>
Original Link: https://dev.to/shoaiyb/php-for-fun-2kc9
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To