Library/Built-in Function in PHP

Library/Built-in Function in PHP

Library/Built-in Function in PHP

Here's an overview of the main functionality for each category of functions in PHP:

1.Array Functions:

  • Creation and Modification:

    • array() - Create an array

    • array_push() - Push one or more elements onto the end of an array

    • array_pop() - Pop the element off the end of an array

    • array_merge() - Merge one or more arrays

    • array_filter() - Filters elements of an array using a callback function

    • array_map() - Applies a callback function to each element of an array

  • Access and Search:

    • count() - Count the number of elements in an array

    • in_array() - Checks if a value exists in an array

    • array_key_exists() - Checks if a specific key exists in an array

    • array_search() - Searches for a value in an array and returns the corresponding key

  • Iteration:

    • foreach() - Loop through each key/value pair in an array

    • array_walk() - Apply a user-defined function to each element of an array

2.String Functions:

  • Manipulation:

    • strlen() - Get the length of a string

    • strpos() - Find the position of the first occurrence of a substring in a string

    • str_replace() - Replace all occurrences of the search string with the replacement string

    • substr() - Return part of a string

    • trim() - Strip whitespace (or other characters) from the beginning and end of a string

  • Case and Formatting:

    • strtolower() - Convert a string to lowercase

    • strtoupper() - Convert a string to uppercase

    • ucwords() - Uppercase the first character of each word in a string

3.File System Functions:

  • File Handling:

    • file_get_contents() - Reads the entire content of a file into a string

    • file_put_contents() - Write a string to a file

    • fopen() - Opens a file or URL

    • fclose() - Closes an open file pointer

  • Directory Handling:

    • mkdir() - Makes a directory

    • rmdir() - Removes a directory

    • scandir() - List files and directories inside a specified path

4.Database Functions:

  • MySQLi Functions:

    • mysqli_connect() - Open a new connection to the MySQL server

    • mysqli_query() - Performs a query on the database

    • mysqli_fetch_assoc() - Fetch a result row as an associative array

5.Database Functions (for PDO):

  • PDO Functions:

    • PDO::__construct() - Creates a new PDO instance

    • PDO::prepare() - Prepares a statement for execution and returns a statement object

    • PDOStatement::execute() - Executes a prepared statement

6.Date and Time Functions:

  • date() - Format a local time/date

  • time() - Return the current Unix timestamp

  • strtotime() - Parse about any English textual datetime description into a Unix timestamp

  • date_diff() - Returns the difference between two DateTime objects

7.Mathematical Functions:

  • abs() - Absolute value

  • sqrt() - Square root

  • pow() - Exponential expression

  • rand() - Generate a random integer

  • min() and max() - Find the lowest and highest values in an array

8.Regular Expression Functions:

  • preg_match() - Perform a regular expression match

  • preg_replace() - Perform a regular expression search and replace

  • preg_split() - Split string by a regular expression

9.Network Functions:

  • fsockopen() - Open Internet or Unix domain socket connection

  • file_get_contents() - Reads entire file into a string

  • curl_init() - Initialize a cURL session

10.Miscellaneous Functions:

  • isset() - Determine if a variable is set and is not NULL

  • empty() - Determine whether a variable is empty

  • die() - Equivalent to exit

  • echo() - Output one or more strings

  • var_dump() - Dumps information about a variable

Please note that this is just an overview, and there are many more functions and options available for each category. For detailed information and usage examples, refer to the official PHP documentation at php.net


