PHP Functions

PHP Functions tutorial explain you How to write Functions in PHP? with simple examples.

PHP Function is a special block of code that we can write and will be executed whenever we need it. Using PHP functions we can write a code which is more readable.

There are two PHP function types: User Defined and Built in. PHP has more than 700 built-in functions in PHP that can be used to write PHP script.

PHP Built-In Functions

Like other programming language, PHP offers some built-in functions for day-to-day use in our coding. Most of these functions are very helpful in achieving programming goals and are well documented.

Create Function in PHP

At the time you are creating a PHP function, follow these simple guidelines:

  • Always begin your function with the keyword function.
  • Remember that your function’s code must be between the “{” and the “}”.
  • At the time you are utilizing your function, make sure you spell the function name correctly.

When we create a function, we first need to give it a name, like my_function.

Example on create function in PHP:

PHP Functions Arguments

We pass arguments to PHP functions in order to process variable data and display, store, or return results.

Example on Passing one argument to function:

The Output will be:

Example on Passing more than one argument to PHP functions:

The Output will be:

Returning Values from PHP functions

Many times we will want our custom PHP function to return a value to your script rather than displaying the data when the function runs.

Using the PHP return statement we can accomplish this easily. The return statement will stop the function from running any further, and send a return value back to the code that called the function to run.

However, PHP function can only return one value, although that value can be any integer, float, array, string, etc. that we choose!
To capture this value we can set a variable equal to the PHP function.

Example on Returning value from function in PHP:

Output will be:

Previous articlePHP For Loops
Next articleHTML Form in PHP
I am a young Information Systems Security Engineer(CEH). I had started with penetration testing when i am 17 years old. It all started with Kali linux and Metasploit.In free time i will play with Security holes/Bug bounty/Pentesting and Hacking !!!