0% found this document useful (0 votes)
28 views

PHP Mod 4

Uploaded by

Drisya Dinesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
28 views

PHP Mod 4

Uploaded by

Drisya Dinesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 43
UNIT 4 PHP FUNCTIONS nas hundreds of base functions and thousands more vi ns Besides Built-in PHP functions, we can create our own fncibae onan isa block of statements that can be used Fepeatedly in a program, A i function will not execute immediately when a page loads. A function spe executed by a call to the function. wil ye defined function inPHP + Auser defined function declaration starts with the word “function. + Afunction name can start with a letter or underscore (not anumber). + Function names are case-insensitive. syntax function functionName() { code to be executed; } Sample "; } Personal(“rahul”,1990); Personal(“sam”,1995); > OUTOUT Rahul born in 1990 Sam born in 1995, PHP Default Argument Value When designing your functions, it is often helpful to be able to assign eas default values for parameters that aren’t Passed - PHP does this for most Tey Of its functions, and it saves you having to pass in parameters most of the |, time if they are usually the same. To define your own default parameters for a function, simply add | Constant value you would like them set to after the variables, like this: Example sing PHP sing Using? output aiPal! ail paisonnt i runctions- Returning Values HP pr ie return statement immediately terminates the execution of itis called from within that function. To let a function retu : ve, use the return statement. 2 al ee ae = function square(snum) { return Snum * $num, } Output 16 Afunction can not return multiple values, but similar results can be obtained by returning an array. Variable Scope The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. We can also introduce local scope inside user-defined function. Any variable used inside function is by default limited to the local function scope. ‘Argument Passing: Passing Arguments By References 7 i in We can pass a variable by reference to 2 function so the function ce Modify the variable. Example speci it the submission type Peclfles the destination an " = yf af ="hidden” name="form_submitted” value="1"/ ; vfs used to check whether the form has been scbnieee hidden OF not, ye pitting action attribute of the form specifies the submissi asses the data. The method attribute specifies the sub; te post method the form data to the server ion URL that mission type. f ilt in PHP super global arr: i i this is the buil gl ay variable that is used quessubmitted via HTTP POST method. ee qhe array variable can be accessed from any script in the program; it jasaglobal scope. this method is ideal when you do not want to display the form post vaesin the URL. good example of using post method is when submitting login details tothe server. Ithas the following syntax. HERE, "$ POST[...]” is the PHP array “Nariable_name’” is the URL variable name. *4P GET method is used to get a is Thisis the built in PHP super global array variable that sSsubmitted via HTTP GET method. + in the proeram® ‘ipt il ha The array variable can be accessed fro™ any scrip bal scope. ft RL. Thismethod displays the form values!” theU . deal for search engine forn It's idea the results. it ha: | orm action="http://localhost/course.php” method="get”>

Course name: