From 49ac48e2707fb7f3e3597e2fcbc4a1d626130869 Mon Sep 17 00:00:00 2001 From: pfwd Date: Tue, 10 Jan 2023 18:11:25 +0000 Subject: [PATCH 1/2] #128 Renaming files --- php-control-structures/notes/05-switch-statements.md | 0 php-control-structures/notes/{05-functions.md => 06-functions.md} | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 php-control-structures/notes/05-switch-statements.md rename php-control-structures/notes/{05-functions.md => 06-functions.md} (100%) diff --git a/php-control-structures/notes/05-switch-statements.md b/php-control-structures/notes/05-switch-statements.md new file mode 100644 index 0000000..e69de29 diff --git a/php-control-structures/notes/05-functions.md b/php-control-structures/notes/06-functions.md similarity index 100% rename from php-control-structures/notes/05-functions.md rename to php-control-structures/notes/06-functions.md From 84eb848ffa2ef574576fbc8f77323515c56b3475 Mon Sep 17 00:00:00 2001 From: pfwd Date: Tue, 10 Jan 2023 18:59:47 +0000 Subject: [PATCH 2/2] #128 Adding switch statement notes --- .../notes/05-switch-statements.md | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/php-control-structures/notes/05-switch-statements.md b/php-control-structures/notes/05-switch-statements.md index e69de29..f289270 100644 --- a/php-control-structures/notes/05-switch-statements.md +++ b/php-control-structures/notes/05-switch-statements.md @@ -0,0 +1,73 @@ +A switch consists of one or many cases that are checked against a supplied value. +Much like a series of if statements a switch can include a default that is used when the supplied value does not match any case. + +In the example the printed string would say `This fruit is a pear` as the variable `$fruit` has the value of `pear`. + +If the supplied `$fruit` does not match any of the case conditions then the output would be `The fruit cannot be found`. + +```php +$fruit = 'pear'; + +switch ($fruit) { + case 'apple': + echo "This fruit is an Apple"; + break; + case 'pear': + echo "This fruit is a pear"; + break; + case 'orange': + echo "This fruit is an orange"; + break; + default: + echo "The fruit cannot be found"; +} + +``` + +This example can also be written using a series of if/if else/else statements. +```php +if ($fruit === 'apple') { + echo "This fruit is an Apple"; +} elseif ($fruit === 'pear') { + echo "This fruit is a pear"; +} elseif ($fruit === 'orange') { + echo "This fruit is an orange"; +} else { + echo "The fruit cannot be found"; +} +``` + +Switch cases can be grouped together. + +In the below example below the output would be `This food is a meat` as `$food` has the value of `beef burger`. + +If `$food` had the value of `apple` or `pear` or `orange` then the output would be `This food is a fruit` + +```php +$food = 'beef burger'; + +switch ($food) { + case 'apple': + case 'pear': + case 'orange': + echo "This food is a fruit"; + break; + case 'beef burger': + echo "This food is a meat"; + break; + default: + echo "The fruit cannot be found"; +} +``` + +The equivalent if statement would look like this + +```php +if ($food === 'apple' || $food === 'pear' || $food === 'orange') { + echo "This food is a fruit"; +} elseif ($food === 'beef burger') { + echo "This food is a meat"; +} else { + echo "The fruit cannot be found"; +} +```