web app development_php_05

36
[email protected] July 07, 2013 Hassen poreya Trainer, Cresco Solution Afghanistan Workforce Development Program PHP Operators, Expressions, Control Statement, Loops

Upload: hassen-poreya

Post on 07-Nov-2014

393 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Web app development_php_05

[email protected] July 07, 2013 Hassen poreya Trainer, Cresco Solution

Afghanistan Workforce Development Program

PHP Operators, Expressions, Control Statement, Loops

Page 2: Web app development_php_05

Operators

A symbol that tells PHP to perform a mathematical or logical operation.

Arithmetic

Logical

Bitwise

Miscellaneous

Page 3: Web app development_php_05

Arithmetic Operators

e.g, 27 / 3 15 % 4 23 % 6

Page 4: Web app development_php_05

Using a string with an Arithmetic Operator

<?php

$foo = "0"; // $foo is string

$foo = $foo + 2; // $foo is now an integer: 2

$foo = $foo + 1.3; // $foo is now a float: 3.3

$foo = 5 + "10 Books"; // $foo is integer: 15

?>

Page 5: Web app development_php_05

Using a string with an Arithmetic Operator

++ increment

-- decrement

If an increment operator precedes the variable, the variable will be incremented prior to evaluation of the expression; otherwise, the variable isn’t operated on until after the value of the expression is computed.

<?php

$count=1;

print($count++); //prints 1, but after print,

count is 2

print(++$count); //prints 3

?>

Page 6: Web app development_php_05

Logical and Relational Operators

Relational operators compare values and return either TRUE or FALSE.

Logical operators perform logical operations on TRUE and FALSE

Page 7: Web app development_php_05

The truth table for operators

Page 8: Web app development_php_05

Bitwise Operator

A bit is either 0 or 1

Bitwise operators view numbers from a binary perspective.

Operator Operation Performed

& And

| Or

Page 9: Web app development_php_05

Bitwise Operator

<?php

$x=14;

$y=7;

$z=$x & $y;

echo “The bitwise (AND) of 14 and 7

is: ”.$z;

?>

Page 10: Web app development_php_05

Other Operators

Page 11: Web app development_php_05

Concatenation Operator

Connect two separated values together.

<?php

$result = “Passed”;

$string = “The exam result of yours

is:”.$result;

print($string);

?>

Page 12: Web app development_php_05

Variable Variables with $ Operator

<?php

//set variables

$var_name = "myValue";

$myValue = 123.456;

print($$var_name . "<br />");

//prints "123.456“

?>

Page 13: Web app development_php_05

Assignment Operators

Page 14: Web app development_php_05

Precedence of Operators

<?php

print((3+2*7) . “<br>”);

//first multiplication

print(((3+2)*7) . “<br>”); //first

addition between parenthesis

?>

Page 15: Web app development_php_05

Expressions

Page 16: Web app development_php_05

Expressions

A bit of PHP that can be evaluated to produce a value.

PHP is an expression-oriented language

Everything is an expression

Expressions are combinations of identifiers and operators.

Page 17: Web app development_php_05

Expressions

Generally executed from left to right

Some operators are processed before others.

Use parentheses to force an operation to occur first

Two general rules

Some operators work only on certain data types.

If the operation is on a mix of an integer and a double, the integer will be converted to a double.

Page 18: Web app development_php_05

Expressions

PHP will even read doubles with an exponent. <?php

print((1 + "1") . "<BR>");

print((1 + " 2") . "<BR>");

print((1 + "3extra stuff") . "<BR>");

print((1 + "4.5e6") . "<BR>");

print((1 + "a7") . "<BR>");

?>

Page 19: Web app development_php_05

Evaluation

PHP will even read doubles with an exponent. <?php

print((1 + "1") . "<BR>"); //1 + 1 == 2

print((1 + " 2") . "<BR>"); //1 + 2 == 3

print((1 + "3extra stuff") . "<BR>"); //1 + 3 == 4

print((1 + "4.5e6") . "<BR>"); //1 + 4500000 == 4500001

print((1 + "a7") . "<BR>"); //1 + 0 == 1

//“a7” begins with a letter, PHP treats it as zero!

?>

Page 20: Web app development_php_05

Control Statement

Page 21: Web app development_php_05

TRUE and FALSE

Zero (0 and 0.0) and an empty string ("") are considered to be FALSE.

Any other numerical value or string is TRUE.

Some control statements expect a Boolean value.

Page 22: Web app development_php_05

The if Statement

If(condition/expression){

statements…

}

You are not forced to put an elseif or an else statement after.

<?php

if(($day == “Friday") or ($day == “Saturday"))

{

print(“University is closed");

}

?>

Page 23: Web app development_php_05

If, ifelse, else Statements

<?php

if ($month == "January") {

$num_month = 1;

}

elseif($month == "February")

$num_month = 2;

// March to October omitted here

elseif($month == "November")

$num_month = 11;

else

$num_month = 12;

?>

Page 24: Web app development_php_05

switch Statements

Switch(expression){

case case-expression

expression;

break;

case case-expression

expression;

break

default

expression;

}

The expression inside the switch statement is evaluated and then compared to each expression following a case expression.

A default statement works exactly like an else statement; it matches when no other case matches.

Break statement is used escape from the switch statement.

Syntax Description

Page 25: Web app development_php_05

switch Statement

<?php

switch($month) {

case "January":

$num_month = 1;

break;

// February to October omitted here

case "November":

$num_month = 11;

break;

default: // It must be December

$num_month = 12;

}

?>

Page 26: Web app development_php_05

Loops

Loops allow you to repeat lines of code based on some condition.

Example

Read lines from a file until the end is reached.

Print some text a certain number of times.

Page 27: Web app development_php_05

while Loop Statement

While (condition/expression){

statement;

}

It is useful when you are not sure how many times you will need to iterate your statements.

Page 28: Web app development_php_05

while Loop Statement

<?php

$total = 0;

$i = 1;

while ($i <= 10){

$total += $i++;

}

echo "The sum of 1 to 10 is: " .

$total;

?>

Page 29: Web app development_php_05

do while statement

do{

statement;

}

while(condition/expression)

Similar to while loop.

First the statement is executed, and then acts according to the condition.

Page 30: Web app development_php_05

do while Statement

<?php

$total = 0;

$i = 1;

do {

$total += $i++;

}

while ($i <= 10);

echo "The sum of 1 to 10 is : "

. $total;

?>

Page 31: Web app development_php_05

break Statement

When a break statement is encountered, the execution jumps to the outside of the loop or switch statement.

<?php

while (true){

echo “This line is printed here!”;

break;

echo “This line would never be printed”;

}

//These codes will be executed!

?>

Page 32: Web app development_php_05

break Statement

<?php

$i=0; $j=0;

while ($i<10){

while($j<5){

if($j==5)

break; // the loop is stopped

$j++; // once $j reaches 5

}

$i++;

}

echo „$i=‟.$i“<br>”;

echo „$j=‟.$i;

?>

Page 33: Web app development_php_05

continue Statement

Similar to the break statement

But only the current execution of the loop is stopped, after that loop is continued.

<?php

while ($i++<5){

if($i==2)

continue;

echo $i; //2 will not be printed.

}

?>

Page 34: Web app development_php_05

Let’s code something

Page 35: Web app development_php_05

Class exercise

Write a while loop that outputs the even numbers between 1 and 100.

Rewrite the script for Problem 1 using do while loop.

Write a while loop that outputs the numbers from 100 to 0 by 10's.

Page 36: Web app development_php_05

[email protected] July 07, 2013 Hassen poreya Trainer, Cresco Solution

Any Questions!