# 30 seconds of PHP

## Snippet List

Click on a snippetâ€™s name to view its code or a tag name to view all snippets in that category.

### Array

#### all

Returns `true`

if the provided function returns `true`

for all elements of an array, `false`

otherwise.

Use `array_filter()`

and `count()`

to check if `$func`

returns `true`

for all the elements in `$items`

.

#### any

Returns `true`

if the provided function returns `true`

for at least one element of an array, `false`

otherwise.

Use `array_filter()`

and `count()`

to check if `$func`

returns `true`

for any of the elements in `$items`

.

#### deepFlatten

Deep flattens an array.

Use recursion.
Use `array_merge`

with an empty array to flatten the array.
Recursively flatten each element that is an array.

#### drop

Returns a new array with `$n`

elements removed from the left.

Use `array_slice()`

to remove `$n`

elements from the left.
Omit the second argument, `$n`

, to only remove one element.

#### findLast

Returns the last element for which the provided function returns a truthy value.

Use `array_filter()`

to remove elements for which `$func`

returns falsy values, `array_pop()`

to get the last one.

#### findLastIndex

Returns the index of the last element for which the provided function returns a truthy value.

Use `array_keys()`

and `array_filter()`

to remove elements for which `$func`

returns falsy values, `array_pop()`

to get the last one.

#### flatten

Flattens an array up to the one level depth.

Use `array_merge()`

and `array_values()`

to flatten the array.

#### groupBy

Groups the elements of an array based on the given function.

Use `call_use_func()`

with `$func`

on `$items`

to group them based on `$func`

.

#### hasDuplicates

Checks a flat list for duplicate values, returning `true`

if duplicate values exists and `false`

if values are all unique.

Use `count()`

and `array_unique()`

to check `$items`

for duplicate values.

#### head

Returns the head of a list.

Use `reset()`

to return the first item in the array.

#### last

Returns the last element in an array.

Use `end()`

to return the last item in the array.

#### orderBy

Sorts a collection of arrays or objects by key.

Uses `sort()`

on the provided array to sort the array vased on `$order`

and `$attr`

.

#### pluck

Retrieves all of the values for a given key.

Use `array_map()`

to map each object in the `$items`

array to the provided `$key`

.

#### pull

Mutates the original array to filter out the values specified.

Use `array_values()`

and `array_diff()`

to remove the specified values from `$items`

.

#### reject

Filters the collection using the given callback.

Use `array_values()`

, `array_diff()`

and `array_filter()`

to filter `$items`

based on `$func`

.

#### remove

Removes elements from an array for which the given function returns `false`

.

Use `array_filter()`

to find array elements that return truthy values and `array_diff_keys()`

to remove the elements not contained in `$filtered`

.

#### rotate

Rotates the array (in left direction) by the number of shifts.

Given the `$shift`

index, merge the array values after `$shift`

with the values before `$shift`

.

#### tail

Returns all elements in an array except for the first one.

Use `array_slice()`

and `count()`

to return all the items in the array except for the first one.

#### take

Returns an array with `$n`

elements removed from the beginning.

Use `array_slice()`

to remove `$n`

items from the beginning of the array.

#### without

Filters out the elements of an array, that have one of the specified values.

Use `array_values()`

and `array_diff()`

to remove any values in `$params`

from `$items`

.

### Math

#### approximatelyEqual

Checks if two numbers are approximately equal to each other.

Use `abs()`

to compare the absolute difference of the two values to `$epsilon`

.
Omit the third parameter, `$epsilon`

, to use a default value of `0.001`

.

#### average

Returns the average of two or more numbers.

Use `array_sum()`

for all the values in `$items`

and return the result divided by their `count()`

.

#### clampNumber

Clamps `$num`

within the inclusive range specified by the boundary values `$a`

and `$b`

.

If `$num`

falls within the range, return `$num`

.
Otherwise, return the nearest number in the range, using `min()`

and `max()`

.

#### factorial

Calculates the factorial of a number.

Use recursion.
If `$n`

is less then or equal to `1`

, return `1`

.
Otherwise, return the product of `$n`

and the factorial of `$n -1`

.
Throws an exception if `$n`

is a negative number.

