Course Banner


PHP Function

Function is a piece of code snippet which performs specific task. The benefit of function is that we don’t have to write same piece of code again and again for same task. Like other programming languages, PHP has also different kind of functions. But, you will see the there is no fundamental difference between PHP functions and other programming languages functions.

Fundamentally function can be divided into two types.

● System Defined functions

The functions which are system defined and cannot be changed. These functions come inbuilt with PHP. You can see here the list of most used PHP system defined functions.

Example :

error_reporting() and mysql_connect() etc are examples of system defined functions.

● User Defined functions

The functions whose name can be defined by users are called user defined functions. Below we are going to see different types of user defined functions in PHP. Function starts with function name and then parenthesis opening and closing .And then curly braces

Note:

function name can start with letters and underscore but cannot start with numbers,

Example:

Function example()

{ // code snippet here }

TIPS : Functions names are not case sensitive

Types of Functions

Types of functions as per return types

● Function with return type

Function which has return type is called function with return type.

Example:

< ?php
  1. functionadd($a,$b){
  2. return$a+$b;
  3. }
  4. echo add(23,45);
  5. ?>


Output :

To show value of return type, you need to call with echo.

● Function without return type

Function which does not have return type is called function without return type.

Example:

< ?php
  1. functionadd($a, $b){
  2.  echo$a+$b;
  3. }
  4. add(23,45);
  5. ?>

Output:

We do not have to write echo to show function value

Types of functions as per parameter

● Function without arguments

Example:

< ?php
  1. functionaddress(){
  2. echo"Address : 7/1D, Marine Lane";
  3.  echo"Email : enquiry@acesoftech.com";
  4.  echo"Phone : 8583959528";
  5. }
  6. ?>
  7. My address is : address();?>


In the above example, whenever we need to show address , we can simply call address() function rather than writing complete address.

● Function with arguments

As you can see the above function does not take arguments from user. The value is fixed and we can use that as static value. But, most of the cases we need a function which can take user input and perform the given task.

This can be achieved by function with arguments.

Example:

< ?php
  1. functionadd($num1, $num2){
  2.     return$num1+$num2;
  3. }
  4.       echo"Total is : ". add(45,34);
  5. ?>


● Function with default value

We have already seen functions with parameters and without parameters. But there are some cases when we can define default value of functions argument.

Example:

< ?php
  1. functionfontName($course = 'JAVA') {
  2.       echo"Course name is : $course<br>";
  3. }
  4. fontName ('PHP');
  5.       echo"
    "
    ;
  6. fontName (); // will use the default value of ‘JAVA’
  7.       echo"
    "
    ;
  8. fontName ('Angular');
  9. ?>