PHP - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PHP Functions

PHP Functions

shape Introduction

Basically, every C++ program consists of huge code. Compiler cannot handle all the tasks itself at a time. Hence, it divides the program into several parts. One such one is a PHP Functions.

shape Description

Function is a group of statements that are grouped together to perform certain task. Once after defining the function,it has to be called to execute. Function can be called as "mini-program", because a function is written once and can be used as many times as needed in a program. A function can be called with the function name. Function executes only when it is called but cannot be executed automatically during page buffering.

Types of Functions in PHP

shape Description

Functions in PHP are divided into two types.

Pre-defined Functions

Predefined function, which is already built-in functions in PHP. There are about 1000 built-in functions in PHP. These can be retrieved by the programmer directly.

User-defined functions

User defined functions, which is built by the user based on the requirement. The user-defined functions can be accessed by using the following terms:

Function Prototype

shape Description

Code can be divided into separate functions.The division will be in such a way that each function performs a specific task logically.

Function Declaration

shape Description

Like variable declaration, every function in PHP need to be declared before it is used.The declaration should be done before the first call of the function.It gives the information about function name, return type and arguments of function. Function declaration is also called as “function prototype”. To be a prototype, function declaration need to have arguments.Although functions that return int values need not use prototype, but it is recommended.Function Body is not the part of function declaration.It is involved in function definition.

shape Syntax

return_type function_name ( argument_list );
In the above syntax, return_type : returns the data type of the function value. function_name : This is function's actual name. argument_list : The argument list refers to the order,type and number of the parameters of a function. Eg: int add(int a,int b)

Function Definition

shape Description

Function Definition consists of all the code which needs to be executed. It contains Declarator ( enclosed in () ) and Body part ( enclosed in {}).

shape Syntax

return_type function_name(argument list) { //body of function }
In the above syntax, the body of the function has a group of statements that defines the functionality of function does.

Function Call

There are two ways to pass the arguments while calling a function from a program.They are :

Function Without Arguments

shape Example

The information is passed to the functions by using arguments.Here, no arguments are passed to the function Example: [php] <!DOCTYPE html> <html> <body> <?php function withoutArg() { echo "Welcome to SPLessons"; } withoutArg(); // function calls ?> </body> </html> [/php] Output:

Function With Arguments

shape Example

Here arguments are passed into function. [php] <!DOCTYPE html> <html> <body> <?php function withArg($x) { echo "The x value is : $x <br />"; } withArg(30); withArg(40); withArg(50); withArg(60); withArg(70); ?> </body> </html> [/php] Output:

Function Without Return Value

shape Example

The function won't return any value. [php] <!DOCTYPE html> <html> <body> <?php function withArg($x) { echo "The x value is : $x"; return 0; } withArg(30); ?> </body> </html> [/php] Output:

Function With Return Value

shape Example

The function returns a value. [php] <!DOCTYPE html> <html> <body> <?php function withRet($x, $y) { $z = $x + $y; return $z; } echo "30 + 40 = " . withRet(30, 40) . "<br /> "; echo "40 + 50 = " . withRet(40, 50); ?> </body> </html> [/php] Output:

Summary

shape Key Points

  • A re-usability block function may or may-not possess arguments but can be called whenever required.
  • Function can be called by value or by reference.