i

PHP Tutorial

Introduction to Functions

Functions in PHP are like other programming languages that take one or more parameters and perform a specific task. The block of code takes the information in the form of parameters to calculate or execute the set of statements and return a value. 

Functions are really helpful and have a lot of advantages.

  • Functions are reusable components. When we want to use common code in various parts of the php script, we can write the set of statements in a function and call it whenever required.

  • Functions makes error detection easier. When we divide the code into smaller chunks into functions, we can easily identify which function is having error and fix the function with fewer efforts.

  • If you want to change or update some lines of code, we can change the statements in function, and whenever you call the function, the changes will reflect automatically.

There are two types of functions in PHP.

1. Built-in Functions – PHP has a huge collection of over 1000 built-in functions. These functions are coded and stored in the php library, and you can call these functions directly whenever required in the script. For e.g. print_r(), fopen(), var_dump, gettype() etc.

2. User-defined Functions – PHP also allows us to create customized functions as per the dynamic requirements apart from the built-in functions. We can create a block of code as per our project needs and use it by calling whenever required.