1 / 41

Programming Methodology and Web Rapid Prototyping (Session 2)

Programming Methodology and Web Rapid Prototyping (Session 2). TC101 , 5 Sessions course, Conducted by Solvith http://solvith.com/ACJC. Lesson Objectives. Session 2 [13 Jul ] (Changed*) Continuation last week (2.5 hour) Go through previous lab (30 mins )

vic
Download Presentation

Programming Methodology and Web Rapid Prototyping (Session 2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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*) • Continuation last week (2.5 hour) • Go through previous lab (30 mins) • 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 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” echo “bye cow brown”; ?> <?php echo “hi brown cow”; //print hi echo “bye cow brown”; ?> <?php echo “hi brown cow”;echo “bye cow brown”; ?> <?php Echo “hi brown cow”;echo “bye cow brown”; ?> Solvith PHP Course- James Song (81273798)

  7. Variable Types 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; ?> Solvith PHP Course- James Song (81273798)

  8. Variable Types 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; ?> Solvith PHP Course- James Song (81273798)

  9. Variable Types 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; ?> Solvith PHP Course- James Song (81273798)

  10. Variable Types 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”; ?> Solvith PHP Course- James Song (81273798)

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

  12. Casting Between Types • 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.10 $number=123 Solvith PHP Course- James Song (81273798)

  13. Casting Between Types • Do type casting. Float to Int <?php $Abc=123.10; $abc_int=(int)$Abc; ?> Int to Float <?php $Abc=123; $abc_int=(float)$Abc; ?> Solvith PHP Course- James Song (81273798)

  14. Casting Between Types • Do type casting. Int to Boolean <?php $yes=1; $yesorno=(bool)$yes; ?> Any number except zero is considered true. Zero is considered false Solvith PHP Course- James Song (81273798)

  15. Operator • 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 Solvith PHP Course- James Song (81273798)

  16. Operator • 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 Solvith PHP Course- James Song (81273798)

  17. Operator -Arithmetic Assume variable A holds 10 and variable B holds 20 then: Solvith PHP Course- James Song (81273798)

  18. Operator - Comparison Assume variable A holds 10 and variable B holds 20 then: Solvith PHP Course- James Song (81273798)

  19. Operator- Assignment Assume variable A holds 10 and variable B holds 20 then: Solvith PHP Course- James Song (81273798)

  20. Expression • Do type casting. Int to Boolean <?php $yes=1; $yesorno=(bool)$yes; ?> Any number except zero is considered true. Zero is considered false Solvith PHP Course- James Song (81273798)

  21. Control Structure • 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. Solvith PHP Course- James Song (81273798)

  22. Control Structure – If Else Syntax: • Else portion is optional Solvith PHP Course- James Song (81273798)

  23. Control Structure – If Else Example: • Else portion is optional Solvith PHP Course- James Song (81273798)

  24. Control Structure – Switch • 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: Solvith PHP Course- James Song (81273798)

  25. Control Structure – Switch Example: Solvith PHP Course- James Song (81273798)

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

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

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

  29. Control Structure – Break and Continue • 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 Solvith PHP Course- James Song (81273798)

  30. Control Structure – Break and Continue • 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, Solvith PHP Course- James Song (81273798)

  31. Hands-on – Leap-Year • 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. Solvith PHP Course- James Song (81273798)

  32. Hands-on – Fortune Cookies • 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: Solvith PHP Course- James Song (81273798)

  33. Hands-on – Fortune Cookies Solvith PHP Course- James Song (81273798)

  34. Hands-on – Random NRIC Generator • Write a programthat 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 Solvith PHP Course- James Song (81273798)

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

  36. PHP Functions • 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: Solvith PHP Course- James Song (81273798)

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

  38. PHP Functions with parameters 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 functionaddFunction($num1, $num2) { $sum = $num1 + $num2; echo "Sum of the two numbers is : $sum"; } addFunction(10, 20); ?> Solvith PHP Course- James Song (81273798)

  39. PHP Functions with Return Value • 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 functionaddFunction($num1, $num2) { $sum = $num1 + $num2; return$sum;} $return_value = addFunction(10, 20); echo "Returned value from the function : $return_value ?> Solvith PHP Course- James Song (81273798)

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

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

More Related