Functions in PHP


PHP functions:
A function is a block of code with name, meant to perform a specific task.
Whenever we want to perform a specific task, we create a function.

Creating a function:
A function is created or defined with the help of function keyword

Syntax: /* function definition */
function functionName( [param1,param2, …. paramN] ) // function header
statement(s) to be executed;

[return returningValue;]

Ex: a function without parameters, without returning value
function wishHi()
echo "Hi", "<br/>";

Note: Functions get execute only when we call them

Syntax: /* function call */
functionName( [ param1, param2 …. paramN ] );

wishHi(); // Hi

Note: Why Functions? Functions are created
- for manageability (i.e. to divide a larger problem into smaller tasks or modules)
- for reusability (i.e. once a function is defined, it can be used multiple times)
- for abstraction (i.e. hiding the way how tasks are done) (creating libraries)

wishHi(); // Hi
wishHi(); // Hi
wishHi(); // Hi

Note: By default a function returns null in PHP
var_dump(wish()); // null

Interview Questions:

1. PHP stands for ______________
a. Hypertext Preprocessor
b. Preprocessor Hypertext
c. Personal Home Processor
d. Personal Hypertext processor
Ans: a