Built in Functions in PHP
N.YUVARAJ
AP / CSE
DCE
What are Built-in Functions?
PHP has over 1,000 built-in functions for common tasks.
They help with operations on strings, arrays, dates, files, databases, etc.
Functions save time and ensure efficiency by eliminating the need to
write repetitive code.
String Functions
Common String Functions:
strlen($string) – Returns the length of the string.
strpos($haystack, $needle) – Finds the position of the first occurrence of a
substring.
str_replace($search, $replace, $subject) – Replaces all occurrences of a search
string with a replacement string.
substr($string, $start, $length) – Returns a part of a string.
Array Functions
Popular Array Functions:
array_push($array, $value) – Adds one or more elements to the end of an
array.
array_pop($array) – Removes the last element from an array.
array_merge($array1, $array2) – Merges two or more arrays.
array_slice($array, $offset, $length) – Extracts a portion of an array.
Math Functions
Essential Math Functions:
abs($number) – Returns the absolute value.
ceil($number) – Rounds a number up to the next highest integer.
floor($number) – Rounds a number down to the nearest integer.
rand($min, $max) – Generates a random integer between two values.
Date and Time Functions
Working with Date and Time:
date($format) – Formats a local date and time.
strtotime($time) – Parses a time string into a Unix timestamp.
time() – Returns the current Unix timestamp.
mktime($hour, $minute, $second, $month, $day, $year) – Returns
the Unix timestamp for a specific date.
File Handling Functions
Managing Files:
fopen($filename, $mode) – Opens a file or URL.
fwrite($handle, $string) – Writes to an open file.
fread($handle, $length) – Reads from an open file.
fclose($handle) – Closes an open file.
Database Functions (MySQL)
Interacting with Databases:
mysqli_connect($host, $user, $password, $database) – Opens a new connection to
a MySQL server.
mysqli_query($connection, $query) – Performs a query against the database.
mysqli_fetch_assoc($result) – Fetches a result row as an associative array.
mysqli_close($connection) – Closes a MySQL connection.
Error Handling Functions
Handling Errors in PHP:
error_reporting($level) – Sets the error reporting level.
trigger_error($message, $type) – Generates a user-level error or warning.
set_error_handler($handler) – Sets a user-defined error handler function.
Miscellaneous Functions
empty($variable) – Checks if a variable is empty.
isset($variable) – Determines if a variable is set and is not null.
die($message) – Outputs a message and terminates the script.
print_r($value) – Prints human-readable information about a variable.
The End