Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 5 SERVER SIDE SCRIPTING

Similar presentations


Presentation on theme: "CHAPTER 5 SERVER SIDE SCRIPTING"— Presentation transcript:

1 CHAPTER 5 SERVER SIDE SCRIPTING
PART 1 OF 3 [Principles | Operation & Expression | Output] Madam Hazwani binti Rahmat

2 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).

3 SERVER SIDE SCRIPTING The user requests a web page from the web server. 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 Information it outputs (the html content) is sent back to the browser so that it can be displayed to the user.

4 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.

5 HOW SERVER SIDE SCRIPTS WORK?
Ex : myshop.com User requests a web page from the web server Web server executes the code and generate HTML content Output sent back to the browser Ex : <?php echo (“Hello world!”); ?> Ex : <p>Hello world</p>

6 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

7 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.

8 BASIC SYNTAX Following are different style for writing PHP script:
Everything between the <?php and ?> is read as PHP code. The statement can also be phrased as simply <? if desired. Anything outside of these PHP tags is read as HTML, hence we can easily switch between PHP and HTML as needed.

9 BASIC SYNTAX The three different forms are as follows: Style 1 <?php PHP Code In Here ?> Style 2 <? PHP Code In Here ?> Style 3 <script language="php"> PHP Code In Here </script>

10 BASIC SYNTAX There are two ways to use HTML on PHP page.
The first way is to put the HTML outside of PHP tags. <?php -and- ?> are put it in the middle HTML. 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.

11 BASIC SYNTAX – Style 1 Example :
<html> <body> <?php //your php code here ?> <b>Here is some more HTML</b> //more php code </body> </html> Example : <html> <body> <?php echo("Hello World!“); ?> <h1>My first PHP page</h1> <?php echo(“Good Morning!“); </body> </html>

12 BASIC SYNTAX – Style 2 Example :
<?php echo("<html>“); echo("<title>Style 2</title>“); echo("<body>“); echo("<b>My Example</b>“);  print("<i>Hello!</i>“); echo("</html>“); ?> This is a nice quick method if you only have a line or so to do.

13 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: Single-line comments:  generally used for single line comments (short explanations of code). Multi-lines comments:  generally used to provide more detailed explanations (go over one line) when necessary.

14 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

15 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.

16 BASIC SYNTAX - COMMENT Example : <?php
echo("Hello World!“); // example of single line comment /* This echo statement will print out a hello world message */ echo("Hello World!“); ?>

17 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.

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

19 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)

20 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: Variables Numbers Strings Boolean values other expressions.

21 OPERATION AND EXPRESSION
Example 1: Example 2: a = 3 + 4 b = (3 + 4) / 2 expression (3+4) is used as an operand along with b and 2 operators operands operators operands

22 OPERATION AND EXPRESSION
Operators are used perform operations on operands (variables and values). Following are major categories of operator supported by PHP. Arithmetic Operators Comparison Operators Logical Operators Assignment Operators

23 OPERATION AND EXPRESSION
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT 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 % Modulus Remainder of x divided by y Example : x % y 5 % 2 10 % 8 10 % 2 1 2 0

24 OPERATION AND EXPRESSION
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT <?php $a = 42; $b = 20; $c = $a + $b; echo("Addition Operation Result: $c“); $c = $a - $b; echo("Subtraction Operation Result: $c“); $c = $a * $b; echo("Multiplication Operation Result: $c“); ?> Output : Addition Operation Result: 62 Subtraction Operation Result: 22 Multiplication Operation Result: 840

25 OPERATION AND EXPRESSION
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT 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).

26 OPERATION AND EXPRESSION
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT 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 or x <> y or x !== y 5!=8 5<>8 5!=="5" TRUE > Greater than True if x is greater than y Example : x > y 5>8 < Less than True if x is less than y Example : x < y 5<8 >= Greater than or equal to True if x is greater than or equal to y Example : x >= y 5>=8 <= Less than or equal to True if x is less than or equal to y Example : x <= y 5<=8

