Top Banner
Programming Methodology and Web Rapid Prototyping (Session 2) TC101 , 5 Sessions course, Conducted by Solvith http://solvith.com/ACJC Solvith PHP Course- James Song (81273798)
41

Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith Solvith PHP Course-

Dec 27, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Programming Methodology and Web Rapid Prototyping (Session 2)

TC101 , 5 Sessions course, Conducted by Solvith

http://solvith.com/ACJC

Solvith PHP Course- James Song (81273798)

Page 2: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Lesson ObjectivesSession 2 [13 Jul] (Changed*)1. Continuation last week (2.5 hour)2. Go through previous lab (30 mins)3. PHP Modular programming . (1 hour)

Functions

*Students should come with team formation (5 members)

*Issuing out of Projects Questions

Solvith PHP Course- James Song (81273798)

Page 3: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Course Grading Criteria- Final

2 x Labs (25 May, 27 July)

40%

Final Test(24 Aug*) 30%

Project (24 Aug*) 30%

Solvith PHP Course- James Song (81273798)

Page 4: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

PHP SyntaxTags Always start your code with <?php Ends of your code with ?> PHP is Case-Sensitive- Captial and Small

Capitals matterInstruction Seperation Each PHP instruction must be ended with a

semicolon To add comments to code use the “//”

Solvith PHP Course- James Song (81273798)

Page 5: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

PHP SyntaxTags Always start your code with <?php Ends of your code with ?> PHP is Case-Sensitive- Captial and Small

Capitals matterInstruction Seperation Each PHP instruction must be ended with a

semicolon PHP is line-blind . Ie: don’t need to end each

code in a line To add comments to code use the “//”

Solvith PHP Course- James Song (81273798)

Page 6: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

PHP Syntax Which of this is a valid code ?

<?phpecho “hi brown cow”; //print hiecho “bye cow brown”;?>

Solvith PHP Course- James Song (81273798)

<?phpecho “hi brown cow”echo “bye cow brown”;?>

<?phpecho “hi brown cow”; echo “bye cow brown”;?>

<?phpEcho “hi brown cow”; echo “bye cow brown”;?>

Page 7: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Variable Types

Solvith PHP Course- James Song (81273798)

Booleans This is the simplest type. A boolean expresses

a truth value. It can be either True or False.

<?php$name=“John”;$gender_ismale = True;?>

Page 8: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Variable Types

Solvith PHP Course- James Song (81273798)

Integers An integer is a number of the set ℤ = {..., -2, -

1, 0, 1, 2, ...}. Largest Supported: 2147483647 Minimum Supported: -2147483648 Smallest Supported :

<?php$name=“John”;$gender_ismale = True;?>

Page 9: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Variable Types

Solvith PHP Course- James Song (81273798)

Floating Point Numbers Also known as floats , doubles or real numbers Basically number with decimal places. Maximum of 1.8 x 10^308 , -1.8 x 10^308

<?php$money=12345.67;?>

Page 10: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Variable Types

Solvith PHP Course- James Song (81273798)

String A string is series of characters, where a

character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support.

You can use single quotes or double quote<?php$a=“There is chemistry tomorrow”;$b=‘There is Physics tomorrow”;?>

Page 11: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Identify the Type

Solvith PHP Course- James Song (81273798)

$abc=‘123’;

$abc=123;

$abc=123.01;

$abc=true;

Page 12: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Casting Between Types

Solvith PHP Course- James Song (81273798)

Do type casting. String to float<?php$Abc=‘123.10’;$Number=floatval($Abc);?>String to Integer<?php$Abc=‘123.10’;$Number=intval($Abc);?>Why is there a difference in output ??

$number=123

$number=123.10

Page 13: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Casting Between Types

Solvith PHP Course- James Song (81273798)

Do type casting. Float to Int<?php$Abc=123.10;$abc_int=(int)$Abc;?>Int to Float<?php$Abc=123;$abc_int=(float)$Abc;?>

Page 14: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Casting Between Types

Solvith PHP Course- James Song (81273798)

Do type casting. Int to Boolean<?php$yes=1;$yesorno=(bool)$yes;?>

Any number except zero is considered true. Zero is considered false

Page 15: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Operator

Solvith PHP Course- James Song (81273798)

Operator is something that takes in certain operand(Inputs) and return and output.

PHP supports: Arithmetic Operators Comparision Operators Logical (or Relational) Operators Assignment Operators Conditional (or ternary) Operators

Page 16: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Operator

Solvith PHP Course- James Song (81273798)

Operator is something that takes in certain operand(Inputs) and return and output or do something.

PHP supports: Arithmetic Operators Comparision Operators Logical (or Relational) Operators Assignment Operators Conditional (or ternary) Operators

Page 17: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Operator -Arithmetic

