SCRIPTING, DATABASES, SYSTEM ARCHITECTURE

introduction to SCRIPTING, DATABASES, SYSTEM ARCHITECTURE PHP I: comments, variables, arithmetic, string op’s, if statements, data collection Claus ...
Author: Cody Adams
3 downloads 0 Views 982KB Size
introduction to SCRIPTING, DATABASES, SYSTEM ARCHITECTURE

PHP I: comments, variables, arithmetic, string op’s, if statements, data collection

Claus Brabrand

((( [email protected] ))) Associate Professor, Ph.D. ((( Software and Systems )))

IT University of Copenhagen

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

Sep 14, 2012

Agenda RECAP n  COMMENTS n  VARIABLES n  STRING OPERATIONS n  ARITHMETIC n  IF STATEMENTS n  DATA COLLECTION n 

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[2]

Sep 14, 2012

Form submission… n 

Form submits info to server (PHP) program:

First name:
Last name:
e John Doe

firstn ame=Jo hn lastna me=Doe PHP Program

client

server Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[3]

Sep 14, 2012

/> ” n e d d i h “ = e ut typ p n i < e l p i t l u m t c e n  Many “type”s of : sel First name:
... n 



n 



n 



n 



n 



n 



n 



Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[4]

Sep 14, 2012

PHP Example: Hello World! eral, Abbreviation: in gen body> I’ll omit and

Hello World!

This is bold and italic Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[5]

Sep 14, 2012

Variables and Arithmetic

The year is: 2012

The year is: Next year is:

Next year is: Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

Next year is: 2013

[6]

Sep 14, 2012

Agenda RECAP n  COMMENTS n  VARIABLES n  STRING OPERATIONS n  ARITHMETIC n  IF STATEMENTS n  DATA COLLECTION n 

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[7]

Sep 14, 2012

Comments n 

A comment is some text that explains what the code is doing in “human language” (for other programmers or oneself, later on)

Note: comments don’t do anything! n 

Then; why bother with comments?!?: n  1)

Makes code easier to read and understand n  2) Helps others understand your code n  3) Helps yourself understand your code, later on Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[8]

Sep 14, 2012

One-line + Multi-line comments n 

One line comments:



n 

Multi-line comments:

Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[9]

Sep 14, 2012

Comments: Example n 

An Example:



n 

Comments are ignored when PHP is executed:



SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 10 ]

Sep 14, 2012

Agenda RECAP n  COMMENTS n  VARIABLES n  STRING OPERATIONS n  ARITHMETIC n  IF STATEMENTS n  DATA COLLECTION n 

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 11 ]

Sep 14, 2012

Variables n 

n 

A variable is a container with a name that can store a value:

2012 $year

name

Name: Starts with a dollar sign ‘$’ followed by an identifier

n 

value

Value: Can be a number or a string of text or (…)

Claus Brabrand, ITU, Denmark

$year

$theYear

$the_year

42

3.14

“hello”

$x

$y

$z

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 12 ]

Sep 14, 2012

Variable Example n 

You’ve already seen an example of variables:



2012 $year n 

The year is: 2012

value name

Var’s can be used in subsequent PHP tags:

The year is: Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

The year is: 2012

[ 13 ]

Sep 14, 2012

Variable Examples… n 

Number Example:



n 

The year is: 2012

Decimal Example:



n 

Pi (π) is: 3.1415926

String (text) Example:

Claus Brabrand, ITU, Denmark

Hi, my name is Claus Brabrand

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 14 ]

Sep 14, 2012

A Larger Example n 

We can use multiple variables:

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 15 ]

Sep 14, 2012

Variables are ‘case sensitive’ n 

Variable names are case sensitive in PHP:



n 

Here is some text Here’s some other text

Use consistent naming scheme for your vars: n  $first_name n  $firstName n  $FirstName

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 16 ]

Sep 14, 2012

Variable Assignment n 

Assignment: $year = 2012 ;

n 

You can change the value of a variable:



n 

“echo” will print the current value of a variable

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 17 ]

Sep 14, 2012

Variable Types Variables can contain different types of values: n 

Integers (numbers): n  …,

n 

-3, -2, -1, 0, 1, 2, 3, …

Floating points (decimal numbers): n  1.5,

n 

2.0, 0.0, 0.41, -123.456, 3.1415926, …

Strings (pieces of text): n  “ITU”,

n 

“42”, “hello!”, “Barack Obama”, “Urgh!@$!”, …

Booleans (truth values): n  true,

Claus Brabrand, ITU, Denmark

false

(there are only these two!) SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 18 ]

Sep 14, 2012

Variable Types (continued) n 

PHP is not very strict with variables: n  We

