13/2/12 lecture 6. functions 2 types: – built in functions – custom defined functions functions...

24
13/2/12 Lecture 6

Upload: laureen-jodie-walters

Post on 03-Jan-2016

223 views

Category:

Documents


1 download

TRANSCRIPT

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

13/2 /12

Lecture 6

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

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.

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

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”

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

4

Using Built in Functions

<html>

<head><title>abs</title></head>

<body><?php$num=-321;$newnum=abs($num);print $newnum;?></body>

</html>

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

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!

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

Simple function without arguments

function say_hello(){echo “<p>Hello everybody!</p>”;}

To call the function:say_hello();

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

Function requiring an Argument

<?phpfunction printBR($txt){echo $txt.”<br/>”;}

printBR(“Line one!”);printBR(“Line two!”);?>

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

Function to return a value (s)

<?phpfunction 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

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

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

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

function $test(){$testvariable = “this is a test variable”;}

echo “test variable is “.$testvariable;

Will create an error!

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

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\PHP\scopefunc2.php on line 12

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

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.

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

13

Question?

What would the following code print to the browser?

<?php

$num=50;

function tenTimes(){global $num;$num=$num*10;}

tenTimes();print $num;?>

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

<?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;?>

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

15

String Manipulation with PHP

You can think of a string in PHP as an array of characters:

$mystring=“this is a text”;print $mystring[0];print $mystring[3];

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

16

Some String Functions

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

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

17

strlen($string);

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

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

18

strstr($string1, $string2);

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 = '[email protected]';$domain = strstr($email, '@');echo $domain; // prints @example.com?>

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

19

substr($string, startpos, endpos);

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

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

20

trim($string);

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;?>

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

21

rtrim($string);

Trims from the end of the string onlyExample: <?php

$text="hello how are you Ciara";$result= trim($text);

echo $result;?>

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

22

ltrim($string);

Trims from the start of the string onlyExample: <?php

$text="Ciara hello how are you";

$result= trim($text);

echo $result;?>

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

23strtolower($string);strtoupper($string); 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."<br>";echo $result1."<br>";?>

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

24str_replace($string1, $string2, start, end);

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;?>