240 likes | 394 Views
Lecture 6. 13/2/12. 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. Built in Functions.
E N D
Lecture 6 13/2/12
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.
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”
Using Built in Functions <html> <head> <title>abs</title> </head> <body> <?php $num=-321; $newnum=abs($num); print $newnum; ?> </body> </html>
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!
Simple function without arguments function say_hello() { echo “<p>Hello everybody!</p>”; } • To call the function: say_hello();
Function requiring an Argument <?php function printBR($txt) { echo $txt.”<br/>”; } printBR(“Line one!”); printBR(“Line two!”); ?>
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
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
function $test(){ $testvariable = “this is a test variable”; } echo “test variable is “.$testvariable; Will create an error!
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
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.
Question? • What would the following code print to the browser? <?php $num=50; function tenTimes() { global $num; $num=$num*10; } tenTimes(); print $num; ?>
<?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; ?>
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];
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);
strlen($string); • Returns the length of a string Example: <?php $text="hello how are you"; $result= strlen($text); echo $result; ?>
strstr($string1, $string2); <?php $email = 'name@example.com'; $domain = strstr($email, '@'); echo $domain; // prints @example.com ?> • 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; ?>
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
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; ?>
rtrim($string); • Trims from the end of the string only • Example: <?php $text="hello how are you Ciara"; $result= trim($text); echo $result; ?>
ltrim($string); • Trims from the start of the string only • Example: <?php $text="Ciara hello how are you"; $result= trim($text); echo $result; ?>
strtolower($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>"; ?>
str_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; ?>