lab4_ php basics ii

Upload: prateek1192

Post on 06-Apr-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Lab4_ PHP Basics II

    1/29

  • 8/3/2019 Lab4_ PHP Basics II

    2/29

  • 8/3/2019 Lab4_ PHP Basics II

    3/29

    ARRAY FUNCTIONSfunction Description Usage

    count( ) Returns the number of key-value pairs in an array

    count (array)

    array_unique( ) Removes duplicates from anarray

    array_unique (array)

    array_combine( ) Takes one array as keys and oneas values and creates a newarray of key value pairs. Botharrays must have equalelements.

    array_combine (array1,array2)

    array_merge( ) Merges key-value pairs of two

    arrays into a single array

    array_merge (array1,

    [array2, array3...])array_chunk( ) Divides an array into small

    subarrays of specified sizearray_chunk (array, size,[preserve_key])

    array_keys( ) Returns an array comprising of keys from input array

    array_keys (array, [value])

  • 8/3/2019 Lab4_ PHP Basics II

    4/29

    ARRAY FUNCTIONS (Contd..)

    array_values( ) Returns an array comprising of values from input array

    array_values (array)

    array_slice( ) Removes specified portion of an array

    array_slice (array, start,length, [preserve])

    array_splice( ) Removes specified portion of an array and replaces it with

    given elements

    array_splice (array, start,[length], [array])

    array_push( ) Uses array as a stack andinserts a given element at last

    array_push (array, value1,[value2...])

    array_pop( ) Uses array as a stack and

    removes last element

    array_pop (array)

    list( ) breaks an array apart intoindividual variables.

    list (var1, [var2...])

  • 8/3/2019 Lab4_ PHP Basics II

    5/29

    ARRAY FUNCTION EXAMPLETry this PHP code

  • 8/3/2019 Lab4_ PHP Basics II

    6/29

    ARRAY FUNCTION EXAMPLE and here is the output of previous code

  • 8/3/2019 Lab4_ PHP Basics II

    7/29

    FUNCTIONS Functions are a way to group common tasks or calculations

    to be re-used easily. Calling a function in PHP:

    $result = sum ($a, 5);print('I am human, I am.');

    Defining a function in PHP:function add_numbers($x, $y) {

    $z = $x + $y;return $z;

    } echo is not a function.

  • 8/3/2019 Lab4_ PHP Basics II

    8/29

    PHP allows you to give parameters default values whendeclaring the function.

    function print__strings ($var1 = "Hello World", $var2 ="I'm Learning PHP"){

    echo($var1);

    echo("\n");echo($var2);

    }

    Function Call : print_strings("hello"); Output :

    hello

    I'm Learning PHP

    FUNCTIONS

  • 8/3/2019 Lab4_ PHP Basics II

    9/29

    Another way to have a dynamic number of parameters is touse PHP's built-in func_num_args , func_get_args , and

    func_get_arg functions.function mean( ){

    $sum = 0;$param_count = func_num_args();for ($i = 0; $i < $param_count; $i++)

    $sum += func_get_arg($i);$mean = $sum / $param_count;echo "Mean: {$mean}";return NULL;

    }

    FUNCTIONS (Contd)

  • 8/3/2019 Lab4_ PHP Basics II

    10/29

    If you want to return multiple variables you need toreturn an array rather than a single variable. For example:

    function maths ($x, $y)

    {$z = $x + $y ;$w = $x - $y ;

    $ret = array ("tot"=>$z, "diff"=>$w);return $ret;

    } When calling this from your script you need to call it intoan array.

    $return = maths (10, 5);$return['tot'] will be the total 15, while $return['diff'] will be thedifference 5 in above case.

    FUNCTIONS (Contd)

  • 8/3/2019 Lab4_ PHP Basics II

    11/29

    Try this PHP code to get familiar with functions.

  • 8/3/2019 Lab4_ PHP Basics II

    12/29

    HTML FORM ELEMENT

    INTERACTION The PHP script running at server, receives the data from the

    form and uses it to perform an action such as updatingdatabase contents, sending database format, user authenticationetc.

    To create a form and point it to a PHP document, the HTMLtag is used and an action is specified as follows:

    All fields in the form are stored in the variables $_GET or$_POST, depending on the method used to submit the form.The difference between the GET and POST methods is

    that GET submits all the values in the URL, while POSTsubmits values transparently through HTTP headers.

  • 8/3/2019 Lab4_ PHP Basics II

    13/29

  • 8/3/2019 Lab4_ PHP Basics II

    14/29

    PHP Code that handles the previousform:

  • 8/3/2019 Lab4_ PHP Basics II

    15/29

    EX: CHECK BOXES & MULTILISTS

  • 8/3/2019 Lab4_ PHP Basics II

    16/29

    PHP Code that handles the previous form:

  • 8/3/2019 Lab4_ PHP Basics II

    17/29

    SESSION It is basically a temporary set of variables whose values

    persist across subsequent web pages.

    Session objects exist until: User closes the browser window Sessions maximum time allotment is exceeded Using PHP function such as session_destroy() is called.

    Every session is assigned a unique session ID, which keepsall the current information together. Session ID can bepassed through the URL or through the use of cookies.

    Session functions: session_start( ) and session_destroy( ) $_SESSION[ ]: PHP super global array variable that

    contains currently registered to a script's session

  • 8/3/2019 Lab4_ PHP Basics II

    18/29

    SESSION EXAMPLEThis PHP scripts displays the number of times a user has accessedthis page

  • 8/3/2019 Lab4_ PHP Basics II

    19/29

    SESSION EXAMPLE and here is the output of the previous script:

  • 8/3/2019 Lab4_ PHP Basics II

    20/29

  • 8/3/2019 Lab4_ PHP Basics II

    21/29

    Save this file asfriends.php

    AUTHENTICATION THROUGH

    SESSION (Contd)

  • 8/3/2019 Lab4_ PHP Basics II

    22/29

    AUTHENTICATION THROUGHSESSION (Contd)

    You will find following output if session is valid:

  • 8/3/2019 Lab4_ PHP Basics II

    23/29

    COOKIES A cookie is a small piece of information given

    to a Web browser by a Web server. The browserstores the information in a text file. Thisinformation is then sent back to the server eachtime the browser requests a page from theserver.

    Cookies can live on a clients computer for aslong as a developer has decided it to remain.

    This means unlike sessions, if you closebrowser, cookies can remain in your computer.

    Continued

  • 8/3/2019 Lab4_ PHP Basics II

    24/29

  • 8/3/2019 Lab4_ PHP Basics II

    25/29

  • 8/3/2019 Lab4_ PHP Basics II

    26/29

    COOKIE Example 1Output : Initially or after 30 seconds

    Output : After first attempt and before 30 seconds elapsed

  • 8/3/2019 Lab4_ PHP Basics II

    27/29

    COOKIE Example 2By setting cookie your username will be remembered andyou need not have to type username again.Save this file as cookie_request.php

  • 8/3/2019 Lab4_ PHP Basics II

    28/29

    COOKIE Example 1Save this file as cast_my_vote.php

  • 8/3/2019 Lab4_ PHP Basics II

    29/29

    COOKIE Example 2

    You will have following output if cookie is found :