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++; } while ($x <= 5);
"; $x++; } while ($x <= 5);
"; }
"; }
"; } OUTPUT: Volvo BMW Toyota
"; }
"; } OUTPUT: BMW Toyota Volvo
"; } familyName("Jani"); familyName("Hege"); familyName("Stale"); familyName("Kai Jim"); familyName("Borge"); OUTPUT: Jani Refsnes. Hege Refsnes. Stale Refsnes. Kai Jim Refsnes. Borge Refsnes.
"; } familyName("Hege","1975"); familyName("Stale","1978"); familyName("Kai Jim","1983"); OUTPUT: Hege Refsnes. Born in 1975 Stale Refsnes. Born in 1978 Kai Jim Refsnes. Born in 1983
"; } setHeight(350); setHeight(); setHeight(135); setHeight(80); OUTPUT: The height is : 350 The height is : 50 The height is : 135 The height is : 80
"; echo "7 + 13 = " . sum(7,13) . "
"; echo "2 + 4 = " . sum(2,4); OUTPUT: 5 + 10 = 15 7 + 13 = 20 2 + 4 = 6
"; } fclose($myfile); OUTPUT: AJAX = Asynchronous JavaScript and XML CSS = Cascading Style Sheets HTML = Hyper Text Markup Language PHP = PHP Hypertext Preprocessor SQL = Structured Query Language SVG = Scalable Vector Graphics XML = EXtensible Markup Language
" . $conn->error; } $conn- >close();
Student table entry
"; echo "connected to the table"; } else { echo "error"; } //Inserting the contents echo "
"; //insertion from html $sname=$_POST['sname']; $regno=$_POST['regno']; $m1=$_POST['m1']; $m2=$_POST['m2']; $sql11="insert into student values('$sname',$regno,$m1,$m2)"; if($conn->query($sql11)==TRUE) { echo "inserted"; } else {echo "error";} echo "
"; $sql1="select * from student"; $result = $conn->query($sql1); if ($result->num_rows > 0) { // output data of each row echo "Sname Regno M1 M2
"; while($row = $result->fetch_assoc()) { echo $row["sname"]." ". $row["regno"]." ".$row["m1"]." ".$row ["m2"]."
"; } } else { echo "empty table"; } $conn- >close();