Built-in Functions
PHP provides many built-in functions that simplify common programming tasks.
These functions are grouped into categories such as:
- String Functions
- Array Functions
- Math Functions
- Date/Time Functions
String functions
String functions help manipulate and analyze text.
Common functions include:
strlen()– get string lengthstrpos()– find position of a substringstr_replace()– replace textsubstr()– extract part of a string
Example
Section titled “Example”$text = "Hello World";
echo strlen($text);Replace Text
Section titled “Replace Text”echo str_replace("World", "PHP", "Hello World");Array functions
Array functions allow working with collections of values.
Common functions include:
count()array_push()array_merge()array_map()
Example
Section titled “Example”$numbers = [1,2,3];
echo count($numbers);Adding Values
Section titled “Adding Values”array_push($numbers, 4);
print_r($numbers);Math functions Math functions perform mathematical calculations.
Common functions include:
abs()– absolute valueround()– round numberssqrt()– square rootrand()– generate random numbers
Example
Section titled “Example”echo abs(-10);Random Number
Section titled “Random Number”echo rand(1, 100);Date and time functions
Date and time functions work with timestamps and formatted dates.
Common functions include:
date()time()strtotime()
Example
Section titled “Example”echo date("Y-m-d");Current Timestamp
Section titled “Current Timestamp”echo time();