PHP is one of the most widely-used server-side scripting languages, and it's packed with built-in functions that make development faster and easier. In this post, we’ll explore the top 10 most commonly used PHP functions, with practical examples for each.
1. strlen() – Get String Length
Purpose: Returns the number of characters in a string.
$text = "Hello World!";
echo strlen($text); // Output: 12
2. str_replace() – Replace Text in a String
Purpose: Replaces all occurrences of a search string with a replacement string.
$text = "I love Java";
echo str_replace("Java", "PHP", $text); // Output: I love PHP
3. explode() – Split a String into an Array
Purpose: Breaks a string into an array using a delimiter.
$colors = "red,green,blue";
$array = explode(",", $colors);
print_r($array); // Output: ['red', 'green', 'blue']
4. implode() – Join Array Elements into a String
Purpose: Joins array elements with a string (opposite of explode()).
$array = ['HTML', 'CSS', 'PHP'];
echo implode(" | ", $array); // Output: HTML | CSS | PHP
5. in_array() – Check if a Value Exists in an Array
Purpose: Returns true if a value exists in an array.
$fruits = ["apple", "banana", "mango"];
if (in_array("banana", $fruits)) {
echo "Found!";
} // Output: Found!
6. array_merge() – Merge Multiple Arrays
Purpose: Combines one or more arrays.
$a = [1, 2];
$b = [3, 4];
print_r(array_merge($a, $b)); // Output: [1, 2, 3, 4]
7. isset() – Check if a Variable is Set
Purpose: Checks whether a variable is declared and not null.
$name = "John";
if (isset($name)) {
echo "Variable exists.";
} // Output: Variable exists.
8. empty() – Check if a Variable is Empty
Purpose: Returns true if the variable is empty.
$value = "";
if (empty($value)) {
echo "Value is empty.";
} // Output: Value is empty.
9. date() – Format Date and Time
Purpose: Formats a timestamp into a readable date.
echo date("Y-m-d H:i:s"); // Output: 2025-08-01 14:25:00 (example)
10. json_encode() & json_decode() – Work with JSON
Purpose: Converts data to and from JSON format.
$data = ["name" => "Alice", "age" => 25];
$json = json_encode($data);
echo $json; // Output: {"name":"Alice","age":25}
$decoded = json_decode($json, true);
print_r($decoded); // Output: Array ( [name] => Alice [age] => 25 )