Solvith PHP Course- James Song (81273798)

Assume variable A holds 10 and variable B holds 20 then:Operator Description Example

+ Adds two operands A + B will give 30

- Subtracts second operand from the first

A - B will give -10

* Multiply both operands A * B will give 200

/ Divide numerator by denumerator

B / A will give 2

% Modulus Operator and remainder of after an integer

division

B % A will give 0

++ Increment operator, increases integer value by one

A++ will give 11

-- Decrement operator, decreases integer value by one

A-- will give 9

Page 18: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Operator - Comparison

Solvith PHP Course- James Song (81273798)

Assume variable A holds 10 and variable B holds 20 then:Operator

Description Example

== Checks if the value of two operands are equal or not, if yes then condition becomes true.

(A == B) is not true.

!= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.

(A != B) is true.

> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.

(A > B) is not true.

< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.

(A < B) is true.

>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

(A >= B) is not true.

<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

(A <= B) is true.

Page 19: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Operator- Assignment

Solvith PHP Course- James Song (81273798)

Assume variable A holds 10 and variable B holds 20 then:Operator Description Example

= Simple assignment operator, Assigns values from right side operands to left

side operand

C = A + B will assigne value of A + B into C

+= Add AND assignment operator, It adds right operand to the left operand and

assign the result to left operand

C += A is equivalent to C = C + A

-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left

operand

C -= A is equivalent to C = C - A

*= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left

operand

C *= A is equivalent to C = C * A

/= Divide AND assignment operator, It divides left operand with the right

operand and assign the result to left operand

C /= A is equivalent to C = C / A

%= Modulus AND assignment operator, It takes modulus using two operands and

assign the result to left operand

C %= A is equivalent to C = C %

Page 20: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Expression

Solvith PHP Course- James Song (81273798)

Do type casting. Int to Boolean

<?php$yes=1;$yesorno=(bool)$yes;?>

Any number except zero is considered true. Zero is considered false

Page 21: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Control Structure

Solvith PHP Course- James Song (81273798)

The if, elseif ...else and switch statements are used to take decision based on the different condition.

You can use conditional statements in your code to make your decisions. PHP supports following three decision making statements:if...else statement - use this statement if you want to execute a set of code when a condition is true and another if the condition is not trueelseif statement - is used with the if...else statement to execute a set of code if one of several condition are trueswitch statement - is used if you want to select one of many blocks of code to be executed, use the Switch statement. The switch statement is used to avoid long blocks of if..elseif..else code.

Page 22: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Control Structure – If Else

Solvith PHP Course- James Song (81273798)

Syntax:

if (condition) {code to be executed if condition is true;}Else{code to be executed if condition is false;}

• Else portion is optional

Page 23: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Control Structure – If Else

Solvith PHP Course- James Song (81273798)

Example:

$d=date(“D”); //get today dates

If ($d==‘Fri’){ echo “Have a nice weekend”;}Else{ echo “Have a Nice Day”;}

• Else portion is optional

Page 24: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Control Structure – Switch

Solvith PHP Course- James Song (81273798)

If you want to select one of many blocks of code to be executed, use the Switch statement.

The switch statement is used to avoid long blocks of if..elseif..else code.

Syntax:switch (expression) {

case label1: code to be executed if expression = label1; break;

case label2: code to be executed if expression = label2; break;

default: code to be executed if expression is different from both label1 and label2; }

Page 25: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Control Structure – Switch

Solvith PHP Course- James Song (81273798)

Example:

<?php$d=date("D");switch ($d){case "Mon": echo "Today is Monday"; break;case "Tue": echo "Today is Tuesday"; break;case "Wed": echo "Today is Wednesday"; break;case "Thu": echo "Today is Thursday"; break;case "Fri": echo "Today is Friday"; break;case "Sat": echo "Today is Saturday"; break;case "Sun": echo "Today is Sunday"; break;default: echo "Wonder which day is this ?";}?>

Page 26: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Control Structure – For loop

Solvith PHP Course- James Song (81273798)

Syntaxfor (initialization; condition; increment) { code to be executed; }

Example:for ($i=0;$i<5;$i++){

echo $i;echo ‘,’;

}

Output: 1,2,3,4,

Page 27: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Control Structure – While

Solvith PHP Course- James Song (81273798)

SyntaxWhile (Condition){Code to be excuted;}

Example:$i=0;While ($i<5){$i++;echo $i;}

Output: 1,2,3,4,5

Page 28: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Control Structure – do while

Solvith PHP Course- James Song (81273798)

Syntaxdo{Code to be executed;}while(condition);

Remember the semicolon after the while

Example:$i=0;do{echo $i;echo ‘,’;$i++;}while ($i<5);

Output: 0,1,2,3,4,

Page 29: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Control Structure – Break and Continue

