PHP Switch Statements
In the previous lesson we used a PHP if... elseif statement in order
to execute different block of code for each different condition. As mentioned,
we could use as many "elseif"s as we like.If you have many conditions, PHP switch statements are a more efficient way of doing this. The server will execute a PHP switch statement quicker than multiple "elseif"s. Also, there's actually less code for the programmer to write.
To write a PHP switch statement, you start with the switch keyword followed by the expression to evaluate (for example, a variable name). You then follow that with a case for each condition (based on the expression).
Syntax
|
Code
|
|
|
Example
In the following example, we declare a variable called $favoriteVege and assign a value to it. We then execute a switch statement in order to display a different string depending on the value of the variable.Don't forget to end each case with a break;. This is required, otherwise all cases would be displayed.
|
Code
|
|
|
Sorry, don't know how much energy that vegetable contains.
PHP Arrays
PHP arrays allow you to store groups of related data in one variable (as opposed to storing them in separate variables). Storing data like this in an array has many benefits. For example, if you have a lot of data, you could populate the array programmatically. You could also output the contents programatically - all you need to know if the name of the array.There are 3 different types of PHP arrays:
- Numeric arrays
- Associative arrays
- Multidimensional arrays
Numeric Arrays
Numeric arrays use a number as the "key". The key is the unique identifier, or ID, of each item within the array. We can use this ID later when working with the contents within the array - we don't need to know the item's value, just the ID.Note that numbering starts at zero.
Creating Numeric Arrays
You can choose between the following options when creating an array. Both of these options have the same result.
·
Option 1 - Manual key assignment
When creating an array this way, you assign each
"key" as a number within open and closing square brackets (
[ and ]).|
Code
|
|
|
·
Option 2 - Automatic key assignment
When using this method to create an array, you
don't need to assign the key - PHP will do that for you.
|
Code
|
|
|
Displaying the Array Contents
You can work with each value in an array by referring to its key. For example, to select the 2nd value in an array, we would do this:$arrayName[1]. Just a reminder that
numbering starts at zero, so the 2nd item actually uses the number one as it's
key.Example:
|
Code
|
|
|
Strawberries
Associative Arrays
Associative arrays are similar to numeric arrays but, instead of using a number for the key, we use a value. We then assign another value to the key - you could think of it as two values for the price of one!Creating Associative Arrays
As with creating numeric arrays, there are two options for creating associative arrays in PHP (although in both cases, we have to create the key manually).
·
Option 1
|
Code
|
|
|
·
Option 2
|
Code
|
|
|
Displaying the Array Contents
You can display the contents of associative arrays just as you would with numeric arrays - by referring to it's key.|
Code
|
|
|
Artichoke: 105 kilojoules
Multidimensional Arrays
Multidimensional arrays allow you to put an array inside another array. In other words, the contents of the array is another array. You can do this as many times as you wish - you could have an array inside another array, which is inside another array, etcThe following diagram demonstrates this. We have an array called "Food", which contains 3 arrays (called "Fruit", "Vegetables", "Grains"). Each of these arrays contain their own arrays (one for each food within that group).
|
Food
|
||||||||||
|
||||||||||
|
||||||||||
|
Creating a Multidimensional Array
We can create the above array using the following code:|
Code
|
|
|
PHP While Loops
PHP while loops allow you to execute the same piece of code continuously while a certain condition is true. Once the condition becomes false, the program will break out of the loop and continue processing the rest of the page.Syntax
|
Code
|
|
|
Example
This example executes the same piece of code while the "sharePrice" variable is less than or equal to 10. With each iteration through the loop, we increment the value of $sharePrice by 1. This means the value of $sharePrice will eventually become greater than 10, which will result in the end of the loop, and the rest of the page will be processed.|
Code
|
|
|
The share price is $1. Don't sell yet.
The share price is $2. Don't sell yet.
The share price is $3. Don't sell yet.
The share price is $4. Don't sell yet.
The share price is $5. Don't sell yet.
The share price is $6. Don't sell yet.
The share price is $7. Don't sell yet.
The share price is $8. Don't sell yet.
The share price is $9. Don't sell yet.
The share price is $10. Don't sell yet.
The share price is $11. SELL NOW!
The share price is $2. Don't sell yet.
The share price is $3. Don't sell yet.
The share price is $4. Don't sell yet.
The share price is $5. Don't sell yet.
The share price is $6. Don't sell yet.
The share price is $7. Don't sell yet.
The share price is $8. Don't sell yet.
The share price is $9. Don't sell yet.
The share price is $10. Don't sell yet.
The share price is $11. SELL NOW!
Do/While Loops
A do/while loop is similar to a while loop. The difference is that a do/while loop executes the code at least once before checking the condition. Therefore, if the condition is false, a do/while loop will execute once. A while loop, on the other hand, won't execute the code at all if the condition is false.Example
This example demonstrates the difference between a while loop and a do/while loop.
·
While Loop
|
Code
|
|
|
·
The above code results in:
- The share price is 15. SELL NOW!
·
Do/While Loop
|
Code
|
|
|
·
The above code results in:
- The share
price is 15. Don't sell yet.
The share price is 16. SELL NOW!
No comments:
Post a Comment