Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

10- Functions php-updtae in php

204 views

Published on

Published in: Education, Technology, Business
  • Be the first to comment

  • Be the first to like this

10- Functions php-updtae in php

  1. 1. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org Hidaya Institute of Science & Technology www.histpk.org A Division of Hidaya Trust, Pakistan
  2. 2. Function In Php By: Muhammad Baqar Qazi.
  3. 3. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org BREAKING YOUR CODE APART
  4. 4. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org calculateArea createMessage animateImage get window height get window width calculate area store area Do calculation Check value Output message Get image position Calculate new value Set image position Change text color If position correct output message
  5. 5. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org • 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 in two ways: by inserting a file containing code into a script or by writing and calling a function. REUSING PHP CODE
  6. 6. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org echo “This code is inside the function”; // loops, if statements, anything! // … { } function name createMessagehideMenuanimateImagecalcuateScoremyFunction () //sometime latter myFunction(); myFunction(); myFunction(); If it’s in function, it won’t run unless you call it FUNCTION IN PHP
  7. 7. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org FUNCTION NAMES • Much like variable names, but do not start with a dollar sign. • Start with a letter or underscore - consist of letters, numbers, and underscores( _ ). • Avoid built in function names
  8. 8. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org myFunction(); function myFunction(){ //lots of code myOtherFunction(); } function myOtherFunction(){ //lots of code } WHERE TO DECLARE FUNCTIONS
  9. 9. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org • 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 } PASSING VALUES TO A FUNCTION
  10. 10. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org •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 VALUES IN THE CORRECT ORDER
  11. 11. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org •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. PASSING THE RIGHT NUMBER OF VALUES
  12. 12. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org •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) { echo $total = $num1 + $num2; } DEFAULT VALUE OF PARAMETER
  13. 13. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org • Often a function will take its arguments, do some computation and return a value to be used as the value of the function call in the calling expression. The return keyword is used for this. function sum ($value1, $value2) { $result = $value1+ $value2; return $result; } echo sum(); RETURN VALUE
  14. 14. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org • You pass the copy of values to the function • The variable outside the function remains unaffected • function increment_value($y) { $y++; echo $y; } $x = 1; increment_value($x); // prints ‘2’ echo $x; // prints ‘1’ PASSING ARGUMENTS BY VALUE
  15. 15. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org • You can pass a variable by reference to a function so the function can modify the variable. • Sometimes we want a function to change one of its arguments - so we indicate that an argument is "by reference" using ( & ) • function increment_value($y) { $y++; echo $y; } $x = 1; increment_ reference($x); // prints ‘2’ echo $x; // prints ‘2’ PASSING ARGUMENTS BY REFERENCE
  16. 16. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org • 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
  17. 17. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org • 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(); USING VARIABLES IN FUNCTIONS
  18. 18. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org Programming in Multiple Files
  19. 19. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org • When your programs get large enough, you may want to break them into multiple files to allow some common bits to be reused in many different files. MULTIPLE FILES
  20. 20. © Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org • include "header.php"; - Pull the file in here • include_once "header.php"; - Pull the file in here unless it has already been pulled in before • require "header.php"; - Pull in the file here and die if it is missing • require_once "header.php"; - You can guess what this means... • These can look like functions - require_once("header.php"); INCLUDING FILES IN PHP

×