1 / 24

Lecture 6

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.

jela
Télécharger la présentation

Lecture 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 6 13/2/12

  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 <html> <head> <title>abs</title> </head> <body> <?php $num=-321; $newnum=abs($num); print $newnum; ?> </body> </html>

  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 “<p>Hello everybody!</p>”; } • To call the function: say_hello();

  7. Function requiring an Argument <?php function printBR($txt) { echo $txt.”<br/>”; } 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\PHP\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? • 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 • 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 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); • Returns the length of a string Example: <?php $text="hello how are you"; $result= strlen($text); echo $result; ?>

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

  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

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

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

  22. ltrim($string); • 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); • 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>"; ?>

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

More Related