PSA2 Technical PHP Operators and Control Structure YAP PDF
PSA2 Technical PHP Operators and Control Structure YAP PDF
PRE-SUMMATIVE ASSESSMENT
2
PHP OPERATORS AND CONTROL STRUCTURE
Section: TR21
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this condition is
true;
} else {
code to be executed if all conditions are false;
}
Example
Output "Have a good morning!" if the current time is less than 10, and "Have a
good day!" if the current time is less than 20. Otherwise it will output "Have a
good night!":
<?php
$t = date("H");
Syntax
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;
}
Example
<?php
$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!";
}
?>
PHP Loops
Often when you write code, you want the same block of code to run over
and over again a certain number of times. So, instead of adding several
almost equal code-lines in a script, we can use loops.
Loops are used to execute the same block of code again and again, as long as
a certain condition is true.
The while loop executes a block of code as long as the specified condition is
true.
Syntax
while (condition is true) {
code to be executed;
}
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
Example Explained
• $x = 1; - Initialize the loop counter ($x), and set the start value to 1
• $x <= 5 - Continue the loop as long as $x is less than or equal to 5
• $x++; - Increase the loop counter value by 1 for each iteration
The do...while loop will always execute the block of code once, it will then check
the condition, and repeat the loop while the specified condition is true.
Syntax
do {
code to be executed;
} while (condition is true);
Example
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:
<?php
$x = 1;
The for loop is used when you know in advance how many times the script
should run.
Syntax
for (init counter; test counter; increment counter)
{ code to be executed for each iteration;
}
Parameters:
Example
The example below displays the numbers from 0 to 10:
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
The foreach loop works only on arrays, and is used to loop through each
key/value pair in an array.
Syntax
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.
Example
The example will output the values of the given array ($colors):
<?php
$colors = array("red", "green", "blue", "yellow");
PHP Break
The break statement can also be used to jump out of a loop.
This example jumps out of the loop when x is equal to 4:
Example
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
PHP Continue
The continue statement breaks one iteration (in the loop), if a specified condition
occurs, and continues with the next iteration in the loop.
Example
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
}
?>
1. Using PHP operators create a length conversion page, integrated with HTML
and CSS (note: use formula for each conversion)
Example: 1 meter = 100
A: 93-100
A-: 90-92
B+: 87-89
B: 83-86
B-: 80-82
C+: 77-79
C: 73-76
C-: 70-72
D+: 67-69
D: 63-66
D-: 60-62
F: Below 60
Sample Output
3. Using Looping Statements write a program which will give you all of the
potential combinations of a two-digit decimal combination, printed in a
comma delimited format :
Sample output :
00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
Snip and paste your source codes here. Snip it directly from the IDE so that colors of the codes are
preserved for readability. Include additional pages if necessary.
1. What are the different Arithmetic operators used in the lab activity? describe each
In this lab activity, I mostly used multiplication and division. I used them both in the first activity
when I’m converting the lengths into different metrics.
VIII. REFERENCES
1. https://www.w3schools.com/css/
2. https://www.w3schools.com/html/
3. https://www.w3schools.com/php/php_variables.asp
4. https://www.w3resource.com/php/operators/arithmetic-operators.php
5. https://www.tutorialspoint.com/php/php_arithmatic_operators_examples.htm
6. https://www.math10.com/en/algebra/convenrsion-factors-length-area-volume-mass-speed-
energy-power-force.html
7. https://www.w3schools.com/php/php_if_else.asp
8. https://www.w3schools.com/php/php_switch.asp
9. https://www.foxinfotech.in/2019/01/php-form-example-student-grading-system.html
10. https://www.w3schools.com/php/php_looping.asp
11. https://www.w3schools.com/php/php_looping_while.asp
12. https://www.w3schools.com/php/php_looping_do_while.asp
13. https://www.w3schools.com/php/php_looping_for.asp
14. https://www.w3schools.com/php/php_looping_foreach.asp
15. https://www.w3schools.com/php/php_looping_break.asp