1.Here's a list of commonly used array functions in PHP:

  1. count(): Returns the number of elements in an array.

     $arr = [1, 2, 3, 4, 5];
     $count = count($arr);
    
  2. array_push(): Adds one or more elements to the end of an array.

     $arr = [1, 2, 3];
     array_push($arr, 4, 5);
    
  3. array_pop(): Removes and returns the last element of an array.

     $arr = [1, 2, 3, 4, 5];
     $lastElement = array_pop($arr);
    
  4. array_merge(): Combines two or more arrays into one array.

     $arr1 = [1, 2, 3];
     $arr2 = [4, 5, 6];
     $mergedArray = array_merge($arr1, $arr2);
    
  5. array_filter(): Filters elements of an array using a callback function.

     $arr = [1, 2, 3, 4, 5];
     $filteredArray = array_filter($arr, function($value) {
         return $value > 2;
     });
    
  6. array_map(): Applies a callback function to each element of an array.

     $arr = [1, 2, 3, 4, 5];
     $squaredArray = array_map(function($value) {
         return $value * $value;
     }, $arr);
    
  7. array_key_exists(): Checks if the given key or index exists in the array.

     $arr = ['name' => 'John', 'age' => 30];
     if (array_key_exists('name', $arr)) {
         // Key 'name' exists in the array
     }
    
  8. array_search(): Searches the array for a given value and returns the corresponding key if successful.

     $arr = ['apple', 'orange', 'banana'];
     $key = array_search('orange', $arr);
    
  9. array_reverse(): Returns an array with elements in the reverse order.

     $arr = [1, 2, 3, 4, 5];
     $reversedArray = array_reverse($arr);
    
  10. array_unique(): Removes duplicate values from an array.

    $arr = [1, 2, 2, 3, 4, 4, 5];
    $uniqueArray = array_unique($arr);
    

These are just a few examples, and there are many more array functions available in PHP. You can refer to the official PHP documentation for a comprehensive list and detailed explanations of each function: PHP Array Functions.


2.Here's a list of commonly used string functions in PHP:

  1. strlen(): Returns the length of a string.

     $str = "Hello, World!";
     $length = strlen($str);
    
  2. strpos(): Finds the position of the first occurrence of a substring in a string.

     $str = "Hello, World!";
     $position = strpos($str, "World");
    
  3. str_replace(): Replaces all occurrences of a search string with a replacement string.

     $str = "Hello, World!";
     $newStr = str_replace("World", "John", $str);
    
  4. strtolower(): Converts a string to lowercase.

     $str = "Hello, World!";
     $lowercaseStr = strtolower($str);
    
  5. strtoupper(): Converts a string to uppercase.

     $str = "Hello, World!";
     $uppercaseStr = strtoupper($str);
    
  6. substr(): Returns a part of a string.

     $str = "Hello, World!";
     $substring = substr($str, 0, 5); // Returns "Hello"
    
  7. trim(): Removes whitespace or other characters from the beginning and end of a string.

     $str = "   Hello, World!   ";
     $trimmedStr = trim($str);
    
  8. explode(): Splits a string by a specified delimiter and returns an array.

     $str = "apple,orange,banana";
     $fruits = explode(",", $str);
    
  9. implode() (or join()): Joins array elements with a string.

     $fruits = ["apple", "orange", "banana"];
     $str = implode(", ", $fruits);
    
  10. strrev(): Reverses a string.

    $str = "Hello, World!";
    $reversedStr = strrev($str);
    
  11. str_pad(): Pads a string to a certain length with another string.

    $str = "Hello";
    $paddedStr = str_pad($str, 10, "*");
    
  12. strcmp(): Compares two strings.

    $str1 = "Hello";
    $str2 = "hello";
    $result = strcmp($str1, $str2);
    

These are just a few examples, and there are many more string functions available in PHP. You can refer to the official PHP documentation for a comprehensive list and detailed explanations of each function: PHP String Functions.


