Useful PHP functions for beginners

, , Leave a comment

Do you know how many functions available in PHP? more than 5000.. !! To see them please go to  PHP quick reference page. I can’t show you every one of them, so going to introduce you to seven really handy ones in this quick tip!

PHP trim() Function

The trim() function removes whitespaces and other predefined characters from both sides of a string.

$str = "  A piece of string?  ";
// Displays: string(22) " A piece of string? "
$trimmed = trim($str);
// Displays: string(18) "A piece of string?"

trim() is also multi-purpose in that in addition to the string you can also pass it a set of characters and it will remove any that match from the beginning or end:

$str = "A piece of string?";
$trimmed = trim($str, "A?");
// Displays: string(16) " piece of string"


There are also ltrim() and rtrim() functions in PHP which are similar to the trim() function but only remove whitespace (or other specified characters) from the left or right of the string respectively.

PHP strlen() Function

Very often when working with strings, you’ll want to know how long it is. For example, when dealing with a form, you may have a field where you want to make sure users can’t go over a certain number of characters. To count the number of characters in a string, you can use the strlen() function:

echo strlen("Hello world!");

The output of the code above will be:


PHP implode() Function

implode function takes two arguments, a string which will be the deliminator and an array. Basically, implode will turn an array into a string with values separated by the specified deliminator.

$data = array("Cat", "Cow", "Dog", "Elephant", "Horse");
echo implode(", ", $data);

The output for the above code would be:

Cat, Cow, Dog, Elephant, Horse

PHP explode() Function

explode also takes two arguments, a string which is the deliminator and a string to “explode”. Basically, explode is the opposite to implode and turns a delimited string into an array.

$string = "Cat, Cow, Dog, Elephant, Horse";
$data = explode(", ", $string);

The output for the above code would be:

Array ( [0] => Cat [1] => Cow [2] => Dog [3] => Elephant [4] => Horse )

PHP array_rand() Function

The array_rand() function returns a random key from an array, or it returns an array of random keys if you specify that the function should return more than one key.


The output of the code above could be:


PHP strtolower() Function

The strtolower() function converts a string to lowercase.

echo strtolower("Hello WORLD.");

The output of the code above will be:

hello world.

PHP strtoupper() Function

The strtoupper() function converts a string to uppercase.

echo strtoupper("Hello WORLD!");

The output of the code above will be:


PHP in_array() Function

PHPs in_array function allows you to check to see if a value is already in an array. It takes two parameters, a value you are looking for and the array you are looking in.

$array_data = array(1, 2, 3, 4, 5);
if (in_array(4, $array_data)) {
echo "Its in here…!!!";
else {
echo "Nope, not here...";

The output of the in_array example would be:

Its in here…!!!