PHP built-in functions
PHP provides a rich set of built-in functions that cover a wide range of functionalities, from string manipulation and array handling to date and time operations and mathematical calculations. These built-in functions are readily available and do not require any additional code to use. Here’s an overview of some common categories of built-in functions in PHP:
1. String Functions
PHP offers numerous functions for manipulating strings. These functions are used to perform operations such as searching, replacing, and formatting strings.
Examples:
strlen()
: Returns the length of a string.echo strlen("Hello, World!"); // Outputs: 13
strtoupper()
: Converts a string to uppercase.echo strtoupper("hello"); // Outputs: HELLO
strpos()
: Finds the position of the first occurrence of a substring in a string.echo strpos("Hello, World!", "World"); // Outputs: 7
substr()
: Returns a part of a string.echo substr("Hello, World!", 7, 5); // Outputs: World
2. Array Functions
Array functions allow you to perform various operations on arrays, such as sorting, filtering, and merging.
Examples:
count()
: Counts all elements in an array.$array = array(1, 2, 3); echo count($array); // Outputs: 3
array_merge()
: Merges one or more arrays into one.$array1 = array("a", "b"); $array2 = array("c", "d"); $result = array_merge($array1, $array2); print_r($result); // Outputs: Array ( [0] => a [1] => b [2] => c [3] => d )
array_search()
: Searches for a value in an array and returns the key if found.$array = array("a" => "apple", "b" => "banana"); echo array_search("banana", $array); // Outputs: b
sort()
: Sorts an array in ascending order.$array = array(3, 1, 2); sort($array); print_r($array); // Outputs: Array ( [0] => 1 [1] => 2 [2] => 3 )
3. Mathematical Functions
Mathematical functions are used to perform various mathematical operations and calculations.
Examples:
abs()
: Returns the absolute value of a number.echo abs(-10); // Outputs: 10
round()
: Rounds a floating-point number to the nearest integer.echo round(3.6); // Outputs: 4
max()
: Finds the highest value in a list of arguments.echo max(1, 5, 3); // Outputs: 5
min()
: Finds the lowest value in a list of arguments.echo min(1, 5, 3); // Outputs: 1
4. Date and Time Functions
These functions allow you to work with date and time values, including formatting and calculations.
Examples:
date()
: Formats a local date and time.echo date("Y-m-d"); // Outputs: 2024-09-03 (current date)
time()
: Returns the current Unix timestamp.echo time(); // Outputs: 1693687432 (current timestamp)
strtotime()
: Parses an English textual datetime description into a Unix timestamp.echo strtotime("next Monday"); // Outputs: timestamp for the next Monday
mktime()
: Get the Unix timestamp for a date.echo mktime(0, 0, 0, 9, 3, 2024); // Outputs: timestamp for 2024-09-03
5. File Handling Functions
File handling functions allow you to perform operations such as reading from and writing to files.
Examples:
fopen()
: Opens a file or URL.$file = fopen("example.txt", "r");
fread()
: Reads a file.$content = fread($file, filesize("example.txt"));
fwrite()
: Writes to a file.fwrite($file, "Hello, World!");
fclose()
: Closes an open file.fclose($file);
6. Utility Functions
Utility functions provide various functionalities such as data type checking and debugging.
Examples:
is_array()
: Checks if a variable is an array.$array = array(1, 2, 3); echo is_array($array); // Outputs: 1 (true)
isset()
: Checks if a variable is set and is notnull
.$var = "Hello"; echo isset($var); // Outputs: 1 (true)
empty()
: Checks if a variable is empty.$var = ""; echo empty($var); // Outputs: 1 (true)
print_r()
: Prints human-readable information about a variable.$array = array("a" => "apple", "b" => "banana"); print_r($array);