PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types (...

21
PHP Logic

Transcript of PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types (...

PHP Logic

IST210 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!

IST210 3

Functions

<html><body><?php echo "Today is: ".date("m/d/y") ?></body></html>

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

IST210 4

More Functions<html>

<body><?php

echo "Today is: ".date("m/d/y")."<br>";echo "A random number in [1,10]: ".rand(1,10)."<br>";$name = "John";echo "The length of string \" $name \" is ".strlen($name);

?></body>

</html>

More string functionshttp://php.net/manual/en/book.strings.phpMore math functionshttp://php.net/manual/en/ref.math.php

IST210 5

Logic Structures• Three basic logic structures– Sequence– Selection– Loop

IST210 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

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

IST210 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 1Statement 2Statement 3……

IST210 9

Sequence• To display headings

<?phpecho "<h1>Heading

1</h1>";echo "<h2>Heading

2</h2>";echo "<h3>Heading

3</h3>";echo "<h4>Heading

4</h4>";echo "<h5>Heading

5</h5>";echo "<h6>Heading

6</h6>";?>

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

• Selectionif expression then

statement 1else

statement 2

IST210 11

IF/ELSE• if – else

• if (Expression)Statement

• if (Expression)Statement 1

elseStatement 2

<?php$x = 50;if ($x >100)

echo("x is a large number.");Else

echo("x is a small number.");?>

IST210 12

IF/ELSE• More than one statement: Use braces

$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");}

IST210 13

IF/ELSE: Debug

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

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 …

IST210 15

Exercise 1: IF/ELSE• Step 1: Open NotePad++ and create “number.php” in your webspace

• Step 2: Write codes to do the following1. 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.

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

IST210 17

Loop• Three approaches to do loops

1. while (Expression)Statement;

2. doStatement;

while (Expression);3. for (Expression1; Expression2; Expression3)

Statement; $i=1;while ($i<=6) { echo "<h$i> Heading $i</h$i>"; $i++;}

$i=1;do { echo "<h$i> Heading $i</h$i>"; $i++;} while ($i<=6) ;

for ($i=1; $i<=6; $i++) echo "<h$i> Heading $i</h$i>";

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 "<h$i> Heading

$i</h$i>";

IST210 19

Loop

• A very common mistake in Loop: infinite loop– Case 1:

– Case 2:

for ($i=1; $i<=6; $i++){ echo "<h$i> Heading $i</h$i>"; $i = 1;}

$i=1;while ($i<=6) { echo "<h$i> Heading $i</h$i>";}

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

IST210 21

Combinations of Logic Structure• A PHP file usually combines different

control structures.