Lecture 6
Download
1 / 24

Lecture 6 - PowerPoint PPT Presentation


  • 130 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Lecture 6' - jela


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Lecture 6

Lecture 6

13/2/12


Functions
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
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
Using Built in Functions

<html>

<head>

<title>abs</title>

</head>

<body>

<?php

$num=-321;

$newnum=abs($num);

print $newnum;

?>

</body>

</html>


Custom defined functions
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
Simple function without arguments

function say_hello()

{

echo “<p>Hello everybody!</p>”;

}

  • To call the function:

    say_hello();


Function requiring an argument
Function requiring an Argument

<?php

function printBR($txt)

{

echo $txt.”<br/>”;

}

printBR(“Line one!”);

printBR(“Line two!”);

?>


Function to return a value s
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
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
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 continued1
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
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
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
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
strlen($string);

  • Returns the length of a string

    Example:

    <?php

    $text="hello how are you";

    $result= strlen($text);

    echo $result;

    ?>


Strstr string1 string2
strstr($string1, $string2);

<?php

$email = '[email protected]';

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

    ?>


ad