don’t need to declare variables (define its type) n  We don’t need to initialize variables (set initial value) n 

A variable can even have different types (of values) during execution:

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 19 ]

Sep 14, 2012

Agenda RECAP n  COMMENTS n  VARIABLES n  STRING OPERATIONS n  ARITHMETIC n  IF STATEMENTS n  DATA COLLECTION n 

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 20 ]

Sep 14, 2012

Strings n 

A string is a sequence of characters enclosed in quotation marks (‘…’) or (“…”):

n 

Single quotes: n  ‘text’,

‘text with space’, ‘ ’, ‘42’, ‘in fact, any symbols %*!@’, …

n 

Double quotes: n  “text”,

“text with space”, “ ”, “42”, “in fact, any symbols %*!@”, …

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 21 ]

Sep 14, 2012

Single vs Double Quotes n 

Single quotes (‘…’) vs Double quotes (“…”):

n 

Single quotes: n  We

can not use variables inside:



n 

Double quotes: n  We

can use variables inside:

Claus Brabrand, ITU, Denmark

The year is: $year refer p e r o f ere h t y n a tes! o M u q e l doub e s u o t

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

The year is: 2012 [ 22 ]

Sep 14, 2012

Character Escaping n 

Common character escapings in text strings: (newline character) n  \t (tabulation character) n  \$ (dollar sign character) n  \’ (single quote character) n  \” (double quote character) n  \\ (backslash character) n  \n

n 

Examples:



Hello world

!

Claus Brabrand, ITU, Denmark

He said “You need this stuff!” to the[ 23students ] Sep 14, 2012

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

You can mix quotes ‘ ” ’ or “ ‘ “ n 

Note: You do not have to escape...: n  ...double

quotes inside single quotes:



Here: “double quotes” inside single quotes. n  ...single

quotes inside double quotes:



n 

However...:

Here: ‘single quotes’ inside double quotes.



But ‘same kind’ of quotes need escaping.

But “same kind” of quotes need escaping. Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 24 ]

Sep 14, 2012

String Concatenation n 

String concatenation via the ‘.’ (dot) operator: n 

n 

( “string1” . “string2” )

Examples:



danmark



Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

Hello world! [ 25 ]

Sep 14, 2012

EXERCISE n 

String concatenation via the ‘.’ (dot) operator: n 

n 

1) Make the bottom PHP program work 2) Change it to echo “Obama, Barack”

( “string1” . “string2” )

Examples:



danmark

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 26 ]

Sep 14, 2012

Agenda RECAP n  COMMENTS n  VARIABLES n  STRING OPERATIONS n  ARITHMETIC n  IF STATEMENTS n  DATA COLLECTION n 

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 27 ]

Sep 14, 2012

Arithmetic n 

You have already seen (simple) arithmetic:



n 

Next year is: 2013

Another example (that also uses assignment):



n 

Effect:

$year = 2012; Claus Brabrand, ITU, Denmark

2012 $year

Year: 2012 Year: 2013

2013 $year

$year = ($year + 1);

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 28 ]

Sep 14, 2012

More Arithmetic n 

Examples:

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 29 ]

Sep 14, 2012

More Arithmetic (rounding off) n 

We can use the built-in function round(…) to “round off” result:



Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 30 ]

Sep 14, 2012

Arithmetic Operators n 

Arithmetic operators: (addition) n  - (substraction) n  * (multiplication) n  / (division) n  +

n 

( ( ( (

1 3 5 7

+ * /

2 4 6 8

) ) ) )

Example:

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

Result is: 2.5

[ 31 ]

Sep 14, 2012

Increment and Decrement n 

Increment and decrement operators: n  $x++

(increment the value of variable x by one)

n 

n  $x--

(decrement the value of variable x by one)

n 

n 

Same as: “$x = ($x + 1)” Same as: “$x = ($x - 1)”

Example:



7 ; “‘x’ is: $x” ; “” ; ; “‘x’ is: $x” ;

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

‘x’ is: 7 ‘x’ is: 8

[ 32 ]

Sep 14, 2012

Agenda RECAP n  COMMENTS n  VARIABLES n  STRING OPERATIONS n  ARITHMETIC n  IF STATEMENTS n  DATA COLLECTION n 

Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 33 ]

Sep 14, 2012

The ‘if’ statement n 

The ‘if’ statement is used to choose between different code (depending on some condition): if ( condition ) { // do something... }

n 

Example:



*if indeed run on a Friday :) Claus Brabrand, ITU, Denmark

SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE

[ 34 ]

Sep 14, 2012

The ‘if-else’ statement n 

Similarly for the ‘if-else’ statement…: if ( condition ) { // do something... } else { // do something else... }

n 

Example: