"> ">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.

Similar presentations


Presentation on theme: "IST 210: PHP LOGIC IST 210: Organization of Data IST210 1."— Presentation transcript:

1 IST 210: PHP LOGIC IST 210: Organization of Data IST210 1

2 Previously IST210 2 <?php $x = rand(1,10); echo "The first random number is $x "; ?> Random number between 1 and 10 Font: color red, size 5 Try it: Create a file lottery0.php

3 Previously IST210 3 Random number between 1 and 10 Font: color red, size 5 Random number between 1 and 10 Font: color green, size 5 <?php $x = rand(1,10); echo "The first random number is $x "; ?>

4 Previously IST210 4 <?php $x = rand(1,10); echo "The first random number is $x "; echo " "; $y = rand(1,10); echo "The second random number is $y "; ?> Random number between 1 and 10 Font: color red, size 5 Random number between 1 and 10 Font: color green, size 5

5 PHP Basics Logical Structures Sequence Selection Loop IST210 5

6 Statements Statement: an instruction written in a high-level language Assignment statement $filename=“abc.txt”; $x = 5+3; $y = (5 == 3); Input/output statement echo “test”; IST210 6

7 Logic Structures Three basic logic structures Sequence Selection Loop IST210 7

8 Sequence The program, when run, must perform each action in order with no possibility of skipping an action or branching off to another action. Sequence: Statement 1 Statement 2 Statement 3 …… IST210 8

9 Sequence To display headings IST210 9 <?php echo " Heading 1 "; echo " Heading 2 "; echo " Heading 3 "; echo " Heading 4 "; echo " Heading 5 "; echo " Heading 6 "; ?>

10 Selection/Decision In a selection structure, a question is asked, and depending on the answer, the program takes one of two courses of action, after which the program moves on to the next event. Selection if expression then statement 1 else statement 2 IST210 10

11 IF/ELSE if – else if (Expression) Statement if (Expression) Statement 1 else Statement 2 IST210 11 <?php $x = 20; if ($x >100) echo "x is a large number."; else echo "x is a small number."; ?> Try it What do you see on your browser? What if you change $x=20; to $x=200; ?

12 IF/ELSE More than one statement: Use braces {} IST210 12 <?php $x = 200; if ($x >100) { echo "x is a large number. "; echo "x is greater than 100"; } else { echo "x is a small number. "; echo "x is smaller than 100"; } ?> Try it

13 IF/ELSE: Exercise IST210 13 <?php $x = 200; $y = 100; if ($x = $y) { echo "x equals to y"; } else { echo "x does not equal to y"; } ?> Debug $x does not equal to y. Where is the bug?

14 A Very Common Mistake A==B: boolean value, true (1) or false (0) A=B: assign variable B’s value to variable A. (The value of the assignment operation is 1.) if (x = 1)  This is always true! then …

15 Loop A loop is a structure that tells a computer to execute a set of statements multiple times. If you have a process that you want repeated hundreds of times, it pays to put it in a loop so you don't need to write hundreds of lines of code. Loops constitute one of the most basic and powerful programming concepts. IST210 15

16 Loop Two most common approaches to do loops 1. while (Expression) Statement; 2. for (Expression1; Expression2; Expression3) Statement; IST210 16 <?php $i=1; while ($i<=6) { echo " Heading $i "; $i++; } ?> Try it While loop

17 Loop Two most common approaches to do loops 1. while (Expression) Statement; 2. for (Expression1; Expression2; Expression3) Statement; IST210 17 <? php for ($i=1; $i<=6; $i++) echo " Heading $i "; ?> Try it For loop

18 For Loop for (Expression1; Expression2; Expression3) Statement; Expression1: Initialization expression executed exactly once -- before the first evaluation of the test expression Expression2: Test expression evaluated each time before the code in the for loop executes Expression3: Increment expression executed after each iteration of the loop, often used to increment the loop variable, which is initialized in the initialization expression and tested in the test expression. IST210 18 for ($i=1; $i<=6; $i++) echo " Heading $i ";

19 Loop A very common mistake in Loop: infinite loop Case 1: Case 2: IST210 19 for ($i=1; $i<=6; $i++) { echo " Heading $i "; $i = 1; } $i=1; while ($i<=6) { echo " Heading $i "; }

20 Combinations of Logic Structure A PHP file usually combines different control structures. IST210 20

21 A LOTTERY GAME Assignment P2 IST210 21

22 Guideline Use the PHP you learnt in class to make a PHP page https://my.up.ist.psu.edu/duw24/lottery.php Game rules: Generate a random number x in [1,3] as the lucky number Generate a random number y in [1,3] as your number If y is the same as x, output “you win the game” y times in different headings; otherwise, output “lose the game” y times in different headings See next two slides for more details IST210 22

23 IST210 23 Heading 1 Font of the number: color red, size 5 Font of the number: color green, size 5 Font: color green in heading 1,2,3 Repeat 3 times (because your number is the same as the lucky number- you win- and 3 is the your random number)

24 IST210 24 Heading 1 Font of the number: color red, size 5 Font of the number: color green, size 5 Font: color red, heading 1,2 Repeat 2 times (because 2 is the your random number)

25 Hint Use rand(1,3) to generate a random number in [1,3] Use two variables, $x and $y, to store the rand number Use IF/ELSE to check whether $x and $y is the same Use FOR loop to echo the results $y times IST210 25

26 More hint IST210 26 <?php $x = rand(1,10); echo "The first random number is $x "; echo " "; $y = rand(1,10); echo "The second random number is $y "; ?> Random number between 1 and 10 Font: color red, size 5 Random number between 1 and 10 Font: color green, size 5

27 Submit Submit to ANGEL in Assignment P2 Your php script (as attachment) Link to the webpage you made (in the submission email) IST210 27


Download ppt "IST 210: PHP LOGIC IST 210: Organization of Data IST210 1."

Similar presentations


Ads by Google