User Define Function :-
- User स्वयं ही खुद के function बनाकर उन्हे define कर सकता है क्योकि कभी-कभी built in function user की आवश्यकताओ के
के अनुसार नहीं होते है अत: user के द्वारा बनाए गए function, user define function कहलाते है
- यह function अपने अंदर एक से ज्यादा statement रखते है तथा ये code के repetition को avoide करते है ।
- ये function page के load होने पर Execute नहीं होते है बल्कि ये function के call होने पर Execute होते है ।
- यदि कोई program function मे divided रहता है तो यह जानना आसान होता है कि error कोनसे function के कारण आ रही है और उसको find out करना आसान होता है ।
- चूंकि function program मे अन्य script से seprate होता है अत: function को हम related php include file को add करके दूसरे program मे भी implement कर सकते है ।
Create a User Defined Function in PHP :-
एक User define function create करने का syntax निम्न है
SYNTAX:-
function functionName( ) {
code to be executed;
}
Example:-

Figure:-1 function example
उपरोक्त code को जब हम browser पर run कराते है तो following Output produce होता है -
Output

Figure:-2 function Output
PHP Functions with Parameters:-
PHP Function के अंदर parameters pass करने का option देती है अर्थात आप function के अंदर parameters pass कर सकते है
ये parameters function के अंदर variable की तरह behave करते है । Functions को Parameters के साथ create करने का syntax निम्न है
SYNTAX:-
function functionName( $Parameter1, $Parameter2) {
code to be executed;
}
उपरोक्त syntax को समझने के लिए हम निम्न example लेते है जिसमे multNuumber नाम का एक function लेते है जिसमे 2 parameters $num1 and $num2 है ।
अब इस function में 2 numbers के multiple process को define करते है । function खत्म होने के बाद function को call किया जाता है और function call होने पर हमे output प्राप्त होता है ।
निम्न Example मे multNumber नाम का एक function create करके उसे call करने का program दिया गया है :-
Example:-

Figure:-1 function with parameters example
उपरोक्त code को जब हम browser पर run कराते है तो following Output produce होता है -
Output

Figure:-2 function Output
PHP Default Argument Value in function:-
यदि हम function के अंदर default argument value pass करते है तो जब भी हम function को बिना कुछ arguments pass किए call करते है तो वह default value
assign कर लेता है । Functions को Default Argument Value के साथ create करने का syntax निम्न है
SYNTAX:-
function functionName( $Parameter= initialize default value) {
code to be executed;
}
उपरोक्त syntax को समझने के लिए हम निम्न example लेते है जिसमे setWidth नाम का एक function लेते है जिसमे $setWidth variable लेकर
उसमे initialize value 50 दी गयी है । जब function खत्म होने के बाद function को agrument pass करके call किया जाता है और function call होने पर हमे हमे agrument के अनुसार output प्राप्त होता है ।
but यदि बिना argument pass किए function को call किया जाता है तो default value output के रूप मे प्राप्त होती है ।
निम्न Example मे setWidth नाम का एक function create करके उसे call करने का program दिया गया है :-
Example:-

Figure:-1 function Default Argument Value example
उपरोक्त code को जब हम browser पर run कराते है तो following Output produce होता है -
Output

Figure:-2 function Default Argument Value Output
PHP Functions Returning Values:-
PHP मे function value को return कर सकता है जिसके लिए value या object के साथ return statement का use किया जाता है ।
retuen statemnet function के execution को stop करता है और returning value को call करने वाले code को send करता है । Functions को Returning Values के साथ create करने का example निम्न है
EXAMPLE:-
उपरोक्त syntax को समझने के लिए हम निम्न example लेते है जिसमे divFunction नाम का एक function लेते है जिसमे 2 parameters $num1 and $num2 है ।
अब इस function में 2 numbers के division process को define करते है । जो function खत्म होने के बाद value return करता है
तथा function को call करने पर हमे output के रूप मे returning value रात प्राप्त होती है ।
निम्न Example मे divFunction नाम का एक function create करके उसे call करने का program दिया गया है :-
Example:-

Figure:-1 function Returning Values example
उपरोक्त code को जब हम browser पर run कराते है तो following Output produce होता है -
Output

Figure:-2 function Returning Values Output
PHP Dynamic Function Calls:-
जब हम function name को as a string किसी variable के अंदर assign करते है तो ये variable exactly function की तरह व्यवहार करते है
तो इसे dyanamic function call कहते है
उपरोक्त syntax को समझने के लिए हम निम्न example लेते है
Example:-

Figure:-1 dynamic function example
उपरोक्त code को जब हम browser पर run कराते है तो following Output produce होता है -
Output

Figure:-2 function Output