10- Functions php-updtae in php
-
Upload
raja-kumar -
Category
Education
-
view
137 -
download
1
description
Transcript of 10- Functions php-updtae in php
![Page 1: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/1.jpg)
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Hidaya Institute of Science &
Technologywww.histpk.org
A Division of Hidaya Trust, Pakistan
![Page 2: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/2.jpg)
Function In Php
By: Muhammad Baqar Qazi.
![Page 3: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/3.jpg)
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
BREAKING YOUR CODE APART
![Page 4: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/4.jpg)
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
calculateArea
createMessage
animateImage
get window heightget window widthcalculate areastore areaDo calculationCheck valueOutput messageGet image positionCalculate new valueSet image positionChange text colorIf position correct output message
![Page 5: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/5.jpg)
© 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
![Page 6: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/6.jpg)
© 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 lattermyFunction();myFunction();myFunction();
If it’s in function, it won’t run unless you call it
FUNCTION IN PHP
![Page 7: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/7.jpg)
© 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
![Page 8: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/8.jpg)
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
myFunction();
function myFunction(){//lots of codemyOtherFunction();
}
function myOtherFunction(){//lots of code
}
WHERE TO DECLARE FUNCTIONS
![Page 9: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/9.jpg)
© 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
![Page 10: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/10.jpg)
© 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
![Page 11: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/11.jpg)
© 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
![Page 12: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/12.jpg)
© 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
![Page 13: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/13.jpg)
© 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
![Page 14: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/14.jpg)
© 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
![Page 15: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/15.jpg)
© 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
![Page 16: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/16.jpg)
© 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
![Page 17: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/17.jpg)
© 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
![Page 18: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/18.jpg)
© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org
Programming in Multiple Files
![Page 19: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/19.jpg)
© 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
![Page 20: 10- Functions php-updtae in php](https://reader033.fdocuments.in/reader033/viewer/2022061218/54b7b2544a7959c9688b45ed/html5/thumbnails/20.jpg)
© 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