• Functions are essential building blocks of any PHP program, hence called Backbone part of Php, as they help to break down complex tasks into simpler & smaller ones.


  • A function is a sub-program/block of codes of a (complete) program that can be used repeatedly by the developer inside the program as per the need.


  • A function will not execute automatically even when a web page loads.
  • A function is executed simply by calling its name inside the program at one or more places as per requirements.
  • The Php function takes input from the user in the form of parameters or arguments to perform certain operations and gives the related output.
  • A Php function supports the facility of reusability of codes and easy code updating &debugging.
  • A typical function name must start with a letter or an underscore and then any numbers or symbols.
  • A function name is usually not case-sensitive.


  • Php has two major categories of functions. These are –
    • Library/Built-in Functions
    • User-defined Functions

Built-in Functions:

  • It is supposed to be the real power of Php comes from its built-in functions.
  • PHP provides a vast collection of built-in functions that perform various tasks, including math or arithmetic calculation, string manipulation, date & time manipulation, array handling, file I/O, database connectivity, and much more. You can also define your custom functions to extend the functionality of PHP and make your code more modular and reusable. It is believed that Php has more than 1000 built-in predefined/core functions today that exist in the system with Php installation.
  • These functions are already coded, tested, executed, and stored in the form of certain functions.
  • To use these functions, we just need to call them as per our requirement and with properly defined syntax.
  • The most common Php built-in functions are: –
    • Math/Arithmetic/Numeric Php Functions
      • PHP provides a wide range of built-in math functions that allow users/developers to perform mathematical/arithmetic operations easily.
      • These functions can be used to perform a wide variety of mathematical calculations in Php, from simple arithmetic operations to more advanced trigonometric functions and logarithmic calculations and many more.
      • Some popular examples of Math functions are – 
        • abs() – Returns the absolute/positive value of a number.

        • sqrt() – Returns the square root value of a number.

        • pow() – Raises a number to a specified power and gives a result.

        • floor() – Rounds a number down to the nearest integer.

        • ceil() – Rounds a number up to the nearest integer.

        • round() – Rounds a number to the nearest integer.

        • rand() – Generates a random integer.

        • max() – Returns the highest value from an array.

        • min() – Returns the lowest value from an array.

        • sin() – Returns the sine value of a number.

        • cos() – Returns the cosine value of a number.

        • tan() – Returns the tangent value of a number.

        • asin() – Returns the inverse sine value of a number.

        • acos() – Returns the inverse cosine value of a number.

        • atan() – Returns the inverse tangent value of a number.

        • deg2rad() – Converts degrees value into radians.

        • rad2deg() – Converts radians value into degrees.

        • log() – Returns the natural logarithm of a number.

        • exp() – Returns the exponential value of a number.

        • pi() – Returns the value of pi.

    • Array Php Functions

For Array, Functions detail use this link

    • String Php Functions

For String, Functions detail use this link

    • File I/O Php Functions
      • Php provides a variety of built-in functions related to File Input/Output that allows users or developers to read from and write to files.
      • Here is a list of some of the most popular used file I/O functions in Php are –
        • fopen() – Opens or creates a new file for reading or writing.

        • fclose() – Closes an open file pointer.

        • fread() – Reads or Prints a specified number of bytes from a file.

        • fwrite() – Writes or stores (Input) a specified number of bytes to a file.

        • fgets() – Reads/Prints a single line from a file.

        • feof() – Checks whether the end of a file has been reached.

        • file() – Reads an entire file into an array.
        • fseek() – Sets the position of the file pointer.

        • rewind() – Sets the file pointer to the beginning of the file.

        • is_file() – Checks whether a given filename exists and is a regular file.

        • is_dir() – Checks whether a given filename exists and is a directory.

        • file_get_contents() – Reads an entire file into a string.

        • file_put_contents() – Writes a string to a file.

        • unlink() – Deletes a file.

        • mkdir() – Creates a new directory.

        • rmdir() – Deletes an existing empty directory.

        • chdir() – Changes the current working directory.

        • rename() – Renames a file.

        • copy() – Copies a file from one location to another.

        • stat() – Returns information about a file.

    • Date & Time Php Functions

Some more examples of detail of the Date & Time function

Here are some common Php date and time functions are:

      • date() – Returns the current date and time of a computer system in a specified format.
      • time() – Returns the current Unix timestamp.
      • date_create() – Creates a new DateTime object.
      • date_diff() – Returns the difference between two DateTime objects.
    • Database Connectivity Php Functions

Here are some common examples of Php database (MySQL) connectivity functions:-

      • mysqli_connect() – Helps in creating a connection of Php with MySQL database.
      • mysqli_query() – Performs a query on a MySQL database.
      • mysqli_fetch_assoc() – Fetches a result row as an associative array from a MySQL database query.
      • mysqli_num_rows() – Returns the number of rows in a MySQL database query result.
      • mysqli_error() – Returns the last error message from a MySQL database operation.
      • mysqli_close() – Closes the connection from a MySQL database.

User-Defined Functions :

  • A user-defined function is a customized/focused function created by the user as per the need of the program.
  • A PHP function is defined using the “function” keyword, followed by the function name, and a pair of parentheses containing any parameters that the function accepts. The code block of the function is enclosed within curly braces ({ }) which may or may not return a value.
  • A user-defined function can be called one/more places in the program by simply writing the name of the function.
  • In Php7, the user-defined function can also return the output using ‘return‘ keyword. To declare a return type in the user-defined function, the datatype is added after a colon (:) symbol of the function. We can specify a different return type (as per our need), then the argument data types.
  • In the Php function, arguments are usually passed by value nature, in which a copy of the value is passed and used in the function. But Php also supports argument is passed as a reference, in which changes to the argument also change the variable that was passed in. To use a passed-by reference argument, the & operator is used.
  • Syntax:

function function_name(){

codes/block of statements
  • Example:

(i) Php function creation and calling:

<!DOCTYPE html>

                   function msg()

   echo “Welcome”;

  msg();          //Calling of user defined function

(ii)   Php return type example:
<?Php declare(strict_types=1);            // strict is compulsory
function addition(float $x, float $y) : float {
return (float)($x + $y);
echo addition(20.20, 25.30);
<?php declare(strict_types=1);            // strict is compulsory
function addition(float $x, float $y) : int{
return (int)($x + $y);
echo addition(20.20, 25.30);


Categories: Php Theory


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.