introduction to server-side web programming acm 511 acm 262 course notes

37
INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

Upload: brenda-trim

Post on 11-Dec-2015

222 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 262 Course Notes

INTRODUCTION TO SERVER-SIDE

WEB PROGRAMMING

ACM 511

Page 2: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Web Development Environment

Authoring Web pages is not a particularly difficult task now-a-days. Many standard desktop software packages come equipped with built-in features to convert word processing documents, spreadsheets, databases, and the like to coded documents that are ready for access across the Web.

Special Web page authoring packages such as Microsoft FrontPage and Adobe Dreamweaver permit creation of Web pages with drag-and-drop ease. In most of these cases it is not even necessary to know or to be aware of the special HTML(HyperText Markup Language) coding that takes place behind the scenes.

If you know the XHTML language, then you can author your Web pages with a simple text editor, usually gaining a great deal more control over their structure and formatting than is possible with drag-and-drop methods. In addition, you have the ability to easily integrate existing XHTML code, Java applets, multimedia plug-ins, and browser scripting languages to bring a modicum of user interactively to your pages.

Page 3: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Web Development Environment

Web "development," as contrasted with Web page "authoring," goes well beyond the use of markup codes and a few plug-ins or scripting techniques to make attractive and informative Web pages. The term pertains to the use of special strategies, tools, and methods for producing Web pages and Web sites characterized as three-tier, client/server, and information processing systems.

Web technologies are used to produce not just simple personal or promotional Web sites containing informative, interesting, or entertaining material for public consumption. Rather, they are becoming important means for supporting the foundational "business processes" of modern organizations -- the underlying operational and management-support functions. The technical infrastructures for supporting these purposes are roughly classified into three types of Web-based systems, termed: intranets , internets, and extranets.

Page 4: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Web Development Environment

Page 5: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Information Processing Model

The input function permits users to interact with the system, requesting processing options, controlling information access, and specifying delivery methods. Plus, the user can become the source of the data which the system processes and which it maintains in its repositories of stored information.

The processing function refers to the data manipulation activities and the processing logic needed to carry out the work of the system. The term implies that the system can be "programmed" to perform the arithmetic and logical operations necessary to manipulate input data and to produce output information.

The output function delivers the results of processing to the user in a correct, timely, and appropriately formatted fashion.

The storage function ensures the currency and integrity of processed information, maintaining it over the long term and permitting it to be added to, changed, or deleted in systematic fashion. In the final analysis, stored information becomes the primary content of Web pages.

Page 6: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

PHP

http://www.sedh.gr/tutorials/web_dev/PHP/default.htm

http://phptester.net/

Page 7: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

PHP

XML tag style <?php

PHP Code Block?>

Short style <?

PHP Code Block?>

Tag style <script language="php">

PHP Code Block</script>

Page 8: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Mixing HTML and PHP

Page 9: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Displaying Content

The echo and print statements appear in the following format:

echo - used to output one or more strings. 

echo "Text to be displayed"

print - used to output a string. In some cases the print statement offers greater functionality than the echo statement. These will be discussed in later tutorials. For now print can be considered an alias for echo. 

print "Text to be displayed“

  With both the echo and print statements, it is necessary to use a <p> or <br/> tag to create paragraphs or line breaks.

Page 10: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Displaying Content

Page 11: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Instruction Terminator

Page 12: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Commenting your code

// - simple PHP comment 

# - alternative simple PHP comment 

/*...*/ - multi-line comment blocks.

Comments are ignored by the PHP parser.

Page 13: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Commenting your code

Page 14: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Scalar Variables

Variables are temporary place holders used to represent values used in a PHP script. PHP includes two main types of variables: scalar and array. Scalar variables contain only one value at a time , and array variables contain a list of values. PHP scalar variables contain the following types:

Integers - whole numbers or numbers without decimals. (1, 999, 325812841) 

Floating-point numbers (also known as floats or doubles) - Numbers that contain decimals. (1.11, 2.5, .44) 

Strings - text or numeric information. String data is always specified with quotes ("Hello World", "478-477-5555") 

Boolean - used for true or false values

PHP variables of all types begin with the "$" sign. Variable names can contain letters, numbers, and the (_) underscore character. Variables cannot, however, begin with numbers. In PHP variable names are case sensitive.

Page 15: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Scalar Variables

Legal Variable Names 

$myvar $F_Name $address1 $my_string_variable 

Illegal Variable Names 

Myvar $&62## 

$username = "jdoe"$first_name = "John"$Last_Name = "Doe"

Page 16: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Scalar Variables

Page 17: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Scalar Variables

Page 18: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Scalar Variables

Variable ConcatenationThe dot operator can also be used to join strings and variables

Page 19: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Scalar Variables

InterpolationPHP also supports a process known as interpolation - replacing a variable with its contents within a string.

Page 20: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Array Variables

While a PHP scalar variable stores a single value, an array variable can be used to store a set or sequence of values. PHP supports numerically indexed arrays and associative arrays. An array in PHP is actually an ordered map. A map is a type that maps values to keys. Array variables consist of two parts - an index and an element. The array index, sometimes referred to as the array key, is a value used to identify or access array elements. The array index is placed in square brackets. Most arrays use numeric indices that typically start with 0 or 1. In PHP associative arrays can use string indices. Both types of arrays are created using the array() construct.

Page 21: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Array Variables

Numerically Indexed Arrays

$my_array = array('red', 'green', 'blue')

This code creates a numerically indexed array called $my_array. The array is assigned three elements - red, green, and blue. Each element is identified by a numeric index.

$my_array[0] = 'red' //index 0 corresponds to element red$my_array[1] = 'green' // index 1 corresponds to element

green$my_array[2] = 'blue' // index 2 corresponds to element blue

Page 22: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Array Variables

Page 23: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Array Variables

Associative Arrays

Associative arrays allow you to use more useful index values. With numerically indexed arrays, index values are created automatically beginning with 0. Associative arrays permit numeric and string index values. The symbol between the index and values (=>) is an equal sign immediately followed by a greater than symbol.

$members = array('FName' => John, 'LName' => Smith, 'Age' => 50)

In this example, the array members contain three elements, however the string indices - FName, LName, and Age are used.

$members['FName'] = 'John' //index FName corresponds to element John$members['LName'] = 'Smith' // index LName corresponds to element Smith$members['Age'] = '50' // index Age corresponds to element 50

Page 24: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Array Variables

Page 25: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Array Variables

Array Functions

count() - the count function is used to count the number of elements in an array. 

sort() - the sort function is used to sort the elements of an existing array.

shuffle() - the shuffle function is used to randomize the elements of a given array.

sizeof() - the sizeof function is an alias of the count() function.

array_slice($array_name,offset, length) - the array_slice function is used to extract a chuck of an existing array.$array_name is the name of the array to slice, offset denotes the position where the slice will begin, length indicates the number of elements that will be sliced from the array.

array_merge($array_name, $array_name) - the array_merge function is used to combine or merge two or more existing arrays. The names of the arrays are separated by commas.

Page 26: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Array Variables

Page 27: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Constants

A constant, like a variable, is a temporary placeholder in memory that holds a value. Unlike variables, the value of a constant never changes. When a constant is declared, the define() function is used and requires the name of the constant and the value you want to give the constant.

Constants can be assigned the following types of data:

Integers - whole numbers or numbers without decimals. (1, 999, 325812841) 

Floating-point numbers (also known as floats or doubles) - Numbers that contain decimals. (1.11, 2.5, .44) 

Strings - text or numeric information. String data is always specified with quotes ("Hello World", "478-477-5555")

Page 28: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Constants

PHP constants unlike variables do not begin with the "$" sign. Constant names are usually uppercase. Constant names can contain letters, numbers, and the (_) underscore character. Constants cannot, however, begin with numbers. Declaring constants is demonstrated below:

define("STRING_CONSTANT", "This is my string."); 

define("NUMERIC_CONSTANT", 5);

Page 29: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Constants

Page 30: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Operators

3 examples

Page 31: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Operators

Page 32: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Operators

Page 33: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Operators

Page 34: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Strings

Single Quoted Strings Single quotes provide the easiest method for working with strings. Using this method, strings are surrounded by single quotes (''). If single quotes are needed as part of the display, they must be escaped with the backslash ("\") character. While single quotes provide an easy method for working with strings, single quotes do not support the use of interpolation.

Double Quoted StringsPHP strings can also be displayed using double quotes (""). If PHP strings are enclosed in double quotes, it is possible to take advantage of interpolation. With double quoted strings, PHP also supports more escape characters

Page 35: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Strings

Page 36: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Strings

strlen(string) - determines the length of a string. 

ltrim(string) - strips whitespace from the beginning of a string.

rtrim(string) - strips whitespace from the end of a string.

strpbrk(string, char) - Searchs the string for the character (char). Returns false or string beginning with the character found.

strtoupper(string) - converts string to uppercase.

strtolower(string) - converts string to lowercase.

strrev(string) - reverses a string.

eregi(string pattern, string subject) - Performs a case insensitive expression match. Searches subject for a match to the regular expression given in pattern.

Page 37: INTRODUCTION TO SERVER-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes

ACM 365 Course Notes

Strings