Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming Methodology and Web Rapid Prototyping (Session 2) TC101, 5 Sessions course, Conducted by Solvith Solvith PHP Course-"— Presentation transcript:

1 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)

2 Lesson Objectives Session 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)

3 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)

4 PHP Syntax Tags  Always start your code with <?php  Ends of your code with ?>  PHP is Case-Sensitive- Captial and Small Capitals matter Instruction Seperation  Each PHP instruction must be ended with a semicolon  To add comments to code use the “//” Solvith PHP Course- James Song (81273798)

5 PHP Syntax Tags  Always start your code with <?php  Ends of your code with ?>  PHP is Case-Sensitive- Captial and Small Capitals matter Instruction 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)

6 PHP Syntax  Which of this is a valid code ? <?php echo “hi brown cow”; //print hi echo “bye cow brown”; ?> Solvith PHP Course- James Song (81273798) <?php echo “hi brown cow” echo “bye cow brown”; ?> <?php echo “hi brown cow”; echo “bye cow brown”; ?> <?php Echo “hi brown cow”; echo “bye cow brown”; ?>

7 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; ?>

8 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; ?>

9 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; ?>

10 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”; ?>

11 Identify the Type Solvith PHP Course- James Song (81273798)  $abc=‘123’;  $abc=123;  $abc=123.01;  $abc=true;

12 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

13 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; ?>

14 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

15 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

16 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

17 Operator -Arithmetic Solvith PHP Course- James Song (81273798) Assume variable A holds 10 and variable B holds 20 then: OperatorDescriptionExample +Adds two operandsA + B will give 30 -Subtracts second operand from the first A - B will give -10 *Multiply both operandsA * 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

18 Operator - Comparison Solvith PHP Course- James Song (81273798) Assume variable A holds 10 and variable B holds 20 then: Operat or DescriptionExample ==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.

19 Operator- Assignment Solvith PHP Course- James Song (81273798) Assume variable A holds 10 and variable B holds 20 then: OperatorDescriptionExample =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 %

20 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

21 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 true elseif statement - is used with the if...else statement to execute a set of code if one of several condition are true switch 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.

22 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

23 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

24 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; }

25 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 ?"; } ?>

26 Control Structure – For loop Solvith PHP Course- James Song (81273798)  Syntax for (initialization; condition; increment) { code to be executed; } Example: for ($i=0;$i<5;$i++) { echo $i; echo ‘,’; } Output: 1,2,3,4,

27 Control Structure – While Solvith PHP Course- James Song (81273798)  Syntax While (Condition) { Code to be excuted; } Example: $i=0; While ($i<5) { $i++; echo $i; } Output: 1,2,3,4,5

28 Control Structure – do while Solvith PHP Course- James Song (81273798)  Syntax do{ 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,

29 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

30 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,

31 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.

32 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:

33 Hands-on – Fortune Cookies Solvith PHP Course- James Song (81273798) DigitFortune Cookie message 1You will have a fine capacity for the enjoyment of life. 2Now is the time to try something new. 3Don't let doubt and suspicion bar your progress. 4 Your principles mean more to you than any money or success. 5Accept the next proposition you hear. 6 A handful of patience is worth more than a bushel of brains. 7You have an active mind and a keen imagination. 8You are talented in many ways. 9Treat everyone as a friend.

34 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= [ (d 1 d 2 d 3 d 4 d 5 d 6 d 7 ) (2 7 6 5 4 3 2 ) ] mod 11 = ( 2d 1 + 7d 2 + 6d 3 + 5d 4 + 4d 5 + 3d 6 + 2d 7 ) mod 11

35 Go through last session lab Solvith PHP Course- James Song (81273798) Go through last session lab

36 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:

37 PHP Functions Solvith PHP Course- James Song (81273798) Syntax: function functionname(inputs) { Code; return output; }

38 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); ?>

39 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 ?>

40 Solvith PHP Course- James Song (81273798)  Encapsulate your prime number function Hands-on – Prime number Function

41 Solvith PHP Course- James Song (81273798)  Reverse numbers Hands-on – Number reversal function


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

Similar presentations


Ads by Google