Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 21, 2021 07:46 am GMT

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:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To