Quick Tip: 7 PHP Functions for Beginners

Learn several super handy PHP functions in minutes

Have you ever taken a look at the list of functions available in PHP? Andrew Burgess just counted 5025 on the PHP quick reference page. Granted, it depends on what extensions you have enabled, but still: that’s one heap of functions! While he can’t show you every one of them, if you’re new to the language, he will introduce you to seven really handy ones in this quick tip! Let's get started.

 

Function 1: array_rand

Let’s start with a simple one. Ever want to get a random item out of an array? You might use rand or mt_rand to get a random number, passing 0 and the last index of the array as the min and max parameters; this will give you a random key that you can use to pull a value from your array. However, there’s a way that’s a little bit quicker: array_rand. Just pass it your array, and it will return the random key. If you want more than one random value from the array, pass a second parameter identifying how many; you’ll get back an array of random keys.

Comments

Be the first to write a comment

You must me logged in to write a comment.