14
INTRO TO SERVER SIDE DEVELOPMENT Week Six Friday, October 4, 13

DIG1108 Lesson 6

Embed Size (px)

Citation preview

Page 1: DIG1108 Lesson 6

INTRO TO SERVER SIDE DEVELOPMENTWeek Six

Friday, October 4, 13

Page 2: DIG1108 Lesson 6

General ReviewReference: php.netLiterals:booleanintegerfloatstringarrayobject

Variables:$anything$any['array']

Expressions1 + 1 == 2$a = $b + 1

Control Structuresconditional branchesconditional loops:while, do-whilefor, foreach

Workflow Diagrams:diamonds: decisionsrectangles: processarrows: branches

Version Control & GitCloud9 IDE:cloning from Githubsharing workspaces

Friday, October 4, 13

Page 3: DIG1108 Lesson 6

Review - ArraysAn array type is a data type that is meant to describe a collection of values or variables, each selected by one or more indices(keys) tha can be computed at run-time of the program.

By default, the array type in PHP generates computed, numerical indices, starting with zero, to make a list: var_dump(array( 'one', 'two', 3, 4, 4.1, 4.2 )); $list = array(); $list[] = 'one'; $list[] = 'two';

The value for the key can also be specified as any scalar literal (neither array, object nor resource), often a string var_dump(array( 'one' => 1, 2 => 'two' )): $list[4] = 'four'; $list['five dot one'] = 5.1;

Friday, October 4, 13

Page 4: DIG1108 Lesson 6

Control Flow StatementsControl Flow - refers to the order in which the individual statements, instructions or function calls of an imperative or declarative program are executed or evaluated. Execution results in a choice being made as to which of two or more paths should be followed.

Types of Control Flow statements:

continuation at a different statement (unconditional branch or jump)

execute statements only if some condition is met (conditional branch)

execute statements until some conditional is met(loop, conditional branch)

execute defined statements and return (sub/co-routines, continuations)s

stop executing statements (unconditional halt)

Friday, October 4, 13

Page 5: DIG1108 Lesson 6

Review - LoopsA loop is a sequence of statements which is specified once but which may be carried out several times in succession, a specified number of times or indefinitelySpecific number of times: for ( $count = 0; $count < $max; $count++ ) do_something();

Once per each item in a collection(array): foreach ( $collection as $item ) do_something(); foreach ($collection as $key => $value ) do_something();

Until some condition is met: while ( $condition == true ) do_something(); do something(); while ( $condition );

Indefinitely(infinitely): while ( true ) do_something(); do something(); while ( true);

Friday, October 4, 13

Page 6: DIG1108 Lesson 6

Procedural ProgrammingProcedural programming is based on specifying the steps the program must take to reach the desired state.

Procedures, also known as routines, subroutines, methods or functions contain a series of computational steps to be carried out. Any given procedure might be called at any point during a program's execution.

Types of Control Flow statements:

continuation at a different statement (unconditional branch or jump)

execute statements only if some condition is met (conditional branch)

execute statements until some conditional is met(loop, conditional branch)

execute defined statements and return (sub/co-routines, continuations)s

stop executing statements (unconditional halt)

Friday, October 4, 13

Page 7: DIG1108 Lesson 6

DeclarationsUnlike variables, functions must be "declared" to use do_something(); // "calling" an undefined function !! Fatal Error: function do_something is not definedThe keyword function declares a function function do_nothing() { } do_nothing(); // "invoking" a functionEach function can only be declared once: foreach ( range(1, 10) as $loop ) function once_and_only_once() { } !! Fatal Error: Cannot redeclare once_and_only_once()

Friday, October 4, 13

Page 8: DIG1108 Lesson 6

Modular ProgrammingModular Programming ("top-down design" or "stepwise refinement") is a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality

Separation of Concerns - one piece at a time. Increasing the complexity of a system compounds the difficulty of maintaining it; smaller and simpler components are easier to maintain

Abstraction - write it once and only once

Encapsulation - everything needed is there

Scoping - doesn't affect other elements

Friday, October 4, 13

Page 9: DIG1108 Lesson 6

FunctionsFunctions must start with the function keyword, must contain a valid function identifier (with variables), provide an optional list of arguments surrounded by parentheses, even if there are none, and a block of code: function do_something( $an_argument, $another ) { // Code goes here }

Nothing from outside is visible to code in the blockNothing inside the block is visible outside the function

Pass values into a function as arguments at invocation: do_something( 'some value', "$another_value );

Friday, October 4, 13

Page 10: DIG1108 Lesson 6

$outside_of_scope = 'outside only';

function do_something( $an_arg, $arg_two = false ) {

$inside_of_scope = 'inside only';

if( $arg_two ) echo $outside_of_scope;

echo $an_arg; // passed in at invocation

return $inside_of_scope; // passed back out}

do_something( 'now' ); // prints "now"echo $inside_of_scope; // Notice: Undefined variable

do_something( 'again', true ); // Notice: Undefined variable

Friday, October 4, 13

Page 11: DIG1108 Lesson 6

ASSIGNMENT 6.1Finding Functions

Friday, October 4, 13

Page 12: DIG1108 Lesson 6

Finding Functions Pair up, login to Github, login to Cloud9

Open an existing Workspace and find some functions togetherCopy and paste the function definition for each into a new file called assignment-6.1.md in your assignments workspaceIdentify the name of the function and the names of all of the arguments with comments; bonus points for identifying the return value of the functionFind at least three invocations of each function

Friday, October 4, 13

Page 13: DIG1108 Lesson 6

ASSIGNMENT 6.2Identifying Functions & Scope

Friday, October 4, 13

Page 14: DIG1108 Lesson 6

Functions & Scope

Find at least three functions in the WordPress projectDocument them in a new file called assignment-6.2.mdUse the format: path/to/file.php:9999

Identify the name of the function and its arguments with commentsIdentify the in-scope variables by nameIdentify the return value of each functionAdd and commit your file, push to Github

Friday, October 4, 13