3.Here's a list of commonly used file system functions in PHP:

  1. file_get_contents(): Reads the entire content of a file into a string.

     $content = file_get_contents("example.txt");
    
  2. file_put_contents(): Writes a string to a file.

     $data = "Hello, World!";
     file_put_contents("example.txt", $data);
    
  3. fopen(), fwrite(), fclose(): Functions for opening, writing to, and closing files.

     $file = fopen("example.txt", "w");
     fwrite($file, "Hello, World!");
     fclose($file);
    
  4. unlink(): Deletes a file.

     unlink("example.txt");
    
  5. file_exists(): Checks if a file or directory exists.

     $exists = file_exists("example.txt");
    
  6. is_file(): Checks if the given file is a regular file.

     $isFile = is_file("example.txt");
    
  7. is_dir(): Checks if the given path is a directory.

     $isDir = is_dir("example_directory");
    
  8. mkdir(): Creates a directory.

     mkdir("new_directory");
    
  9. rmdir(): Removes a directory.

     rmdir("empty_directory");
    
  10. copy(): Copies a file.

    copy("source.txt", "destination.txt");
    
  11. rename(): Renames a file or directory.

    rename("old_name.txt", "new_name.txt");
    
  12. file(): Reads an entire file into an array.

    $lines = file("example.txt");
    
  13. glob(): Returns an array of filenames that match a specified pattern.

    $files = glob("*.txt");
    
  14. filesize(): Gets the size of a file.

    $size = filesize("example.txt");
    
  15. filemtime(): Gets the last modified time of a file.

    $lastModified = filemtime("example.txt");
    

These are just a few examples, and there are many more file system functions available in PHP. You can refer to the official PHP documentation for a comprehensive list and detailed explanations of each function: PHP Filesystem Functions.


4.Database functions in PHP

Database functions in PHP can vary depending on the database system you are using. Below are examples of commonly used functions for MySQL databases, using MySQLi:

  1. mysqli_connect(): Opens a new connection to the MySQL server.

     $conn = mysqli_connect("hostname", "username", "password", "database");
    
  2. mysqli_query(): Performs a query on the database.

     $query = "SELECT * FROM users";
     $result = mysqli_query($conn, $query);
    
  3. mysqli_fetch_assoc(): Fetches a result row as an associative array.

     while ($row = mysqli_fetch_assoc($result)) {
         // Process each row
     }
    
  4. mysqli_num_rows(): Gets the number of rows in a result.

     $numRows = mysqli_num_rows($result);
    
  5. mysqli_insert_id(): Returns the auto-generated id used in the last query.

     $lastId = mysqli_insert_id($conn);
    
  6. mysqli_error(): Returns a string description of the last error.

     if (!$result) {
         echo "Error: " . mysqli_error($conn);
     }
    
  7. mysqli_real_escape_string(): Escapes special characters in a string for use in an SQL statement.

     $input = "It's a sample input";
     $escapedInput = mysqli_real_escape_string($conn, $input);
    
  8. mysqli_close(): Closes the previously opened database connection.

     mysqli_close($conn);
    

5.Here are some commonly used functions for database operations using PDO (PHP Data Objects):

  1. PDO::__construct(): Creates a new PDO instance representing a connection to a database.

     $dsn = "mysql:host=hostname;dbname=database";
     $user = "username";
     $password = "password";
     $pdo = new PDO($dsn, $user, $password);
    
  2. PDO::prepare(): Prepares a statement for execution and returns a statement object.

     $query = "SELECT * FROM users WHERE id = :id";
     $stmt = $pdo->prepare($query);
    
  3. PDOStatement::bindParam(): Binds a parameter to the specified variable name.

     $id = 1;
     $stmt->bindParam(':id', $id, PDO::PARAM_INT);
    
  4. PDOStatement::execute(): Executes a prepared statement.

     $stmt->execute();
    
  5. PDOStatement::fetch(): Fetches the next row from a result set.

     $row = $stmt->fetch(PDO::FETCH_ASSOC);
    
  6. PDOStatement::fetchAll(): Returns an array containing all of the result set rows.

     $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
  7. PDO::query(): Executes an SQL statement, returning a result set as a PDOStatement object.

     $result = $pdo->query("SELECT * FROM users");
    
  8. PDO::exec(): Executes an SQL statement and returns the number of affected rows.

     $rowCount = $pdo->exec("DELETE FROM users WHERE id = 1");
    
  9. PDOStatement::rowCount(): Returns the number of rows affected by the last SQL statement.

     $rowCount = $stmt->rowCount();
    
  10. PDO::lastInsertId(): Returns the ID of the last inserted row or sequence value.

    $lastId = $pdo->lastInsertId();
    
  11. PDO::beginTransaction(), PDO::commit(), PDO::rollBack(): Methods for handling transactions.

    try {
        $pdo->beginTransaction();
        // Perform database operations
        $pdo->commit();
    } catch (PDOException $e) {
        $pdo->rollBack();
        echo "Failed: " . $e->getMessage();
    }
    
  12. PDO::setAttribute(): Sets an attribute on the database handle.

    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    

These functions provide a foundation for interacting with databases using PDO in PHP. Remember to handle database connections and queries securely to prevent SQL injection and other security issues. Always use prepared statements or parameterized queries to enhance security.


6.Here are some commonly used date and time functions in PHP:

  1. date(): Formats a local time/date.

     echo date("Y-m-d H:i:s"); // Current date and time
    
  2. time(): Returns the current Unix timestamp.

     $timestamp = time();
    
  3. strtotime(): Parses any English textual datetime description into a Unix timestamp.

     $timestamp = strtotime("next Sunday");
    
  4. mktime(): Returns the Unix timestamp for a date.

     $timestamp = mktime(12, 0, 0, 3, 15, 2023); // March 15, 2023, 12:00 PM
    
  5. date_create() and date_format(): Object-oriented way to work with dates.

     $date = date_create('2023-11-20');
     echo date_format($date, 'Y-m-d');
    
  6. date_diff(): Calculates the difference between two DateTime objects.

     $date1 = date_create('2023-11-20');
     $date2 = date_create('2023-12-25');
     $interval = date_diff($date1, $date2);
     echo $interval->format('%R%a days');
    
  7. strftime() and gmstrftime(): Formats a local or GMT/UTC time and/or date according to locale settings.

     setlocale(LC_TIME, 'en_US');
     echo strftime("%A, %B %d, %Y %H:%M:%S"); // Monday, November 20, 2023 15:30:00
    
  8. date_default_timezone_set(): Sets the default timezone used by all date/time functions.

     date_default_timezone_set('America/New_York');
    
  9. getdate(): Returns an associative array containing information about the date.

     $dateInfo = getdate();
     print_r($dateInfo);
    
  10. checkdate(): Validates a Gregorian date.

    $isValid = checkdate(2, 29, 2020); // Check if February 29, 2020, is a valid date
    
  11. strtotime() with date(): Convert a timestamp to a formatted date.

    $timestamp = strtotime('2023-11-20');
    $formattedDate = date('Y-m-d', $timestamp);
    
  12. microtime(): Returns the current Unix timestamp with microseconds.

    $microtime = microtime(true);
    

These functions allow you to manipulate and format date and time values in various ways. Keep in mind that the actual output may vary based on the server's time zone and locale settings.


7.Here are some commonly used mathematical functions in PHP:

  1. abs(): Returns the absolute value of a number.

     $absoluteValue = abs(-5); // Result: 5
    
  2. ceil(): Rounds a number up to the nearest integer.

     $roundedUp = ceil(4.3); // Result: 5
    
  3. floor(): Rounds a number down to the nearest integer.

     $roundedDown = floor(4.8); // Result: 4
    
  4. round(): Rounds a float to the nearest integer.

     $rounded = round(4.5); // Result: 5
    
  5. max(): Returns the highest value from a list of arguments.

     $maximum = max(2, 7, 1, 9); // Result: 9
    
  6. min(): Returns the lowest value from a list of arguments.

     $minimum = min(2, 7, 1, 9); // Result: 1
    
  7. pow() or **: Raises a number to the power of another.

     $power = pow(2, 3); // Result: 8
    
  8. sqrt(): Returns the square root of a number.

     $squareRoot = sqrt(9); // Result: 3
    
  9. rand(): Generates a random integer.

     $randomNumber = rand(1, 100); // Generates a random number between 1 and 100
    
  10. mt_rand(): Generates a random integer using the Mersenne Twister algorithm.

    $randomNumber = mt_rand(1, 100); // Generates a random number between 1 and 100
    
  11. log(): Returns the natural logarithm of a number.

    $logValue = log(10); // Result: 2.302585092994
    
  12. exp(): Returns e raised to the power of a number.

    $exponential = exp(2); // Result: 7.3890560989307
    
  13. sin(), cos(), tan(): Trigonometric functions (sine, cosine, tangent).

    $sinValue = sin(deg2rad(30)); // Sine of 30 degrees
    
  14. deg2rad(), rad2deg(): Converts degrees to radians and radians to degrees.

    $radians = deg2rad(45); // Converts 45 degrees to radians
    

