CIS505 Class Notes

=Concepts Overview=

Coding Conventions
The main source of inspiration for the coding conventions in CIS 505 are Drupal's coding standards at http://drupal.org/coding-standards.

Naming
Use a camelCase naming convention when giving names to all variables, parameters, and functions. Examples of using camelcase include: $myNumber, function arrfnOne, function arrfnTwo ($myParameter), etc..
 * In choosing the name for any script filenames, make sure not to use camelcase. Instead, keep all letters lowercase and opt to use dashes for any spaces you wish to incorporate into the name.  Example of an acceptable PHP filename: classdemo-h1.php.

Commenting
There are two types of block comments: script block comment and function block comment.

Script Block Comment
The script block comment's role is to introduce us to the functionality of the program. It includes comment tags that inform a documentation tool how to display specific types of information. The following is an example of a script block comment:

Function Block Comment
This example is for a function which takes two parameters and doesn't return anything. I have included the signature of the function as well.

Function Calls
Functions should be called with no spaces between the function name, the opening parenthesis, and the first parameter; spaces between commas and each parameter, and no space between the last parameter, the closing parenthesis, and the semicolon. If you are making a call to a function without specifying any parameters, be sure to have one space inside the open parentheses after the function name. Here's an example: There should be one space on either side of an equals sign used to assign the return value of a function to a variable. In the case of a block of related assignments, more space may be inserted to promote readability:

Operators
All binary operators (operators that come between two values), such as +, -, =, !=, ==, >, etc. should have a space before and after the operator, for readability. For example, an assignment should be formatted as $foo = $bar; rather than $foo=$bar;. Unary operators (operators that operate on only one value), such as ++, should not have a space between the operator and the variable or number they are operating on.

Control Structures
Note: In this class, for all control structures (conditionals and loops), use an opening bracket, {, right after the condition(s) you're testing. Leave a space between the closing parenthesis and the beginning brace.

See below for cis505 syntax rules regarding proper formation of control structure bodies (does this sound correct?):

if Statement
Control statements should have one space between the control keyword and opening parenthesis, to distinguish them from function calls.

You are strongly encouraged to always use curly braces even in situations where they are technically optional. Having them increases readability and decreases the likelihood of logic errors being introduced when new lines are added.

The following are some examples of appropriate syntax used within a sample student script:

Arrays
Arrays should be formatted with a space separating each element (after the comma), and spaces around the => key association operator, if applicable: Note that if the line declaring an array spans longer than 80 characters (often the case with form and menu declarations), each element should be broken into its own line, and indented one level:

=Iterative Programming=

In CIS505, we apply a computer programming technique known as iterative programming. With this technique we design our PHP scripts in small, easily understood blocks of code. The advantage of this technique is that implementation is driven by the design and logic of the programming solution.

The Wikipedia article on Iterative and incremental development (http://en.wikipedia.org/wiki/Iterative_and_incremental_development) Defines the idea and goal of the iterative development as follows:

''The basic idea behind the agile method is to develop a system through repeated cycles (iterative) and in smaller portions at a time (incremental), allowing software developers to take advantage of what was learned during development of earlier parts or versions of the system. Learning comes from both the development and use of the system, where possible key steps in the process start with a simple implementation of a subset of the software requirements and iteratively enhance the evolving versions until the full system is implemented. At each iteration, design modifications are made and new functional capabilities are added. ... The goal for the design and implementation of any iteration is to be simple, straightforward, and modular, supporting redesign at that stage ...''

By using the iterative programming technique:
 * Any PHP script must have a function call to the function that drive the processing of that scrip.
 * The script's function, in turn, makes calls to other functions.
 * No function should have code that exceeds 30-40 lines.

=Getting Started with PHP Programming=

What is a variable?
A variable is used to hold a value, such as a number or a string, or a set of values, if it's an array. A variable has a name that the programmer picks. The name must start with the $ sign and contains only letters, digits and underscore.

The simplest way to define variables is when we write assignment statements. Variables are also created when we write a function and we list its parameters in the function signature. Finally, variables are used in expressions, which combine operations with numbers, strings, variable names, and function calls.

Examples
The assignment statements above use three variables. Values of type integer, string, and of type array are assigned to these variables. Notice that number values are written as they are. However, a string value must be written within double quotes. Finally to create an array, the keyword array must precede a pair of parentheses within which we list the values in the array, whether they are string values, numbers or other arrays.

What is a statement?
To instruct the PHP interpreter to execute something, we write a statement. Simple statements, such as assignments, end with ;.

When we described variables, we introduced the assignment statement. Other types of statements are:
 * conditionals, also known as selection statements or decision statements. The keywords that divulge conditionals are if, else, elseif, switch, and case.
 * loops, which use the keywords while, do ... while, for, and foreach.
 * function calls
 * return statements
 * break and continue statements

Conditionals and loops are compound statements. Compound statements do not end with ;. This is because they have some kind of header, followed by a block of statements, always enclosed in { and }.

Function calls, when used as statements, end with ;. Function calls can also be used in expressions.

Examples
This is an assignment statement. The value "Margerat" is assigned to the variable $myname.

This is an if-else conditional statement.
 * It starts with the keyword if, followed by an expression that is enclosed within parentheses: $myName == "".
 * After the if condition, we write the block of statements that corresponds to this if branch. A block of statements is enclosed within { and }.
 * The next part is the else branch. Note that there is no condition for else. The block of statements that correspond to the else branch is also enclosed within { and }.
 * Both if and else branches have only one statement. In both cases the statement is a function call. The PHP library function print is called with a string value that gets output when this coding example is interpreted.

Handling Forms
Handling forms in PHP combines PHP application logic code with HTML user interface code. The form uses the HTML element  ...  and contains other HTML elements that are form specific, such as .

To process the form, the action attribute of the form is assigned as value the URL of the PHP script that will do the processing. When the form is submitted (its URL is entered in the browser), the web server commands the PHP interpreter to execute the PHP script in the action attribute. The PHP interpreter produces a dynamic web page which is served back to the browser.

In summary, four conditions are needed to build an HTML form:
 * An opening and closing html tag.
 * Specify a submission type, either get or post for the method attribute of the form.
 * One or more input fields.
 * The destination URL of the PHP script that resides on the web server and processes the form. This URL can be a path and file name that are relative to the document root path and server URL (as configured for the web server). The destination URL value is assigned to the action attribute of the form.

Examples
The following section is designed to give examples of form input types. In keeping with the iterative programming theme, the first part of the example shows how to display the given example form using HTML; the second part shows the PHP processing required to use the form input in order to display dynamic output to a client. Working within a PHP script, if creating a form, the first thing to do is to call an echo statement and immediately specify a heredoc operator, which will encompass the entire HTML code for the form.

Text Boxes
We create a text box by specifying a value of "text" for the type attribute.

Submit Button
=Model-View-Controller Architectural Pattern= Model-View-Controller (MVC) is a way of organizing the design and implementation of an application with a Graphical User Interface (GUI). In an MVC organization the user interface (view), the data, usually managed by a database (model), and the logic of the application (controller) are kept as separate as possible.

MVC was invented in 1979 by Trygve Reenskaug, a Norwegian computer scientist who was visiting Xerox Palo Alto Research Center (PARC) at that time.

The MVC is called architectural pattern because it provides a general solution to a common problem. The problem, in the words of the MVC inventor, concerns users who are controlling a large and complex data set through a graphical user interface. The solution proposes the decomposition of the software system into three subsystems, Model, View, and Controller, that interact in predefined ways.