1 / 12

Functions In PHP

Functions In PHP. 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.

shay-cannon
Télécharger la présentation

Functions In PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions In PHP

  2. 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.

  3. Creating Reusable Code (Functions)

  4. 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; • }

  5. 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.

  6. 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.

  7. 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();

  8. 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; • }

  9. 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.

  10. 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.

  11. 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; • }

More Related