These are some of the basic mathematical functions in PHP. There are more advanced mathematical functions available, and you can refer to the official PHP documentation for a comprehensive list: PHP Mathematical Functions.


8.Regular expression functions

In PHP, regular expression functions are part of the PCRE (Perl Compatible Regular Expressions) extension. Here are some commonly used regular expression functions:

  1. preg_match(): Performs a regular expression match.

     $pattern = '/[0-9]+/';
     $subject = 'There are 123 apples.';
     preg_match($pattern, $subject, $matches);
    
  2. preg_match_all(): Performs a global regular expression match.

     $pattern = '/[0-9]+/';
     $subject = 'There are 123 apples and 456 oranges.';
     preg_match_all($pattern, $subject, $matches);
    
  3. preg_replace(): Performs a regular expression search and replace.

     $pattern = '/apple/';
     $replacement = 'banana';
     $subject = 'I have an apple.';
     $result = preg_replace($pattern, $replacement, $subject);
    
  4. preg_split(): Splits a string by a regular expression.

     $pattern = '/\s+/';
     $subject = 'This is a sentence.';
     $words = preg_split($pattern, $subject);
    
  5. preg_quote(): Quote regular expression characters.

     $input = 'a*b';
     $pattern = '/^' . preg_quote($input, '/') . '$/';
    
  6. preg_grep(): Return array entries that match the pattern.

     $pattern = '/[0-9]+/';
     $array = ['apple', '123', 'orange'];
     $result = preg_grep($pattern, $array);
    
  7. preg_filter(): Perform a regular expression search and replace using callbacks.

     $pattern = '/[0-9]+/';
     $subject = ['apple', '123', 'orange'];
     $result = preg_filter($pattern, 'X', $subject);
    
  8. preg_match_callback(): Perform a regular expression match and call a callback function.

     $pattern = '/[0-9]+/';
     $subject = 'There are 123 apples.';
     preg_match_callback($pattern, function ($matches) {
         // Callback function
         print_r($matches);
     }, $subject);
    

These functions are used for pattern matching and manipulation with regular expressions in PHP. Regular expressions provide a powerful way to search, match, and manipulate strings based on specific patterns. The patterns are defined using a special syntax, and you can refer to the official PHP documentation for more details: PHP PCRE Functions.


9.Network-related functions

