functions in php

20
© 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

Upload: syed-mudasir-shah

Post on 04-Aug-2015

61 views

Category:

Education


2 download

TRANSCRIPT

© 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

Function In Php

By: Muhammad Baqar Qazi.

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org

BREAKING YOUR CODE APART

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© Copyright 2012 Hidaya Trust (Pakistan) ● A Non-Profit Organization ● www.hidayatrust.org / www,histpk.org

Programming in Multiple Files

© 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

© 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