Solvith PHP Course- James Song (81273798)

The PHP break keyword is used to terminate the execution of a loop prematurely.

The break statement is situated inside the statement block. If gives you full control and whenever you want to exit from the loop you can come out. After coming out of a loop immediate statement to the loop will be executed.

for ($i=0;$i<5;$i++){

echo $i;If ($i==3)

break;echo ‘,’;

}

Output: 0,1,2,3

Page 30: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Control Structure – Break and Continue

Solvith PHP Course- James Song (81273798)

The PHP continue keyword is used to halt the current iteration of a loop but it does not terminate the loop.

Just like the break statement the continue statement is situated inside the statement block containing the code that the loop executes, preceded by a conditional test. For the pass encountering continue statement, rest of the loop code is skipped and next pass starts.

for ($i=0;$i<5;$i++){

If ($i==3) continue;echo $i;echo ‘,’;

}

Output: 0,1,2,4,

Page 31: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Hands-on – Leap-Year

Solvith PHP Course- James Song (81273798)

Generate the last 127 leap year dates.

Leap-Year Criteria

• In the Gregorian calendar 3 criteria must be taken into account to identify leap years:

• The year is evenly divisible by 4;• If the year can be evenly divided by 100, it is NOT a leap

year, unless;• The year is also evenly divisible by 400. Then it is a leap

year.

Page 32: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Hands-on – Fortune Cookies

Solvith PHP Course- James Song (81273798)

• Write a program cookies.c to read in a positive integer and add up its digits repeatedly until the sum is a single digit. For example, if the integer is 12345, then adding its digits (1 + 2 + 3 + 4 + 5) yields 15, and adding its digits again (1 + 5) yields 6. Hence the answer is 6.

• Using this single digit result, print out the corresponding Fortune Cookie message according to the table below:

Page 33: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Hands-on – Fortune Cookies

Solvith PHP Course- James Song (81273798)

Digit Fortune Cookie message

1 You will have a fine capacity for the enjoyment of life.

2 Now is the time to try something new.

3 Don't let doubt and suspicion bar your progress.

4 Your principles mean more to you than any money or success.

5 Accept the next proposition you hear.

6 A handful of patience is worth more than a bushel of brains.

7 You have an active mind and a keen imagination.

8 You are talented in many ways.9 Treat everyone as a friend.

Page 34: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Hands-on – Random NRIC Generator

Solvith PHP Course- James Song (81273798)

• Write a program that can generate random NRIC number of Singaporeans including the alphabetical checksum.

• Generate 100 of them.

d = [(d1 d2 d3 d4 d5 d6 d7) • (2 7 6 5 4 3 2 )] mod 11

= ( 2d1 + 7d2 + 6d3 + 5d4 + 4d5 + 3d6 +

2d7 ) mod 11

d 10 9 8 7 6 5 4 3 2 1 0Check digit A B C D E F G H I Z J

Page 35: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Go through last session lab

Solvith PHP Course- James Song (81273798)

Go through last session lab

Page 36: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

PHP Functions

Solvith PHP Course- James Song (81273798)

PHP functions are similar to other programming languages. A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value.

Its very easy to create your own PHP function. Suppose you want to create a PHP function which will simply write a simple message on your browser when you will call it. Following example creates a function called writeMessage() and then calls it just after creating it.

Note that while creating a function its name should start with keyword function and all the PHP code should be put inside { and } braces as shown in the following example below:

Page 37: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

PHP Functions

Solvith PHP Course- James Song (81273798)

Syntax:function functionname(inputs){Code;return output;}

Page 38: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

PHP Functions with parameters

Solvith PHP Course- James Song (81273798)

PHP gives you option to pass your parameters inside a function. You can pass as many as parameters your like. These parameters work like variables inside your function. Following example takes two integer parameters and add them together and then print them.

<?php function addFunction($num1, $num2) { $sum = $num1 + $num2; echo "Sum of the two numbers is : $sum"; } addFunction(10, 20); ?>

Page 39: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

PHP Functions with Return Value

Solvith PHP Course- James Song (81273798)

A function can return a value using the return statement in conjunction with a value or object. return stops the execution of the function and sends the value back to the calling code.

You can return more than one value from a function using return array(1,2,3,4).

Following example takes two integer parameters and add them together and then returns their sum to the calling program. Note that return keyword is used to return a value from a function.

<?php function addFunction($num1, $num2) { $sum = $num1 + $num2; return $sum;} $return_value = addFunction(10, 20); echo "Returned value from the function : $return_value ?>

Page 40: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Solvith PHP Course- James Song (81273798)

Encapsulate your prime number functionHands-on – Prime number Function

Page 41: Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith  Solvith PHP Course-

Solvith PHP Course- James Song (81273798)

Reverse numbersHands-on – Number reversal function