#### fibonacci

Generates an array, containing the Fibonacci sequence, up until the nth term.

Create an empty array, initializing the first two values (`0`

and `1`

).
Loop from 2 through `$n`

and add values into the array, using the sum of the last two values.

#### gcd

Calculates the greatest common divisor between two or more numbers.

Use recursion.
Use `array_reduce()`

with the `gcd`

function to appy to all elements in the `$numbers`

list.
Base case is when `y`

equals `0`

. In this case, return `x`

.
Otherwise, return the gcd of `y`

and the remainder of the division `x/y`

.

#### isEven

Returns `true`

if the given number is even, `false`

otherwise.

Checks whether a number is odd or even using the modulo (`%`

) operator.
Returns `true`

if the number is even, `false`

if the number is odd.

#### isPrime

Checks if the provided integer is a prime number.

Check numbers from `2`

to the square root of the given number.
Return `false`

if any of them divides the given number, else return `true`

, unless the number is less than `2`

.

#### lcm

Returns the least common multiple of two or more numbers.

Use the greatest common divisor (GCD) formula and the fact that `lcm(x,y) = x * y / gcd(x,y)`

to determine the least common multiple.
The GCD formula uses recursion.

#### maxN

Returns the maximum value from the provided array.

Use `array_filter()`

and `max()`

to find the maximum value in an array.

#### median

Returns the median of an array of numbers.

Find the middle of the array, use `sort()`

to sort the values.
Return the number at the midpoint if the array's length is odd, otherwise the average of the two middle numbers.

#### minN

Returns the minimum value from the provided array.

Use `array_filter()`

and `min()`

to find the minimum value in an array.

### Function

#### compose

Return a new function that composes multiple functions into a single callable.

Use `array_reduce()`

to perform right-to-left function composition.

#### curry

Curries a function to take arguments in multiple calls.

If the number of provided arguments (`$args`

) is sufficient, call the passed function, `$function`

.
Otherwise, return a curried function that expects the rest of the arguments.

#### memoize

Returns the memoized (cached) function.

Create an empty cache by instantiating a new array. Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not. Allow access to the cache by setting it as a property on the returned function.

#### once

Call a function only once.

Return a function, which only calls the provided function, `$function`

, if `$called`

is `false`

and sets `$called`

to `true`

.

### String

#### countVowels

Returns number of vowels in the provided string.

Use a regular expression to count the number of vowels (`a`

, `e`

, `i`

, `o`

and `u`

a) in a string.

#### decapitalize

Decapitalizes the first letter of a string.

Decapitalizes the first letter of the string and then adds it with rest of the string.
Omit the `$upperRest`

parameter to keep the rest of the string intact, or set it to `true`

to convert to uppercase.

#### endsWith

Checks if a string is ends with a given substring.

Use `strrpos()`

in combination with `strlen`

to find the position of `$needle`

in `$haystack`

.

#### firstStringBetween

Returns the first string there is between the strings from the parameter `$start`

and `$end`

.

Use `trim()`

and `strstr()`

to find the string contained between `$start`

and `$end`

.

#### isAnagram

Compare two strings and returns `true`

if both strings are anagram, `false`

otherwise.

Use `count_chars()`

to compare `$string1`

and `$string2`

.

#### isContains

Check if a word / substring exists in a given string input.

Using `strpos()`

to find the position of the first occurrence of a substring in a string.

#### isLowerCase

Returns `true`

if the given string is lower case, `false`

otherwise.

Convert the given string to lower case, using `strtolower`

and compare it to the original.

#### isUpperCase

Returns `true`

if the given string is upper case, false otherwise.

Convert the given string to upper case, using `strtoupper`

and compare it to the original.

#### palindrome

Returns `true`

if the given string is a palindrome, `false`

otherwise.

Check if the value of `strrev($string)`

is equal to the passed `$string`

.

#### shorten

Returns a shortened string.

Use `mb_strlen()`

, `mb_substr()`

and `rtrim()`

to shorten a string to a give number of characters.

#### startsWith

Check if a string starts with a given substring.

Use `strpos()`

to find the position of `$needle`

in `$haystack`

.

#### About

A few word about us, our goals and our projects.