CHAPTER 5 SERVER SIDE SCRIPTING

Slides:



Advertisements
Similar presentations
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Advertisements

Introduction to C Programming
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
JavaScript, Third Edition
Introduction to C Programming
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
PHP Workshop ‹#› PHP: The Basics. PHP Workshop ‹#› What is it? PHP is a scripting language commonly used on web servers. –Stands for “PHP: Hypertext Preprocessor”
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
INTERNET APPLICATION DEVELOPMENT For More visit:
Nael Alian Introduction to PHP
Week 9 PHP Cookies and Session Introduction to JavaScript.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
November 2003Bent Thomsen - FIT 6-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Introduction to PHP Advanced Database System Lab no.1.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
IT ELECTIVE 2.  Web server Can refer to either the hardware (the computer) or the software (the computer application) that helps to deliver content that.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CHAPTER 6 Introduction to PHP5 Part I อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
PHP Workshop ‹#› أطلق إبداعك 2 أطلق إبداعك 2 مدرس معتمد من مركز زووم PHP: The Basics.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
FP512 WEB PROGRAMMING 1 PREPARED BY: PN. NUR SYUHADA BINTI MOHAMAD.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
PHP using MySQL Database for Web Development (part II)
Bill Tucker Austin Community College COSC 1315
Session 2 Basics of PHP.
>> Fundamental Concepts in PHP
Chapter 6 JavaScript: Introduction to Scripting
CHAPTER 4 CLIENT SIDE SCRIPTING PART 1 OF 3
Chapter 2 - Introduction to C Programming
PHP (PHP: Hypertext Preprocessor)
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Variables, Expressions, and IO
Introduction to Scripting
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
BASIC PHP and MYSQL Edward S. Flores.
PHP Introduction.
Chapter 2 - Introduction to C Programming
Intro to PHP & Variables
Exercises on JavaScript & Revision
Introduction to C++ Programming
WEB PROGRAMMING JavaScript.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
CS105 Introduction to Computer Concepts
Basics.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Chapter 2: Introduction to C++.
Tutorial 10: Programming with javascript
Introducing JavaScript
An Introduction to JavaScript
PHP an introduction.
Web Programming– UFCFB Lecture 13
Algoritma & Pemrograman 1
CS105 Introduction to Computer Concepts JavaScript
Presentation transcript:

CHAPTER 5 SERVER SIDE SCRIPTING PART 1 OF 3 [Principles | Operation & Expression | Output] Madam Hazwani binti Rahmat http://fsktm2.uthm.edu.my/hazwani/v2/

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

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.

HOW SERVER SIDE SCRIPTS WORK? Ex : http://www. 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>

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

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>

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.

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>

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.

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.

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 : <?php echo("Hello World!“); // example of single line comment /* This echo statement will print out a hello world message */ echo("Hello World!“); ?>

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

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

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

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

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

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

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

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

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)

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

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

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

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 42 + 20 = 62 */ $c += $a; echo("Add AND Assignment Operation Result: $c“); ?> Output : Addition Operation Result: 62 Add AND Assignment Operation Result: 104

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.

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.

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.

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>"; ?>

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

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