From d57132b3915bccaa2264a35de38540a24af51ebe Mon Sep 17 00:00:00 2001 From: pfwd Date: Sat, 10 Dec 2022 18:35:06 +0000 Subject: [PATCH] #124 Adding course note for PHP control structures - Conditional statements --- .../notes/01-conditional-statements.md | 96 ++++++++++++++++++- 1 file changed, 94 insertions(+), 2 deletions(-) diff --git a/php-control-structures/notes/01-conditional-statements.md b/php-control-structures/notes/01-conditional-statements.md index 2eda634..0dbb844 100644 --- a/php-control-structures/notes/01-conditional-statements.md +++ b/php-control-structures/notes/01-conditional-statements.md @@ -1,3 +1,95 @@ -Notes coming soon. +A conditional statement changes the flow of the application based on the evaluation of a condition. -[Edit these notes](https://github.com/howToCodeWell/course-notes) +There are three types of conditional statements in PHP +1. if statement +2. if else statement +3. if elseif else statement + +## 1. If statement +An if statement consists of the conditional statement and the body. The body will get executed if the conditional statement is true. +```php +if ($conditionToCheck) { + // Statement body. + // This gets executed when the $conditionToCheck is true +} +``` + +The example below will output `It is ready` because the variable `$isReady` is evaluated to true. +```php +$isReady = true; +if ($isReady === true) { + // This will run as $isReady is true + echo "It is ready"; +} +``` +The example below will not output `It is ready` because the variable `$isReady` is evaluated to false. +```php +$isReady = false; +if ($isReady === true) { + // This will not run as $isReady is false + echo "It is ready"; +} +``` + +## 2. If else statement +An if else statement consists of the conditional statement and a else block. The else block is executed if the conditional statement is not met. + +```php +if ($conditionToCheck) { + // Statement body. + // This gets executed when the $conditionToCheck is true +} else { + // else block. + // This gets executed when the $conditionToCheck is false +} +``` + +The example below will set the value of `$settings['api_key]` to `abc_test` because the `$mode` has the value of `test`. +```php +$settings = [ + 'api_key' => 'abc_dev' +]; + +$mode = 'test'; +if ($mode === 'prod') { + $settings = ['api_key'] = 'abc_prod' +} else { + $settings = ['api_key'] = 'abc_test' +} + +``` +## 3. If elseif statement +An if elseif statement consists of the conditional statement, an elseif block and a else block. The elseif block is executed if the first conditional statement has not been met but its condition has been met. + +The else block gets executed when the if condition or the elseif condition has not been met. + +```php +if ($valueToCheck) { + // Statement body. + // This gets executed when the $valueToCheck is true +} elseif($anotherValueToCheck) { + // elseif block + // This gets executed when the $valueToCheck is false but the $anotherValueToCheck condition is true +} else { + // else block. + // This gets executed when the $valueToCheck and the $anotherValueToCheck are both false +} +``` + +The example below will set the value of `$discountAmount` to `20` because the `$discountCode` has the value of `HOWTOCODEWELL`. + +In this example the `elseif` block is executed as the if condition has not met but the `elseif` condition is correct. + +```php +$discountCode = 'HOWTOCODEWELL'; + +if ($discountCode === 'BLACKFRIDAY') { + $discountAmount = 10; +} elseif($discountCode === 'HOWTOCODEWELL') { + $discountAmount = 20; +} else { + $discountAmount = 0; +} +``` + +If there are many possible values for `$discountCode` then a switch statement would be recommended. \ No newline at end of file