Semis Lecture 2 Flashcards
Very often when you write code, you want to perform different actions for different conditions. You can use ___ in your code to do this.
conditional statements
In PHP we have the following conditional statements: (4)
- if statement
- if…else statement
- if…elseif….else statement
- switch statement
executes some code if one condition is true
if statement
executes some code if a condition is true and another code if that condition is false
if…else statement
executes different codes for more than two conditions
if…elseif….else statement
selects one of many blocks of code to be executed
switch statement
Syntax of the if statement
if (condition) {
code to be executed if condition is true;
}
this code block is an example of:
$t = date(“H”);
if ($t < “20”) {
echo “Have a good day!”;
}
if statement
Syntax of the if… else statement
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
this code block is an example of:
$t = date(“H”);
if ($t < “20”) {
echo “Have a good day!”;
} else {
echo “Have a good night!”;
}
if… else statement
Syntax of the if… elseif… else statement
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}
this code block is an example of:
$t = date(“H”);
echo “<p>The hour (of the server) is “ . $t;
echo “, and will give the following message:</p>”;
if ($t < “10”) {
echo “Have a good morning!”;
} elseif ($t < “20”) {
echo “Have a good day!”;
} else {
echo “Have a good night!”;
}
if… elseif… else statement
Syntax of the switch statement
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
…
default:
code to be executed if n is different from all labels;
}
This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.
Switch statement
this code block is an example of:
$favcolor = “red”;
switch ($favcolor) {
case “red”:
echo “Your favorite color is red!”;
break;
case “blue”:
echo “Your favorite color is blue!”;
break;
case “green”:
echo “Your favorite color is green!”;
break;
default:
echo “Your favorite color is neither red, blue, nor green!”;
}
switch statement
Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal code-lines in a script, we can use ___ to perform a task like this.
loops
In PHP, we have the following looping statements: (4)
- while
- do…while
- for
- foreach
loops through a block of code as long as the specified condition is true
while
loops through a block of code once, and then repeats the loop as long as the specified condition is true
do…while
loops through a block of code a specified number of times
for
loops through a block of code for each element in an array
foreach
Syntax of while loop
while (condition is true) {
code to be executed;
}
$x = 1;
while($x <= 5) {
echo “The number is: $x
<br></br>”;
$x++;
}
The example below first sets a variable $x to 1 ($x = 1). Then, the while loop will continue to run as long as $x is less than, or equal to 5 ($x <= 5). $x will increase by 1 each time the loop runs ($x++)
Syntax of do… while loop
do {
code to be executed; }
while (condition is true);
$x = 1;
do {
echo “The number is: $x <br></br>”;
$x++;
} while ($x <= 5);
The example below first sets a variable $x to 1 ($x = 1). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5
Notice that in a do while loop the condition is tested ___ executing the statements within the loop. This means that the do while loop would execute its statements ___, even if the condition is false the first time.
AFTER;
at least once
The example below sets the $x variable to 6, then it runs the loop, and then the condition is checked:
$x = 6;
do {
echo “The number is: $x <br></br>”;
$x++;
} while ($x <= 5);
Syntax for for Loop
for (init counter; test counter; increment counter) {
code to be executed;
}
Explain the Parameters:
for (init counter; test counter; increment counter) {
code to be executed;
}
- init counter: Initialize the loop counter value
- test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the
loop continues. If it evaluates to FALSE, the loop ends. - increment counter: Increases the loop counter value
example code block for?
for ($x = 0; $x <= 10; $x++) {
echo “The number is: $x <br></br>”;
}
for loop
Syntax for foreach loop
foreach ($array as $value)
{
code to be executed;
}
foreach ($array as $value)
{
code to be executed;
}
For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element
The following example demonstrates a loop that will output the values of the given array ($colors):
$colors = array(“red”, “green”, “blue”, “yellow”);
foreach ($colors as $value){
echo “$value <br></br>”;
}
There are two ways to create indexed arrays:
- The index can be assigned automatically, or
- the index can be assigned manually:
index can be assigned automatically:
(index always starts at 0), like this:
* $cars = array(“Volvo”, “BMW”, “Toyota”);
index can be assigned manually:
$cars[0] = “Volvo”;
$cars[1] = “BMW”;
$cars[2] = “Toyota”;
The following example creates an indexed array named $cars, assigns three elements to it, and then prints a text containing the array values:
$cars = array(“Volvo”, “BMW”, “Toyota”);
echo “I like “ . $cars[0] . “, “ . $cars[1] . “ and “ . $cars[2] . “.”;
The ___ function is used to return the length (the number of elements) of an array:
count()
$cars = array(“Volvo”, “BMW”, “Toyota”);
echo count($cars);
OUTPUT:
3
To loop through and print all the values of an indexed array, you could use a for loop, like this:
$cars = array(“Volvo”, “BMW”, “Toyota”);
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo “<br></br>”;
}
OUTPUT:
Volvo
BMW
Toyota
___ are arrays that use named keys that you assign to them
Associative arrays
There are two ways to create an associative array:
First:
$age = array(“Peter”=>”35”, “Ben”=>”37”, “Joe”=>”43”);
Second:
$age[‘Peter’] = “35”;
$age[‘Ben’] = “37”;
$age[‘Joe’] = “43”;
The named keys in the Associative Array can then be used in a script
$age = array(“Peter”=>”35”, “Ben”=>”37”, “Joe”=>”43”);
echo “Peter is “ . $age[‘Peter’] . “ years old.”;
OUTPUT:
Peter is 35 years old.
To loop through and print all the values of an associative array, you could use a foreach loop, like this:
$age = array(“Peter”=>”35”, “Ben”=>”37”, “Joe”=>”43”);
foreach($age as $x => $x_value) {
echo “Key=” . $x . “, Value=” . $x_value;
echo “<br></br>”;
}
The elements in an array can be sorted in alphabetical or numerical order, descending or ascending
Sorting Arrays
sort arrays in ascending order
sort()
sort arrays in descending order
rsort()
sort associative arrays in ascending order, according to the value
asort()
sort associative arrays in ascending order, according to the key
ksort()
sort associative arrays in descending order, according to the value
arsort()
sort associative arrays in descending order, according to the key
krsort()
Sort Array in Ascending Order - sort()
The following example sorts the elements of the $cars array in ascending
alphabetical order:
$cars = array(“Volvo”, “BMW”, “Toyota”);
sort($cars);
$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo “<br></br>”;
}
OUTPUT:
BMW
Toyota
Volvo
Sort Array in Ascending Order - sort()
The following example sorts the elements of the $numbers array in ascending
numerical order:
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
OUTPUT:
2
4
6
11
22
Sort Array (Ascending Order), According to Value - asort()
The following example sorts an associative array in ascending order, according to the value:
$age = array(“Peter”=>”35”, “Ben”=>”37”, “Joe”=>”43”);
asort($age);
OUTPUT:
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43
Sort Array (Ascending Order), According to Key - ksort()
The following example sorts an associative array in ascending order, according to the key:
$age = array(“Peter”=>”35”, “Ben”=>”37”, “Joe”=>”43”);
ksort($age);
OUTPUT:
Key=Ben, Value=37
Key=Joe, Value=43
Key=Peter, Value=35
Sort Array (Descending Order), According to Value - arsort()
The following example sorts an associative array in descending order, according to the value:
$age = array(“Peter”=>”35”, “Ben”=>”37”, “Joe”=>”43”);
arsort($age);
OUTPUT:
Key=Joe, Value=43
Key=Ben, Value=37
Key=Peter, Value=35
Sort Array (Descending Order), According to Key - krsort()
The following example sorts an associative array in descending order, according to the key:
$age = array(“Peter”=>”35”, “Ben”=>”37”, “Joe”=>”43”);
krsort($age);
OUTPUT:
Key=Peter, Value=35
Key=Joe, Value=43
Key=Ben, Value=37
An array stores multiple values in one single variable.
In the following example $cars is an array. The PHP var_dump() function returns the data type and value:
$cars = array(“Volvo”,”BMW”,”Toyota”);
var_dump($cars);
OUTPUT:
array(3) { [0]=> string(5) “Volvo” [1]=> string(3) “BMW” [2]=> string(6) “Toyota” }
The real power of PHP comes from its ___.
functions
PHP has ___ built-in functions
more than 1000