120 likes | 224 Vues
Learn to define functions, use variables, pass values, and set default parameters for reusable code in PHP scripts.
E N D
Reusing PHP Code • Often scripts need to perform the same actions in several different locations in the script. • It may even be the case that you use the same code in different scripts. • If you find yourself typing the same ten lines of code over and over (or cutting and pasting it repeatedly). • you can move that code into a separate file and get it from that file whenever you need it. • You can reuse code two ways: by inserting a file containing code into a script or by writing and calling a function.
Defining functions • Applications often perform the same task at different points in the script or in different scripts. • A function is a group of PHP statements that perform a specific task. • You can create a function by putting the code into a function block. The general format is as follows: • function functionname() • { • block of statements; • return; • }
Defining functions…. • The return statement stops the function and returns to the main script. • The return statement at the end of the function is not required, • but it makes the function easier to understand.
Using variables in functions • You can create and use a variable inside your function. • Such a variable is called local to the function A local variable is not available outside of the function. • If you want to use the variable outside the function, you have to make the variable global, rather than local, by using a global statement.
Using variables in functions… • Similarly, if a variable is created outside the function, you can’t use it inside the function unless it is global. • $first_name = “Jess”; • $last_name = “Jones”; • function format_name() • { • global $first_name, $last_name; • $name = $last_name.”, “.$first_name; • echo “$name”; • } • format_name();
Passing values to a function • You pass values to a function by putting the values between the parentheses when you call the function • functionname(value1,value2,...); • The function statement includes variable names for the values it’s expecting, • as follows: • function functionname($varname1,$varname2,...) • { • statements • return; • }
Passing values in the correct order • The function receives the values in the order they are passed. • function functionx($x,$y,$z) • { • do stuff • } • You call the function as follows: • functionx($var1,$var2,$var3); • The function sets $x=$var1, $y=$var2, and $z=$var3.
Passing the right number of values • A function is designed to expect a certain number of values to be passed to it. • If you don’t send enough values, the function sets the missing one(s) to NULL. • If you send too many values, the function ignores the extra values.
Default Value of Parameter • You can set default values to be used when a value isn’t passed. • The defaults are set when you write the function. • function add_2_numbers($num1=1,$num2=1) • { • $total = $num1 + $num2; • return $total; • }