27 OPERATION AND EXPRESSION
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT <?php $a = 42; $b = 20; if($a==$b) { echo("TEST1 : a is equal to b“); } else echo("TEST1 : a is not equal to b“); ?> Output : TEST1 : a is not equal to b

28 OPERATION AND EXPRESSION
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT Logical operators compare elements using comparisons AND, OR, and the LIKE. Operator Name Description Example Result && AND True if both x and y are true Example : x && y x=6 ,y=3 (x < 10 && y > 1) TRUE || OR True if either or both x and y are true Example : x || y x=6 , y=3 (x==5 || y==5) FALSE ! NOT True if x is not true Example : !x x=6, y=3 !(x==y) XOR True if either x or y is true, but not both Example : x xor y x=6 ,y=3  (x==6 xor y==3)

29 OPERATION AND EXPRESSION
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT <?php $a = 42; $b = 0; if($a<10 && $b>1) { echo("TEST1 : Both a and b are true“); } else echo("TEST1 : Both or either a or b is false); ?> Output : TEST1 : Both or either a or b is false

30 OPERATION AND EXPRESSION
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT The assignment operator is used to assign values to variables in PHP. Such an assignment of value is done with the "=", or equal character. Operator Name Description ++ x Pre-increment Increments x by one, then returns x x ++ Post-increment Returns x, then increments x by one -- x Pre-decrement Decrements x by one, then returns x x -- Post-decrement Returns x, then decrements x by one

31 OPERATION AND EXPRESSION
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT Assignment Details Description Example Result x = y The left operand gets set to the value of the expression on the right x=6, y=3 x=3 y=3 x += y x = x + y Addition x=9 y=3 x -= y x = x - y Subtraction x=6, y=3 x -= y x *= y x = x * y Multiplication x=6, y=3 x *= y x=18 y=3 x /= y x = x / y Division x=6, y=3 x /= y x=2 y=3 x %= y x = x % y Modulus x=6, y=3 x %= y x=0 y=3

32 OPERATION AND EXPRESSION
ARITHMETIC | COMPARISON| LOGICAL| ASSIGNMENT <?php $a = 42; $b = 20; /* Assignment operator */ $c = $a + $b; echo("Addition Operation Result: $c“); /* c value was = 62 */ $c += $a; echo("Add AND Assignment Operation Result: $c“); ?> Output : Addition Operation Result: 62 Add AND Assignment Operation Result: 104

33 OUTPUT There are 4 different ways of outputting text to the browser:
echo("Text here"); echo "Text here"; print("Text here"); print "Text here"; All of these do the same thing where the information to be printed is contained between the quotation mark. However, aapparently in very large programs that are simply outputting text, the ECHO statement will run slightly faster.

34 OUTPUT Example: <?php echo("Hello World"); echo “Hello World"; print("Hello World"); print "Hello World"; ?> Echo and print commands can prints either a string variable or quotes.

35 OUTPUT – outputting string
Echo and print uses quotes to define the beginning and end of the string. Hence, to use a quotation mark inside echo or print, the escape character; a backslash (\) must be used: The backslash will tell PHP that the quotations are to be used within the string and NOT to be used to end echo's string.

36 OUTPUT – outputting string
Example : <?php // This won't work because of the quotes around specialH5! echo "<h5 class="specialH5">Hello World!</h5>";   // OK because we escaped the quotes! echo "<h5 class=\"specialH5\">Hello World!</h5>";   // OK because use of apostrophe ‘ instead echo "<h5 class='specialH5'>Hello World!</h5>"; ?>

37 OUTPUT – outputting variable
To output variable values, no quotations are required, even if the variable hold a string. Example : <?php $my_string = "Hello, my name is: "; $my_number = 4; $my_letter = a;  echo $my_string; echo $my_number; echo $my_letter; ?> Output : Hello, my name is: 4a

38 OUTPUT – outputting variable & text
Outputting variables can also be done by placing the variable inside of double-quoted strings (e.g. "string here and a $variable"). Putting a variable inside the quotes (" ") tells PHP to take the string value of that variable and use it in the string. Example: <?php $my_string = "Hello, my name is: ";  echo "$my_string Bob"; ?> Output : Hello, my name is: Bob


Download ppt "CHAPTER 5 SERVER SIDE SCRIPTING"

Similar presentations


Ads by Google