PHP Functions in details

PHP Functions in details


Functions are shortcuts for commonly used chunks of code. They make programming much easier because you don’t have to re-use long code snippets.A function is a particular piece of code, which does some processing, and can be accessed from anywhere within the program.

There are already more than 1000 of built-in library functions created for a different area, but we can create our own functions .

While creating a function its name should start with keyword function and all the PHP code should be put inside { and } characters.

Syntax of function :

      function functionName(parameters)

         {

             // code to be executed;

        }

Build-in Functions:

  • Built-in functions are the functions that are provided by PHP to make programming more convenient.
  • Many built-in functions are present in PHP which can be employed in our code. These functions are very helpful in achieving programming goals.
  • The built-in functions in PHP are very simple and easy to use. They make PHP powerful and useful.
  • PHP is very rich in terms of Built-in functions. Some of the important Built-in functions of PHP are as follows:

PHP Array functions :

  • array() - Create an array
  • array_chunk() - Splits an array into chunks of arrays
  • array_diff() - Compares array values, and returns the differences
  • array_merge() - Merges one or more arrays into one array

PHP  Calender Functions :

  • cal_from_jd() - Returns information about a given calendar.
  • cal_from_jd() - Converts a Julian day count into a date of a specified calendar.
  • JDMonthName() - Returns a month name.
  • JDDayOfWeek() - Returns the day of a week

PHP Date & Time Functions:

  • date_date_set() - Sets the date.
  • date_default_timezone_get() – Returns the default time zone.
  • date_default_timezone_set() - Sets the default time zone.
  • date_format() - Returns date formatted according to given format.
  • date_time_set() - Sets the time.

PHP User-defined functions :

PHP allows us to create our own customised functions called the user-defined functions.User defined functions in PHP enable coders to create custom block of codes for specific events. Function declaration starts with the word function.Function name should start with a letter or underscore and it can never be a number. While declaring function, the best practice is to give a name which suggests the purpose of the function.

Example :

Output : 9