introduction to server-side scripting

32
Introduction to Server- side Scripting CSC318 DYNAMIC WEB APPLICATION DEVELOPMENT

Upload: amirul-shafeeq

Post on 09-Feb-2017

255 views

Category:

Devices & Hardware


7 download

TRANSCRIPT

Page 1: introduction to server-side scripting

Introduction to Server-side Scripting

CSC318DYNAMIC WEB APPLICATION DEVELOPMENT

Page 2: introduction to server-side scripting

• How does a Server-Side Page Work• Introduction to PHP

CONTENT

Page 3: introduction to server-side scripting

• "Server-side" just means that the control of the script is handled by the Web Crossing server rather than running a script on each user's personal computer.

• Web Crossing runs the scripts and sends standard HTML (web pages) to each user's browser.

• All the end user's browser has to worry about is displaying the results and does not have to worry about the underlying script used to generate the web pages.

How does a Server-Side Page Work

Page 4: introduction to server-side scripting
Page 5: introduction to server-side scripting

Examples of Server-side Scripting Language• ASP (*.asp)• ASP.NET (*.aspx)• ColdFusion Markup Language (*.cfm)• JavaServer Pages• PHP (*.php)• Python (*.py)• Ruby (*.rb, *.rbw)• SMX (*.smx)• Lasso (*.lasso)• Tcl (*.tcl)• WebDNA (*.dna,*.tpl)

Page 6: introduction to server-side scripting

Introduction to 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 many databases (MySQL, Informix,

Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)

• PHP is an open source software (OSS) • PHP is free to download and use

Page 7: introduction to server-side scripting

Introduction to PHP

• PHP files may contain text, HTML tags and scripts • PHP files are returned to the browser as plain

HTML • PHP files have a file extension of ".php", ".php3",

or ".phtml" • PHP is compatible with almost all servers used

today (Apache, IIS, etc.) • PHP is FREE to download from the official PHP

resource: www.php.net

Page 8: introduction to server-side scripting

Basic PHP Syntax • A PHP scripting block always starts with <?php and ends with ?

>. A PHP scripting block can be placed anywhere in the document.

• On servers with shorthand support enabled you can start a scripting block with <? and end with ?>.

• However, for maximum compatibility, we recommend that you use the standard form (<?php) rather than the shorthand form.

<?php :?>

Page 9: introduction to server-side scripting

Example

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

• Two basic statements to output text with PHP: echo and print. • Note:

The file must have the .php extension. In file with the .html extension, the PHP code will not be executed.

Page 10: introduction to server-side scripting

Comments in PHP

• In PHP, we use // to make a single-line comment or /* and */ to make a large comment block.

• Example:

<html> <body> <?php //This is a comment /* This is a comment block */ ?> </body> </html>

Page 11: introduction to server-side scripting

Variables in PHP

• Variables are used for storing a values, like text strings, numbers or arrays.

• When a variable is set it can be used over and over again in your script

• All variables in PHP start with a $ sign symbol. • The correct way of setting a variable in PHP:

$var_name = value; • Example:

<?php $txt = "JKA"; $number = 1517; ?>

Page 12: introduction to server-side scripting

Variable Naming Rules

• A variable name must start with a letter or an underscore "_"

• A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ )

• A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString )

Page 13: introduction to server-side scripting

Strings in PHP

• String variables are used for values that contains character strings.

• After we create a string we can manipulate it. A string can be used directly in a function or it can be stored in a variable.

• Example:

Page 14: introduction to server-side scripting

The Concatenation Operator

• The concatenation operator (.) is used to put two string values together.

• To concatenate two variables together, use the dot (.) operator.

• Example:

Page 15: introduction to server-side scripting

Using the strlen() function• The strlen() function is used to find the length of a string.• The length of a string is often used in loops or other functions,

when it is important to know when the string ends. (i.e. in a loop, we would want to stop the loop after the last character in the string)

• Example:

Page 16: introduction to server-side scripting

Using the strpos( ) function

• The strpos() function is used to search for a string or character within a string.

• If a match is found in the string, this function will return the position of the first match. If no match is found, it will return FALSE.

• Example:

• As you see the position of the string "world" in our string is position 6. The reason that it is 6, and not 7, is that the first position in the string is 0, and not 1.

Page 17: introduction to server-side scripting

PHP Arithmetic Operators

Page 18: introduction to server-side scripting

PHP Assignment Operators

Page 19: introduction to server-side scripting

PHP Comparison Operators

Page 20: introduction to server-side scripting

PHP Logical Operators

Page 21: introduction to server-side scripting

Conditional statement

• The If...Else Statement– Syntax:

– Example:

Page 22: introduction to server-side scripting

Conditional statement

• The ElseIf Statement– Syntax:

– Example:

Page 23: introduction to server-side scripting

Looping

• In PHP we have the following looping statements: a) while - loops through a block of code if and as long

as a specified condition is true b) do...while - loops through a block of code once, and

then repeats the loop as long as a special condition is true

c) for - loops through a block of code a specified number of times

d) foreach - loops through a block of code for each element in an array

Page 24: introduction to server-side scripting

The for Statement

• Syntax:

– init: Is mostly used to set a counter, but can be any code to be executed once at the beginning of the loop statement.

– cond: Is evaluated at beginning of each loop iteration. If the condition evaluates to TRUE, the loop continues and the code executes. If it evaluates to FALSE, the execution of the loop ends.

Page 25: introduction to server-side scripting

The for Statement

– incr: Is mostly used to increment a counter, but can be any code to be executed at the end of each loop.

• Example:

Page 26: introduction to server-side scripting

The while Statement

• Syntax:

• Example:

Page 27: introduction to server-side scripting

The do...while Statement

• Syntax:

• Example:

Page 28: introduction to server-side scripting

PHP Functions

• A function is a block of code that can be executed whenever we need it. Creating PHP functions: All functions start with the word "function()" Name the function - It should be possible to understand

what the function does by its name. The name can start with a letter or underscore (not a number)

Add a "{" - The function code starts after the opening curly brace

Insert the function code Add a "}" - The function is finished by a closing curly brace

Page 29: introduction to server-side scripting

Example

Page 30: introduction to server-side scripting

PHP Functions - Adding parameters

• To add more functionality to a function, we can add parameters. A parameter is just like a variable.

• Example:

Page 31: introduction to server-side scripting

Example

Page 32: introduction to server-side scripting

PHP Functions - Return values

• Functions can also be used to return values. • Example: