web app development_php_05

Post on 07-Nov-2014

393 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

hasen@microcis.net July 07, 2013 Hassen poreya Trainer, Cresco Solution

Afghanistan Workforce Development Program

PHP Operators, Expressions, Control Statement, Loops

Operators

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

Arithmetic

Logical

Bitwise

Miscellaneous

Arithmetic Operators

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

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

?>

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

?>

Logical and Relational Operators

Relational operators compare values and return either TRUE or FALSE.

Logical operators perform logical operations on TRUE and FALSE

The truth table for operators

Bitwise Operator

A bit is either 0 or 1

Bitwise operators view numbers from a binary perspective.

Operator Operation Performed

& And

| Or

Bitwise Operator

<?php

$x=14;

$y=7;

$z=$x & $y;

echo “The bitwise (AND) of 14 and 7

is: ”.$z;

?>

Other Operators

Concatenation Operator

Connect two separated values together.

<?php

$result = “Passed”;

$string = “The exam result of yours

is:”.$result;

print($string);

?>

Variable Variables with $ Operator

<?php

//set variables

$var_name = "myValue";

$myValue = 123.456;

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

//prints "123.456“

?>

Assignment Operators

Precedence of Operators

<?php

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

//first multiplication

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

addition between parenthesis

?>

Expressions

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.

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.

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

?>

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!

?>

Control Statement

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.

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

}

?>

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;

?>

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

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;

}

?>

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.

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.

while Loop Statement

<?php

$total = 0;

$i = 1;

while ($i <= 10){

$total += $i++;

}

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

$total;

?>

do while statement

do{

statement;

}

while(condition/expression)

Similar to while loop.

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

do while Statement

<?php

$total = 0;

$i = 1;

do {

$total += $i++;

}

while ($i <= 10);

echo "The sum of 1 to 10 is : "

. $total;

?>

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!

?>

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;

?>

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.

}

?>

Let’s code something

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.

hasen@microcis.net July 07, 2013 Hassen poreya Trainer, Cresco Solution

Any Questions!

top related