PHP Tutorial

Define PHP

PHP Installation


PHP Comment

PHP Case Sensitivity

PHP Variable, DataType

PHP Echo & Print

PHP Operators

PHP Receiving Input

Decision Making - if...else

PHP Switch Case

PHP Loops

PHP Jumping Statement

PHP Image Gallery

PHP File Upload

PHP Arrays

PHP Date Functions

PHP String Functions

PHP Math Functions

PHP Functions

PHP Variable Scope

PHP Constant Variable

PHP Superglobals

PHP Form Validation

PHP Include Statement

PHP Filter

PHP File Handling

PHP Cookies

PHP Session

PHP Send Emails

PHP Captcha

PHP MySQL Select

PHP MySQL Insert

PHP MySQL Delete

PHP MySQL Update

PHP MySQL Injection

PHP Assignment

Page Stats

Visitor: 348

PHP Functions

A function is a block of statements that perform a task. A function can be call when required instead of writing that code again and again. When the function is call the control passes to that function, execute the function statements and return back from where it is called.
In PHP, Function names are NOT case-sensitive. A function name can start with a letter or underscore but not with a number. Functions are of 2 types:
Pre-Defined Functions
User-Defined Funcitons

PHP User-Defined Funcitons

To create a User-Defined Function, we have to specify with function keyword followed by function_name, function parameters and function statements inside the curly braces.

Example 1: Print Hello World using Function.

PHP Function with Parameters

A function argument is a variable which is used to store the value passed to a function for furthur calculations. You can pass as many as parameters your like.

Example 2: Find sum of 2 numbers using Function.

PHP Functions returning value

A function can return a value to the function call, return value is the result of the function calculation.

Example 3.1: Find sum of 2 numbers using function with return value.

You can return more than one value from a function using return array(values).

Example 3.2: Find Sum, Subtraction, Multiplication, Division of 2 numbers using function with return value.

Passing Arguments by Value and Reference

In PHP, we can pass the value to the function parameter by using: value or reference. Reference is the alias to the variable name. Any changes made to an argument will change the value of the original variable. You can pass an argument by reference by adding an ampersand to the variable name in function definition.

Example 4: Function with argument passing by value and passing by reference.

PHP Default Argument Value

Default Argument are useful to provide default values in the function declaration. A function with default arguments can be called without specifying its arguments value.
Default argument must be declared from right to left.

Example 5: Function with Default Arguments