You are currently viewing What is User Defined Functions in PHP
  • Post category:PHP

What is User Defined Functions in PHP

User Defined Functions in PHP

PHP has a rich set of built-in functions such as mathematical, string, data, array functions, etc. It is also possible to define a function as per the specific requirement of the program. Such a function is called a user defined functions in PHP. Class is a collection of objects. The object has properties and behavior. A file is simply a resource for storing information on a computer. PHP provides a convenient way of working with files via its rich collection of built-in functions.

A PHP function is a piece of code that can be reused many times. In PHP, we can define a conditional function, a Function within a Function, and a Recursive function also.

Advantages of PHP functions:

Packaging the code into functions has three important advantages:

  1. It reduces duplication within a program, by allowing to the extraction of commonly used routines into a single component. This has the added benefit of making programs smaller, more efficient, and easier to read (code reusability). This will reduce the length of the program and increase the code readability and documentation.
  2. Debugging and testing a program becomes easier when the program is subdivided into functions, as it becomes easier to trace the source of an error and correct it.
  3. Functions encourage abstract thinking because packaging program code into a function is nothing more or less than understanding how a specific task may be encapsulated into a generic component. In this sense, function encourages the creation of more robust and extensible software designs.

Syntax:

function functionname(arg1, arg2,...argN)
{
// code to be executed
}

Note: There is no $ dollar before functionname. Function names must start with letters and underscore only like other labels in PHP. It can’t start with numbers or special symbols.

READ ALSO  How to Define Variables in PHP

Ex:

<?php
function call()
{
echo "Hello PHP Function";
}

call() ; // calling function
?>

Output:
Hello PHP Function

Leave a Reply