10 PHP functions you must know

10 PHP functions you must knowStefan PöltlBlockedUnblockFollowFollowingApr 17In this post, you will find 10 PHP functions that are really powerful and will enrich your PHP knowledge.

checkdnsrr()With this function you can check the DNS records for an IP or hostname that you can pass to the function as a param.

A really good use case is to check if the domain of a given email address exists.

This helps you to avoid bounce emails trying to create an account on your website or platform.

extract()Extract variables from a given array into the current symbol table, which means you can access the array values as variables in your ongoing program.

A symbol table reflects a scope which basically maps your PHP code variables to the internal Zend Engine values(ZVAL).

One issue with the extract function is that you need to be very careful with this function.

If you apply it with $_GET to automatically inject variables you might inject unsanitized user input leading to malicious software.

It’s recommended to use the EXTR_SKIP constant to not override variables in the current symbol table.

output of the extract usage programThink about your use case carefully and be aware of that function when you’re in a code review.

usort()Sorting arrays with special conditions is possible with usort and most of the times really simple with the spaceship operator introduced with PHP 7.

The upper script sorts an array by comparing two DateTime objects passed to the callback function with the spaceship operator.

The spaceship operator returns 0 if $a and $b are equal, -1 if $a is older than $b and 1 if $a is younger than $b 1.

func_get_args()You can find this function used often in code that generates a hash key to store a value in an in-memory cache(Redis, Memcached).

So you can take the params passed to a data retrieval function with func_get_args and build a cache key to check and update your in-memory cache with data from an external service:getenv()To read environment variables from the system you can use this function.

fetch the value of an environment variableThink about accessing environment variables injected by a docker-compose setup or in a build process within Jenkins.

glob()Finding files described by a pattern in an easy way with the glob function.

Pass a pattern and get an array with the matching files back or an empty array if no files are found.

In case of error the function returns false.

Check out this simple example to find .

c and .

h files if they exist in a PHP extension directory:Find .

c and .

h files in a directoryoutput of findin .

h and .

c filesarray_column()A really common usecase which I faced a lot is to extract one value from a two dimensional array into a flat array.

This can be easily done with the array_column function.

In the following example, we want a flattened array with the age value only:json_encode()Serialization and Deserialization is a must in every programming language.

JSON is a well known format across the coding world to stringify an object or array you need to use the json_encode function.

A great example is to use the postgres or mysql json data type to persist an encoded object/array.

Let’s check this coding example to serialize a POPO(plain old PHP object) to a storable string:Output of a json serialized PHP objectcheckdate()The simplest way to check if a date is valid, pass the day, month and year to the checkdate function:levenshtein()The levenshtein distance computes the difference between two strings.

It counts the number of how much characters needs to be added, replaced or deleted to get from one string to the comparable one:So if a user passes a typo as input to your command, you can try to match the user input and suggest the closest string by utilizing the levenshtein distance.

You can find this usage in the symfony framework to suggest a command name if it couldn’t find it by your input:SummaryThese 10 PHP functions show you the power of PHP and will help you for coding more efficiently and to reach your goals faster.

Keep on coding!.. More details

Leave a Reply