PHP: 02 - Control Structures

Control Structures

 

Operators

==, <=, >=, <>

 

Ternary

$result = (exp1) ? (expr2) :(expr3);

if exp1 is true, use expr2

otherwise, use expr3;

If, elseif, else

 

if

if ($a > $b){

}

elseif

if ($a > $b){

} elseif ($a > $c){

}

else

if ($a > $b){

} elseif ($a > $c){

}else{

}

 

Logical Operators

 

AND, &&

if (($a > $b) && ($c > $d))

OR, ||

if (($a > $b) && ($c > $d))

 

 

Switch

 

switch($a){

case 0:

echo: "a equals 0";

break;

case 1:

echo: "a equals 1";

break;

default;

echo: "a not known";

break;

}

 

 

While Loop

count = 0;

while ($count <= 10){

echo $count;

$count ++;

}

For Loop

for (init; test; each){

statement;

}

 

for($count=0; $count<=10; $count++){

}

Foreach Loop

 

Arrays

foreach ($array as $value){

}

 

 

$keys = array(1,2,3,4,5);

foreach ($keys as $key){

echo $key

}

 

Associative Arrays

 

foreach ($array as $key => $value)

Statement;

 

Example1

// using each key => value pair

// good for when you need the index with the value in the foreach

$ages = array(1,2,3,4,5);

 

foreach ($ages as $position => $age){

echo $position . ": " . $age . "<br />";

}

 

0: 1

1: 2

2: 3

3: 4

4: 5

 

 

Example2

$ages = array(

"John" => 1,

"Jane" => 2,

"Kelly" => 3

);

 

foreach ($ages as $name => $age){

echo $name. ": " . $age . "<br />";

}

 

John: 1

Jane: 2

Kelly: 3

 

 

Example3

$prices = array(

"New Computer"=>2000,

"Training"=>25,

"Learning PHP"=>"priceless"

);

 

foreach($prices as $key->$value) {

if(is_int($value)){

echo $key . ": $" . $value . "<br />";

} else {

echo $key . ": " . $value . "<br />";

}

}

 

New Computer: $2000

Training: $25

Learning PHP: priceless

 

 

 

Continue

Once condition is met, continue with the loop

 

Continue;

 

Break

Once condition is met, break out of the current loop

 

Break;

 

 

for ($count=0; $count<=10; $count++){

echo $count . ", ";

}

 

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

 

for ($count=0; $count<=10; $count++){

echo $count;

if ($count < 10) {echo ", ";}

}

 

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 

 

for ($count=0; $count<=10; $count++){

echo $count;

if ($count == 10) {break;}

echo ", ";

}

 

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 

 

 

 

Pointers

 

current($array1)

current pointer

next($array1)

move pointer next

reset($array1)

set pointer to start

 

 

// Arrays have pointers that point to a position in the array

// We can use current, next and reset to manipulate the pointer

echo "1: " . current($ages) . "<br />";

next($ages);

echo "2: " . current($ages) . "<br />";

reset($ages);

echo "3: " . current($ages) . "<br />";

 

1: 4
2: 8
3: 4

 

 

// while loop that moves the array pointer

// It is important to understand this type of loop before working with databases

// $age is a POINTER, not a VARIABLE

// returns TRUE if $age was able to be assigned

// (pointer $current able to move next()

while ($age = current($ages)) {

echo $age . ", ";

next($ages); //move pointer to next position in array

}

 

4, 8, 15, 16, 23, 42,

Tags