JS Lab 3
This commit is contained in:
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
|
||||
// -----------------------------
|
||||
// IF statement
|
||||
// -----------------------------
|
||||
|
||||
if ($condition) {
|
||||
// code.
|
||||
}
|
||||
|
||||
// E.g.
|
||||
if ($year === 2024) {
|
||||
$isOlympicYear = TRUE;
|
||||
}
|
||||
|
||||
// Bonus: IF statements with a lot of conditions
|
||||
if (
|
||||
$condition_1 &&
|
||||
(
|
||||
$condition_2 ||
|
||||
$condition_3
|
||||
) &&
|
||||
$condition_4
|
||||
) {
|
||||
// code.
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// IF/ELSE statement
|
||||
// -----------------------------
|
||||
|
||||
if ($condition) {
|
||||
// code.
|
||||
}
|
||||
else {
|
||||
// other code.
|
||||
}
|
||||
|
||||
// E.g.
|
||||
if ($year === 2024) {
|
||||
$isOlympicYear = TRUE;
|
||||
}
|
||||
else {
|
||||
$isOlympicYear = FALSE;
|
||||
}
|
||||
|
||||
// Alternatively, you could write the above like this.
|
||||
$isOlympicYear = FALSE;
|
||||
if ($year === 2024) {
|
||||
$isOlympicYear = TRUE;
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// ELSEIF statements.
|
||||
// -----------------------------
|
||||
|
||||
if ($condition_1) {
|
||||
// code.
|
||||
}
|
||||
elseif ($condition_2) {
|
||||
// other code.
|
||||
}
|
||||
else {
|
||||
// other other code.
|
||||
}
|
||||
|
||||
// E.g.
|
||||
if ($year === 2024) {
|
||||
$isOlympicYear = TRUE;
|
||||
}
|
||||
elseif ($year === 2025) {
|
||||
$isOlympicYear = FALSE;
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Nesting statements
|
||||
// -----------------------------
|
||||
|
||||
// E.g.
|
||||
if ($year === 2024) {
|
||||
$isOlympicYear = TRUE;
|
||||
|
||||
if ($city === 'Paris') {
|
||||
echo "Bonjour!";
|
||||
}
|
||||
else {
|
||||
echo "Hello!";
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Early Return statements
|
||||
// -----------------------------
|
||||
|
||||
function checkAge($age) {
|
||||
// Early return if age is less than 18
|
||||
if ($age < 18) {
|
||||
return "Too young";
|
||||
}
|
||||
// Continue with the function if age is 18+
|
||||
return "Welcome!";
|
||||
}
|
||||
|
||||
echo checkAge(16); // Outputs: Too young
|
||||
echo checkAge(20); // Outputs: Welcome!
|
||||
|
||||
// -----------------------------
|
||||
// Ternary Expressions
|
||||
// - Only good for replacing non-complex IF/ELSE statements
|
||||
// -----------------------------
|
||||
|
||||
$age = 20;
|
||||
$can_vote = ($age >= 18) ? 'Yes' : 'No'
|
||||
|
||||
// Alternatively, you could write the above like this, but the above saves lines of code.
|
||||
if ($age >= 18) {
|
||||
$can_vote = 'Yes';
|
||||
}
|
||||
else {
|
||||
$can_vote = 'No';
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Switch statements
|
||||
// -----------------------------
|
||||
|
||||
// E.g.
|
||||
switch ($isOlympicYear) {
|
||||
case 2023:
|
||||
break; // Without a break, the code will continue executing to the next case.
|
||||
|
||||
case 2024:
|
||||
break;
|
||||
|
||||
case 2025:
|
||||
break;
|
||||
|
||||
default: // Default is the equivalent to an ELSE
|
||||
break;
|
||||
}
|
||||
|
||||
// Using a switch like above instead of the following
|
||||
if ($year === 2023) {
|
||||
//
|
||||
}
|
||||
elseif ($year === 2024) {
|
||||
//
|
||||
}
|
||||
elseif ($year === 2025) {
|
||||
//
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// For loops
|
||||
// -----------------------------
|
||||
|
||||
// E.g.
|
||||
for ($i = 1; $i < 5; $i++) {
|
||||
echo "This is the $i -th iteration.";
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// While loops
|
||||
// -----------------------------
|
||||
|
||||
// E.g.
|
||||
$i = 1;
|
||||
while ($i < 5) {
|
||||
echo "This is the $i -th iteration.";
|
||||
$i++;
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Passing Arguments by Reference
|
||||
// -----------------------------
|
||||
|
||||
function add_five(&$value) {
|
||||
$value += 5;
|
||||
}
|
||||
|
||||
$num = 2;
|
||||
add_five($num);
|
||||
echo $num; // Outputs: 7
|
||||
|
||||
// -----------------------------
|
||||
// Using the ... operator
|
||||
// - Allows for any number of arguments
|
||||
// -----------------------------
|
||||
|
||||
function sum(...$numbers) {
|
||||
$sum = 0;
|
||||
|
||||
for ($i = 0; $i < count($numbers); $i++) {
|
||||
$sum += $numbers[$i];
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
echo sum(1, 2, 3, 4); // Output: 10
|
||||
|
||||
Reference in New Issue
Block a user