PHP Theory
Functions in PHP
A function is a block of statements that performs a particular task.
PHP User Defined function :
It is possible to define your own functions.
Advantages:
- By using functions, we can avoid rewriting same logic or code again and again in the program.
- We can call functions at any number of times and from any place in a program.
- To improve the reusability.
- To reduce the size of the code..
To Create a function:
- Function name must start with a letter or an underscore but not with a integer.
- All functions can start with function keyword.
- It must not contain any spaces at function name.
Syntax |
---|
function function_Name() { //Code to be executed; } |
PHP Function Parameters:
In Function Parameters or arguments If we call the function the data can be passed to functions through arguments..
Syntax |
---|
//Defining function function add($num1, $num2) { //Code to be executed; } // Calling function add(10, 20); |
PHP Function Default Argument
When we call the function 'name' without any arguments it will take default value as an argument.
Syntax |
---|
function name($n = "SkillPundit") { // Code to be executed; } name(); |