Presentation is loading. Please wait.

Presentation is loading. Please wait.

13/2/12 Lecture 6. Functions 2 types: – Built in functions – Custom defined functions Functions minimize the amount of repetition of code. function consists.

Similar presentations


Presentation on theme: "13/2/12 Lecture 6. Functions 2 types: – Built in functions – Custom defined functions Functions minimize the amount of repetition of code. function consists."— Presentation transcript:

1 13/2/12 Lecture 6

2 Functions 2 types: – Built in functions – Custom defined functions Functions minimize the amount of repetition of code. function consists of function name followed by parentheses. Some functions may need values passed to it.

3 Built in Functions Thousands of built in functions. Can be found on w3schools.com or php.net Example built-in function: strtoupper() strtoupper(“php on Mondays”); converts string “php on Mondays” to “PHP ON MONDAYS”

4 Using Built in Functions 4 abs <?php $num=-321; $newnum=abs($num); print $newnum; ?>

5 Custom Defined Functions Syntax: function name_of_function($arg1, $arg2) { //code of the function } Note: You can but are not required to include arguments within the parentheses!

6 Simple function without arguments function say_hello() { echo “ Hello everybody! ”; } To call the function: say_hello();

7 Function requiring an Argument <?php function printBR($txt) { echo $txt.” ”; } printBR(“Line one!”); printBR(“Line two!”); ?>

8 Function to return a value (s) <?php function addNums($first, $second) { $result = $first + $second; return $result; } echo addNums(10, 3); ?> //Sends 10 and 3 as arguments to the addNums function and will print 13

9 Variable Scope Variables declared within functions remain local to those functions (i.e. they will not be accessible outside the function) <?php function $test(){ $testvariable = "this is a test variable"; } echo "test variable is".$testvariable; ?> PHP Parse error: parse error, unexpected T_VARIABLE, expecting T_STRING in c:\Inetpub\wwwroot\MBSEBus\CHeavin\Scripts\PHP\scopefunc.php on line 10

10 function $test(){ $testvariable = “this is a test variable”; } echo “test variable is “.$testvariable; Will create an error!

11 Variable scope continued Variables defined outside functions are inaccessible within functions by default Following will output an error! $testvariable = “this is a test variable”; function $test(){ echo $testvariable; } test(); PHP Parse error: parse error, unexpected T_VARIABLE, expecting T_STRING in c:\Inetpub\wwwroot\MBSEBus\CHeavin\Scripts\PH P\scopefunc2.php on line 12

12 Variable Scope Continued Use global statement to access variable in a function that has been defined outside the said function <?php $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b; ?> The above script will output 3. By declaring $a and $b global within the function, all references to either variable will refer to the global version. There is no limit to the number of global variables that can be manipulated by a function.

13 Question? 13 What would the following code print to the browser? <?php $num=50; function tenTimes() { global $num; $num=$num*10; } tenTimes(); print $num; ?>

14 <?php $a = 10; $b = 20; $c = 30; $d = 40; function Sum() { global $a, $b, $c, $d, $e; $e = ((($a+$b)-$c)/$d); } Sum(); echo $e; ?>

15 String Manipulation with PHP 15 You can think of a string in PHP as an array of characters: $mystring=“this is a text”; print $mystring[0]; print $mystring[3];

16 Some String Functions 16 strlen($string); strstr($string1, $string2); substr($string, startpos, endpos); trim($string); rtrim($string); ltrim($string); strtolower($string); strtoupper($string); str_replace($string1, $string2, start, end);

17 strlen($string); 17 Returns the length of a string Example: <?php $text="hello how are you"; $result= strlen($text); echo $result; ?>

18 strstr($string1, $string2); 18 Prints first occurrence of string Finds string 2 inside string 1 If not found returns false, otherwise returns the portion of string 1 that contains it Example: <?php $text="hello how are you"; $text1="how"; $result= strstr($text, $text1); echo $result; ?> <?php $email = 'name@example.com'; $domain = strstr($email, '@'); echo $domain; // prints @example.com ?>

19 substr($string, startpos, endpos); 19 Returns string either start position to end or the section specified by startpos and endpos Example: <?php $text="hello how are you"; $result= substr($text, 2, 4); echo $result; ?> Output - llo

20 trim($string); 20 Trims away white space including newlines and spaces from beginning and end of string Example: <?php $text=" hello how are you "; $result= trim($text); echo $result; ?>

21 rtrim($string); 21 Trims from the end of the string only Example: <?php $text="hello how are you Ciara"; $result= trim($text); echo $result; ?>

22 ltrim($string); 22 Trims from the start of the string only Example: <?php $text="Ciara hello how are you"; $result= trim($text); echo $result; ?>

23 strtolower($string); strtoupper($string); 23 tolower converts all characters to lower toupper converts all characters to upper Example: <?php $text="Ciara Hello How Are You"; $result= strtoupper($text); $result1=strtolower($text); echo $result." "; echo $result1." "; ?>

24 str_replace($string1, $string2, start, end); 24 Similar to substr but replaces the substring with string 2 at start through to optional end point Returns transformed string Example: <?php $text="hello how are you"; $search="hello"; $replace="HI"; $result= str_replace($search,$replace,$text); echo $result; ?>


Download ppt "13/2/12 Lecture 6. Functions 2 types: – Built in functions – Custom defined functions Functions minimize the amount of repetition of code. function consists."

Similar presentations


Ads by Google