PHP provides various network-related functions for working with URLs, sockets, and other networking tasks. Here are some commonly used network functions:

  1. file_get_contents(): Reads the entire content of a file or URL into a string.

     $content = file_get_contents('https://example.com');
    
  2. fopen(), fread(), fclose(): Functions for opening, reading from, and closing files, including URLs.

     $file = fopen('https://example.com', 'r');
     $content = fread($file, 1024);
     fclose($file);
    
  3. fsockopen(): Opens a network connection or a socket.

     $socket = fsockopen('example.com', 80, $errno, $errstr, 30);
    
  4. gethostbyname(): Returns the IPv4 address of a given Internet host name.

     $ip = gethostbyname('example.com');
    
  5. gethostbyaddr(): Returns the host name of the Internet host specified by the IP address.

     $hostname = gethostbyaddr('93.184.216.34');
    
  6. parse_url(): Parses a URL and returns its components.

     $url = 'https://example.com/path/to/page?query=string';
     $components = parse_url($url);
    
  7. urlencode() and urldecode(): URL-encode and URL-decode a string.

     $encodedString = urlencode('Hello World!');
     $decodedString = urldecode($encodedString);
    
  8. http_build_query(): Generates URL-encoded query string.

     $params = ['name' => 'John', 'age' => 30];
     $queryString = http_build_query($params);
    
  9. curl_init(), curl_setopt(), curl_exec(), curl_close(): Functions for using cURL to make HTTP requests.

     $ch = curl_init('https://example.com/api');
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $response = curl_exec($ch);
     curl_close($ch);
    
  10. get_headers(): Fetches all the headers sent by the server in response to an HTTP request.

    $headers = get_headers('https://example.com');
    
  11. ip2long() and long2ip(): Converts an IP address to a long integer and vice versa.

    $ipLong = ip2long('192.168.0.1');
    $ipString = long2ip($ipLong);
    
  12. filter_var() and filter_var_array(): Filters a variable with a specified filter.

    $email = 'user@example.com';
    $filteredEmail = filter_var($email, FILTER_VALIDATE_EMAIL);
    

These functions are commonly used for network-related tasks in PHP, including working with URLs, sockets, and making HTTP requests. The choice of function depends on the specific task you need to accomplish. Always handle user input and network data securely to prevent security vulnerabilities.


10.Miscellaneous Functions

"Miscellaneous Functions" can encompass a wide range of functions that don't fit neatly into specific categories. Here are some miscellaneous functions in PHP:

  1. isset(): Determine if a variable is set and is not null.

     $variable = 'some value';
     if (isset($variable)) {
         // Variable is set
     }
    
  2. empty(): Determine if a variable is empty.

     $value = '';
     if (empty($value)) {
         // Variable is empty
     }
    
  3. unset(): Unset a variable.

     $variable = 'some value';
     unset($variable);
    
  4. defined(): Checks whether a given constant exists.

     define('MY_CONSTANT', 'some value');
     if (defined('MY_CONSTANT')) {
         // Constant is defined
     }
    
  5. die() and exit(): Output a message and terminate the current script.

     if ($errorCondition) {
         die('Error: Script terminated.');
     }
    
  6. sleep(): Delays the execution of the script for a specified number of seconds.

     sleep(5); // Sleep for 5 seconds
    
  7. usleep(): Delays the execution of the script in microseconds.

     usleep(500000); // Sleep for 0.5 seconds
    
  8. header(): Send a raw HTTP header.

     header('Location: https://example.com');
    
  9. error_reporting(): Set the error reporting level at runtime.

     error_reporting(E_ALL); // Report all errors
    
  10. ini_set(): Sets the value of a configuration option.

    ini_set('display_errors', 1); // Display errors
    
  11. gettype(): Get the type of a variable.

    $variable = 42;
    $type = gettype($variable); // Returns 'integer'
    
  12. is_callable(): Verify that the contents of a variable can be called as a function.

    $callable = 'myFunction';
    if (is_callable($callable)) {
        $callable(); // Call the function
    }
    
  13. get_defined_vars(): Returns an associative array with the names of all variables and their values.

    $var1 = 'value1';
    $var2 = 'value2';
    $allVariables = get_defined_vars();
    
  14. memory_get_usage() and memory_get_peak_usage(): Get the current and peak memory usage.

    $currentMemoryUsage = memory_get_usage();
    $peakMemoryUsage = memory_get_peak_usage();
    
  15. microtime(): Returns the current Unix timestamp with microseconds.

    $microtime = microtime(true);
    

These are just a few examples of miscellaneous functions in PHP. They cover a range of tasks, including variable handling, script termination, sleep/delay, header manipulation, and more. The use of these functions depends on the specific requirements of your application. Always refer to the PHP documentation for the most up-to-date information and additional functions: PHP Manual.


Did you find this article valuable?

Support Saifur's Blog by becoming a sponsor. Any amount is appreciated!