Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,

Similar presentations


Presentation on theme: "PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,"— Presentation transcript:

1 PHP Logic

2 Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String, Array, Object – PHP variables start with $ – You can use variables without define the type $x = 5; $pi = 3.14; $name = "George"; – Name your variables in meaningful ways $s = "matrix" vs. $servername = “matrix” – Case sensitive! IST2102

3 Functions IST2103 http://my.up.ist.psu.edu/zuz22/date.php

4 More Functions IST2104 <?php echo "Today is: ".date("m/d/y")." "; echo "A random number in [1,10]: ".rand(1,10)." "; $name = "John"; echo "The length of string \" $name \" is ".strlen($name); ?> More string functions http://php.net/manual/en/book.strings.php More math functions http://php.net/manual/en/ref.math.php

5 Logic Structures Three basic logic structures – Sequence – Selection – Loop 5IST210

6 Expressions Any legal combination of symbols that represents a value – AN EXPRESSION ALWAYS CARRIES A VALUE! Every expression consists of at least one operand and can have one or more operators – Operands are values – Operators are symbols that represent particular actions Examples – 5+3 (value is 8) – $x*$y – $x > $y (value is 1 if $x is larger than $y, and 0 otherwise) – $x == $y (value is 1 if $x is equal to $y, and 0 otherwise) – $x <> $y (value is 1 if $x is not equal to $y, and 0 otherwise) More: – http://www.w3schools.com/php/php_operators.asp 6IST210

7 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”; 7IST210

8 Logic Structure 1: 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 …… 8IST210

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

10 Logic Structure 2: 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 10IST210

11 IF/ELSE if – else if (Expression) Statement if (Expression) Statement 1 else Statement 2 11IST210 <?php $x = 50; if ($x >100) echo("x is a large number."); Else echo("x is a small number."); ?>

12 IF/ELSE More than one statement: Use braces 12IST210 $x = 50; 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"); }

13 IF/ELSE: Debug IST21013 <?php $x = 200; $y = 100; if ($x = $y) { echo "x equals to y"; } else { echo "x does not equal to y"; } ?> $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 Exercise 1: IF/ELSE Step 1: Open NotePad++ and create “number.php” in your webspace Step 2: Write codes to do the following 1.Generate a random number x in [1,10] and display it Hint 1: Use rand(1,10) to generate a random number in [1,10] Hint 2: Use variable $x to store the rand number Hint 3: Use echo to output the variables 2.Determine whether x is an odd number or an even number, and display the result Hint: Use the expression $x % 2 to find the remainder of $x divided by 2. IST21015

16 Logic Structure 3: Loop A structure to repeat an action multiple times under a given condition. Loops constitute one of the most basic and powerful programming concepts. 16IST210

17 Loop Three approaches to do loops 1.while (Expression) Statement; 2.do Statement; while (Expression); 3.for (Expression1; Expression2; Expression3) Statement; IST21017 $i=1; while ($i<=6) { echo " Heading $i "; $i++; } $i=1; do { echo " Heading $i "; $i++; } while ($i<=6) ; for ($i=1; $i<=6; $i++) echo " Heading $i ";

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. for ($i=1; $i<=6; $i++) echo " Heading $i ";

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

20 Exercise 2: Loop Use Loop to display “I love Penn State” in increasing font size (from 2 to 6) Font size 2 Font size 6

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


Download ppt "PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,"

Similar presentations


Ads by Google