web programming using php

Upload: pradeoth-mukundan

Post on 04-Feb-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/21/2019 Web Programming Using Php

    1/62

    WEB PROGRAMMING

    USING PHP

    Rameela Ravindran K

    Department of Computer Science

    St. Thomas College, Thrissur

  • 7/21/2019 Web Programming Using Php

    2/62

    INTRODUCTION TO PHP

    Web development language

    stands for :-Hypertext Preprocessor

    server-side scripting language- can be embedded inHTMLons to love

    Reasons to love PhpCost

    Open source softwarePhp license

    Ease of use (easy to learn and write :- a little bit ofexperience in a C-syntaxed programming language andlittle more involved than HTML

    Html embedness (ordinary HTML pages that

    escape into PHP mode only when necessary)

  • 7/21/2019 Web Programming Using Php

    3/62

    Cross-platform compatibility (Apahe unix, iiswindows, Netscape Enterprise Server)

    Not tag based

    StabilityServer need not be rebooted oftenSoftware doesn't change radically

    Speed (compared to Jsp CGI)

    Many extensions (supports ODBC, SQL, access etc:-supports major protocols also)

    Fast feature development

    team will accept every random contribution into the

    official distribution without community buy-in, butindependent developers can and do distribute theirown extensions which may be

    later folded into the main PHP package in more orless unitary form.

  • 7/21/2019 Web Programming Using Php

    4/62

    Popularity

    Not proprietary (not tied to any one serveroperating)

  • 7/21/2019 Web Programming Using Php

    5/62

    SERVER SIDE WEB SCRIPTING

    On the client, it can mean multimediapresentations, scrolling headlines, pages thatupdate themselves automatically, or elements thatappear and disappear. On the server, the termgenerally denotes content assembledon the fly, at

    the time the page is requested. If you displaythecurrent date and time on a page, for example, thecontent will change from one occasion to anotherand thus will be dynamic.

    Client-Sideanything to do with layout or browser events happens onthe client

    client-side technologies cannot do anything that requiresconnecting to a back end server. JavaScript cannot

    assemble a customized drop-down list on the fly fromuser preferences stored in a database

  • 7/21/2019 Web Programming Using Php

    6/62

    Server-side Web scripting is mostly aboutconnecting Web sites to back end servers, such asdatabases. This enables the following types oftwo-way communication:Server to client: Web pages can be assembled

    from back end-server output

    Client to server: Customer-entered information

    can be acted upon.

    Common examples of client-to-server interactionare online forms with some drop-down lists

  • 7/21/2019 Web Programming Using Php

    7/62

  • 7/21/2019 Web Programming Using Php

    8/62

    handle many of the crucial technical andadministrative details necessary to keep a siterunning, such as:HardwareSoftware upgradesInterNIC registration, IP addressing, DNSMail servers (POP/IMAP and SMTP)Bandwidth

    Power supplyBackupsSecurity

  • 7/21/2019 Web Programming Using Php

    9/62

    INSTALLING PHP

    A server or workstation with enough RAM for OS

    Unix, Mac OS X, or Windows operating system

    Working, dedicated Internet connection

    Install PHP on Windows:Working PHP-supported Web server. Under previousversions of PHP, IIS/PWS was the easiest choicebecause a module version of PHP was available for it;but PHP now has added a much wider selection of

    modules for Windows.correctly installed PHP-supported databasePHP Windows binary distributionutility to unzip files

  • 7/21/2019 Web Programming Using Php

    10/62

    Extract the binary archive using unzip utility; C:\PHP -common location.

    Copy .dll files from PHP directory to systems directory (usually

    C:\Winnt\System32). php5ts.dll , Web server moduleC:\PHP\Sapi\php5isapi.dll., dlls subfolder are the necessary to be copied to the system

    Copy either php.ini-dist or php.ini-recommended toWindowsdirectory (C:\Winnt or C:\Winnt40), and rename it php.ini. Open this file ina text editor. Edit this file to get configuration directives. Set error reportingto E_ALL on their development machines at this point. For now, the mostimportant thing is the doc_root directive under the Paths and Directoriessectionmake sure this matches your IIS Inetpub folder

    Stop and restart the WWW service. Go to the Start menu Settings

    Control Panel Services. Scroll down the list to IIS Admin Service.Select it and click Stop. After it stops, select World Wide Web PublishingService and click Start. Stopping and restarting the service from withinInternet Service Manager.

    5. Open a text editor Type: . Save this file inyourWeb servers document root as info.php. Start any Web browser and browsethe fileyou must always use an HTTP request (http://www.testdomain.com/info.phpor http://localhost/info.php or

    http://127.0.0.1/info.php) rather than a filename(C:\inetpub\wwwroot\info.php) for the file to be parsed correctly. Youshould see a long table of information about your new PHP5 installation.

    http://www.testdomain.com/info.phphttp://www.testdomain.com/info.php
  • 7/21/2019 Web Programming Using Php

    11/62

    BEGINNING WITH PHPCanonical PHP tags

    The most universally effective PHP tag style is

    Short-open (SGML-style) tags

    Short or short-open tags

    do one of two things to enable PHP to recognize thetags:Choose the --enable-short-tags configuration optionwhen youre building PHP.

    Set the short_open_tag setting in your php.ini file toon.

    ASP-style tagsASP-style tags mimic the tags used by Microsoft ActiveServer Pages to delineate code blocks :

  • 7/21/2019 Web Programming Using Php

    12/62

    HTML script tags

  • 7/21/2019 Web Programming Using Php

    13/62

    SYNTAX AND VARIABLES

    PHP syntax is relevant only within PHP

    PHPs Syntax Is C-LikePHP is whitespace insensitive

    Statements are expressions terminated bysemicolons

    $greetings =Welcome to PHP;Expressions are combinations of tokens

    Expressions are evaluated

    Expressions mathematical, boolean, string,

    assignment expressionPrecedence, associatively, and evaluation order

  • 7/21/2019 Web Programming Using Php

    14/62

    COMMENTS

    Portion of a program that exists only for humanreadersMultiline comments: starts with the characterpair /* and terminates with */

    They cannot be nestedSingle line comments: # and // are the commentingto a given line

  • 7/21/2019 Web Programming Using Php

    15/62

    VARIABLES

    denoted with a leading dollar sign ($).

    value of a variable is the value of its most recentassignment.

    need not to be declared before assignment.no intrinsic type other than the type of theircurrent value.

    have default values.

    Variable = value

  • 7/21/2019 Web Programming Using Php

    16/62

    After the initial $, variable names must becomposed of letters (uppercase or lowercase),

    digits (09), and underscore characters (_).first character after the $ may not be a number.

    types are associated with values rather thanvariables

    no declaration is necessaryReassigning variables

    IsSet that tests a variable to see whether it hasbeen assigned a value.

    unset() will restore a variable to an unassignedstate (for example, unset($set_var); will make$set_var into an unbound variable, regardless ofits previous assignments).

  • 7/21/2019 Web Programming Using Php

    17/62

    Variable ScopeGlobalLocal

    Defined ConstantsDefine(variablename, value);

    Pre-defined variables$_GET$_POST

    $_COOKIE$_FILES$_ENV$_SESSION

  • 7/21/2019 Web Programming Using Php

    18/62

    DATA TYPES

    PHP has a total of eight types: .

    Integers are whole numbers, without a decimal point, like495. range -231-1 to 231-1

    Doubles are floating-point numbers, like 3.14159 or 49.0.

    Booleans have only two possible values: TRUE and FALSE.NULL is a special type that only has one value: NULL.

    Strings are sequences of characters, like PHP 4.0 supports

    string operations.

    Arrays are named and indexed collections of other values.

    Objects are instances of programmer-defined classes, which

    can package up both otherkinds of values and functions thatare specific to the class.

    Resources are special variables that hold references to

    resources external to PHP (suchas database connections).

  • 7/21/2019 Web Programming Using Php

    19/62

    Strings can be enclosed in either single or doublequotation marks, with different behavior at readtime.

    Singly quoted strings are treated almost literally,

    doubly quoted strings replace variables with theirvalues as well as specially interpreting certaincharacter sequences.

  • 7/21/2019 Web Programming Using Php

    20/62

    Output:The two most basic constructs for printing to outputare echo and print

    The simplest use of echo is to print a string as

    argument, for example:echo This will print in the users browser window.;

    Or equivalently:echo(This will print in the users browser window.);

    multiple arguments to the unparenthesized version ofecho, separated by commas, as in:

    echo This will print in the , users browserwindow.;

  • 7/21/2019 Web Programming Using Php

    21/62

    Printprint can accept only one argument.print returns a value, which represents whether theprint statement succeeded.

    $animal = antelope;$animal_heads = 1;$animal_legs = 4;print(The $animal has $animal_heads head(s).


    );print(the $animal has $animal_legs leg(s).
    );

  • 7/21/2019 Web Programming Using Php

    22/62

  • 7/21/2019 Web Programming Using Php

    23/62

  • 7/21/2019 Web Programming Using Php

    24/62

  • 7/21/2019 Web Programming Using Php

    25/62

    Break

    Continue

    Alternate control syntaxes

    alternate to start and end if switch for whileconstruct

    If(expression) if(expression):

    { statement1

    Statement1 statement2

    statement2} endif;

  • 7/21/2019 Web Programming Using Php

    26/62

    FUNCTION

    Function function_name($argument_1,$argument_1)

    {

    }

    Call by valueCall by referenceDefault argumentReturn by value

    Function &function_name($argument_1,$argument_1){

    Return $variable;

    }$variable= &function_name;

  • 7/21/2019 Web Programming Using Php

    27/62

  • 7/21/2019 Web Programming Using Php

    28/62

    PASSING INFORMATION BETWEENPAGES

    Passing variables between pages using URL

    Passing variables between pages using Cookies

    Passing variables between pages using sessions

    PHP will catch the variable tossed from one pageto the next and make it available for further use.PHP happens to be unusually good at this type of

    data-passing function, which makes it fast andeasy to employ for a wide variety of Web sitetasks.

  • 7/21/2019 Web Programming Using Php

    29/62

    GET ARGUMENTS

    The GET method passes arguments from onepage to the next as part of theUniform Resource

    Indicator querystring.

    When used for form handling, GET appends the

    indicated variable name(s) and value(s) to theURL designated in the ACTION attribute with aquestion mark separator and submits the wholething to the processing agent- a Web server

  • 7/21/2019 Web Programming Using Php

    30/62

    A GET method example, part 1

    Root, root, root for the:

    Chicago Cubs (NationalLeague)

    Chicago White Sox (AmericanLeague)

  • 7/21/2019 Web Programming Using Php

    31/62

    When the user makes a selection and clicks the Submit button,the browser agglutinates these elements in this order, with nospaces between the elements:

    The URL in quotes after the word ACTION (http://localhost/baseball.php)

    A question mark (?) denoting that the following charactersconstitute a GET string.

    A variable NAME, an equal sign, and the matching VALUE(Team=Cubbies)

    An ampersand (&) and the next NAME-VALUE pair(Submit=Select); further name-value pairs separated byampersands can be added as many times as the server

    querystring-length limit allows. The browser thus constructs the URL string:http://localhost/baseball.php?

    Team=Cubbies&Submit=Select

    Itth f d thiURLitit dd

    http://localhost/baseball.phphttp://localhost/baseball.php
  • 7/21/2019 Web Programming Using Php

    32/62

    It then forwards this URL into its own address space as a newrequest. The PHP script to which the preceding form is submitted(baseball.php) will grab the GET variables from the end of therequest string, stuff them into the $_GET superglobal array.

    The following code sample shows the PHP form handler for thepreceding HTML form:

    A GET method example, part 2

    Go,!

  • 7/21/2019 Web Programming Using Php

    33/62

    Advantages of GET over POSTIt constructs an actual new and differentiable URL

    query string. Users can now bookmark this page. Theresult of forms using the POST method is notbookmarkable.

    Disadvantages:

    The GET method is not suitable for logins because

    the username and password are fully visibleonscreen as well as potentially stored in the clientbrowsers memory as a visited page.

    Every GET submission is recorded in the Web server

    log, data set included.Because the GET method assigns data to a serverenvironment variable, the length of the URL islimited. (Maximum 300-word chunk of HTML-formatted prose using GET method)

  • 7/21/2019 Web Programming Using Php

    34/62

    HTTP POST METHOD

  • 7/21/2019 Web Programming Using Php

    35/62

    HTTP COOKIES

  • 7/21/2019 Web Programming Using Php

    36/62

    HTTP SESSION

  • 7/21/2019 Web Programming Using Php

    37/62

    ARRAY

    Create arrayBy assigning a value into one

    $my_array= the first thing;

    By using array()

    $my_array=array();$my_array= array(apple, orange, pear);$my_array[0]= apple;$my_array[1]=pear;$my_array= array(1,1,52,73);

    By calling a function that happens to return an arrayas its value

    $my_array= range(1,5);$my_array=fun(4);

  • 7/21/2019 Web Programming Using Php

    38/62

    RETRIEVING VALUE

    IndexEcho $my_array[1];

    List()$my_array= array(apple, orange, pear);list($f1,$f2,$f3)=$my_array;

  • 7/21/2019 Web Programming Using Php

    39/62

  • 7/21/2019 Web Programming Using Php

    40/62

    Deleting from arrayUse unset()

    IterationHidden pointer system built into arrayEach index value points to the nextForeach loop

    Foreach($a as $v)

    traversing using list() and each()

    While(list($key,$val)=each($array)

  • 7/21/2019 Web Programming Using Php

    41/62

  • 7/21/2019 Web Programming Using Php

    42/62

    Array functionsIs_array($myarray)Count($myarray) // nonempty elementsSizeof()

    In_array($myarray, a)Isset($myarray[$key] // key is valid or not

  • 7/21/2019 Web Programming Using Php

    43/62

    ARRAY OPERATIONS

    Union (+)

    Equality(==) // same key value pair

    Identity (===)// same key value pair in the sameorder and of same type

    Inequality (!= )

    Non identity (!==)

  • 7/21/2019 Web Programming Using Php

    44/62

    ARRAY FUNCTION

    Is_array()

    Array_fill($startindex,$num,$value)

    Array_key_exists($key, $array)

    Array_sum($myarray)Array_count_values // array using the values ofthe input as key and their frequency as values

    Array_unique()

  • 7/21/2019 Web Programming Using Php

    45/62

    Sort(&$my_array[,$sort_flag=______)Sort_regularSort_numeric

    sort_string

    Asort(&$my_array[,$sort_flag= -------------)

    Rsort((&$my_array[,$sort_flag= -------------)

    arsort (&$my_array[,$sort_flag= -------------)

    ksort (&$my_array[,$sort_flag= -------------) //sortby key

    krsort (&$my_array[,$sort_flag= -------------)

    natsort (&$my_array[,$sort_flag= -------------)natcasesort (&$my_array[,$sort_flag= -------------)

  • 7/21/2019 Web Programming Using Php

    46/62

    In_array($value,$array)

    Each($array) // return each key value pair

    List()

    Current($array) // return current elementNext()

    Pre()

    End() // sets the pointer to the last element

    Reset()count()

  • 7/21/2019 Web Programming Using Php

    47/62

    STRINGS IN PHP

    Sequence of characters

    Accessed using index

    concatenation using .

    Heredoc syntax

  • 7/21/2019 Web Programming Using Php

    48/62

    STRING FUNCTIONS

    Strlen($string)

    Strops()strpos($mystring,$findme[,offset=0])Mystring string to be searched inFindme item to be searched

    Offset which character to start searching$pos= strpos($mystring,$findme)

    Strrpos()Postion of the last occurrence of the substring

    Stripos()Strripos()

    str

  • 7/21/2019 Web Programming Using Php

    49/62

    DATABASE

    Collection of data with regular and predictablestructure

    Examples :postgreSQLMySQLSQL ServerOracle

  • 7/21/2019 Web Programming Using Php

    50/62

    MYSQL

    Freeware

    Stable no crashing

    Lightweight require no

  • 7/21/2019 Web Programming Using Php

    51/62

  • 7/21/2019 Web Programming Using Php

    52/62

  • 7/21/2019 Web Programming Using Php

    53/62

  • 7/21/2019 Web Programming Using Php

    54/62

  • 7/21/2019 Web Programming Using Php

    55/62

  • 7/21/2019 Web Programming Using Php

    56/62

  • 7/21/2019 Web Programming Using Php

    57/62

  • 7/21/2019 Web Programming Using Php

    58/62

  • 7/21/2019 Web Programming Using Php

    59/62

  • 7/21/2019 Web Programming Using Php

    60/62

  • 7/21/2019 Web Programming Using Php

    61/62

  • 7/21/2019 Web Programming Using Php

    62/62