CHAPTER 5 SERVER SIDE SCRIPTING - SIDE SCRIPTING CLIENT SIDE SCRIPTING PLATFORM Web server Web browser

  • View
    223

  • Download
    2

Embed Size (px)

Text of CHAPTER 5 SERVER SIDE SCRIPTING - SIDE SCRIPTING CLIENT SIDE SCRIPTING PLATFORM Web server Web...

  • http://fsktm2.uthm.edu.my/azizulzamri

    CHAPTER 5

    SERVER SIDE SCRIPTING

    PART 1 OF 3

    Azizulzamri bin Muhamed Amin

    [Principles | Operation & Expression | Output]

  • SERVER SIDE SCRIPTING

    The scripts can be written in any of a number of server-side scripting

    languages available (Example : PHP, ASP.NET).

    Server-side scripting differs from client-side scripting which are run client-

    side in the web browser.

    Server side code is used to retrieve and generate content for the dynamic

    pages (Example : to retrieve the content from database).

  • SERVER SIDE SCRIPTING

    1) The user requests a web page from the web server.

    2) The web server executes the code (no PHP code ever reaches the user) in

    the web page and generates an HTML content for that page.

    Note :

    the code is called "server side code/script" because it is executed by the

    web server.

    the page containing the code is called dynamic page

    3) Information it outputs (the html content) is sent back to the browser so that

    it can be displayed to the user.

  • SERVER SIDE SCRIPTING

    Note:

    By the time the data get's to the user's browser, there is no PHP code left, only

    the HTML code remains.

    That's why if attempt to run your PHP documents on a computer with no web

    server will only display the code instead of its output.

  • User requests a web page from the web server

    Web server executes the code and generate

    HTML content

    Output sent back to the

    client browser

    Ex : Hello welcome to my

    shop.com

    HOW SERVER SIDE SCRIPTS WORK?

    Ex :

    http://www. myshop.com

    Ex :

  • Differences of Scripting Lang.

    SERVER SIDE SCRIPTING CLIENT SIDE SCRIPTING

    PLATFORM Web server Web browser

    EXECUTION Before page load After page load

    SOURCE CODE Unseen Visible

    NEED FOR SERVER Yes No

  • BASIC SYNTAX

    PHP stands for Hypertext Preprocessor and is a server-side language.

    The script is run on your web server, not on the user's browser

    PHP is relatively new (compared to languages such as Java) but is quickly

    becoming one of the most popular scripting languages on the internet.

    PHP scripts are always enclosed in between two PHP tags.

    This tags tells the web server to parse the information between the tags as

    PHP.

  • BASIC SYNTAX

    Following are different style for writing PHP script:

    1) Everything between the is read as PHP code.

    2) The statement can also be phrased as simply

  • BASIC SYNTAX

    The three different forms are as follows:

    Style 1

    Style 2

    Style 3

    PHP Code In Here

  • BASIC SYNTAX

    There are two ways to use HTML on PHP page.

    1) The first way is to put the HTML outside of PHP tags.

    are put it in the middle HTML.

    2) The second way to use HTML with PHP is by using PRINT or ECHO.

    This method you include the HTML inside of the PHP tags.

    Each code line in PHP must end with a semicolon. The semicolon is a

    separator and is used to distinguish one set of instructions from another.

  • BASIC SYNTAX Style 1

    Here is some more HTML

    Example :

  • BASIC SYNTAX Style 2

    Example :

  • echo / echo()

    The echo() function outputs one or more strings.

    Note: The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.

    Tip: The echo() function is slightly faster than print().

    Tip: The echo() function also has a shortcut syntax. Prior to PHP 5.4.0, this syntax only works with the short_open_tag configuration setting enabled.

    http://www.w3schools.com/php/func_string_print.asp

  • Example

    In PHP there are two basic ways to get output: echo and print.

    echo(strings)

    The differences are small: echo has no return value while print has a return value of 1 so it can

    be used in expressions. echo can take multiple parameters (although such usage is rare) while

    print can take one argument. echo is marginally faster than print.

    The echo statement can be used with or without parentheses: echo or echo().

    Parameter Description

    strings Required. One or more strings to

    be sent to the output

  • Print / print()

  • Index , form , print data

    1. Index.php

    My first PHP page

    2. form

    Your name:

    Your age:

    3. Print data from form

    Hi .

    You are years old.

    .

  • BASIC SYNTAX - COMMENT

    A comment is the portion of a program that exists only for the human

    reader and stripped out before displaying the programs result.

    All text that appears between the start of the comment and the end will be

    ignored.

    There are two commenting formats in PHP:

    1) Single-line comments:

    generally used for single line comments (short explanations of code).

    2) Multi-lines comments:

    generally used to provide more detailed explanations (go over one

    line) when necessary.

  • BASIC SYNTAX - COMMENT

    A single line comment is written as follows:

    // Your comment can go in here

    Everything after the // will be ignored when the script is executed.

    Single line comment can also be placed on the end of a statement.

    Example:

    print("Hello $name); // Welcome to the user

  • BASIC SYNTAX - COMMENT

    The multiline style of commenting is the same as in C.

    A multiline comment is written as follows:

    /*

    The following piece of code will take the input

    the user gave and will check that it is valid before

    adding it to the database

    */

    Anything between the /* and the */ will be ignored.

    It is important to always close this type of comment as not doing so could

    make the script not working.

  • BASIC SYNTAX - COMMENT

    Example :

  • BASIC SYNTAX - variable

    As with other programming languages, PHP allows you to define variables.

    All variable begin with a $ sign.

    In PHP there are several variable types, but the most common is called a

    string.

    Variables can hold text and numbers.

  • BASIC SYNTAX - variable

    To assign some text to a string you would use the following code:

    $welcome_text = "Hello and welcome to my website.";

    Variables are case sensetive so $Welcome_Text is not the same as $welcome_text

    When assigning numbers to variables you do not need to include the quotes so:

    $user_id = 987;

    would be allowed.

  • BASIC SYNTAX - variable

    Rules for PHP variables:

    A variable starts with the $ sign, followed by the name of the variable

    A variable name must begin with a letter or the underscore character

    A variable name can only contain alpha-numeric characters and

    underscores (A-z, 0-9, and _ )

    A variable name should not contain spaces

    Variable names are case sensitive ($y and $Y are two different

    variables)

  • OPERATION AND EXPRESSION

    Expressions are used to perform operations and give an answer to a single

    value.

    Expressions consist of two parts : operators and operands.

    Operands can be:

    1) Variables

    2) Numbers

    3) Strings

    4) Boolean values

    5) other expressions.

  • OPERATION AND EXPRESSION

    a = 3 + 4

    b = (3 + 4) / 2

    expression (3+4) is used as an

    operand along with b and 2

    Example 1: Example 2:

    operands operands operators operators

  • OPERATION AND EXPRESSION

    Operators are used perform operations on operands (variables and

    values).

    Following are major categories of operator supported by PHP.

    1) Arithmetic Operators

    2) Comparison Operators

    3) Logical Operators

    4) Assignment Operators

  • OPERATION AND EXPRESSION

    Arithmetic operators apply mathematical functions to the operands.

    Operator Name Description Example Result

    + Addition Sum of x and y

    Example : x + y 2 + 2 4

    - Subtraction Subtract of x and y

    Example : x - y 5 - 2 3

    * Multiplication Multiplication of x and y

    Example : x * y 5 * 2 10

    / Division Division of x and y

    Example : x / y 15 / 5 3

    % Modulus Remainder of x divided by y

    Example : x % y 5 % 2

    10 % 8

    10 % 2

    1

    2

    0

    ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT

  • OPERATION AND EXPRESSION

  • OPERATION AND EXPRESSION

    Comparison operator compare one operand to another operand.

    Comparison operators are used inside conditional statements and evaluate

    to either true or false.

    They give the ability to compare whether elements are equal, identical, less

    than or greater than one another (with some other variations).

    ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT

  • OPERATION AND EXPRESSION

    Operator Name Description Example Result

    == Equal True if x is equal to y

    Example : x == y 5==8

    FALSE

    !=

    !==

    Not equal True if x is not equal to y or

    they are not of same type

    Example : x != y o