Designing Solutions Through Programming block 1 Lesson Notes - October 12 2017: Difference between revisions
Mr. MacKenty (talk | contribs) |
Mr. MacKenty (talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 33: | Line 33: | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
<?php | <?php | ||
/* This file helps 9th and 10th grade (15 and 16 years old) students understand pragmatic and practical aspects of arrays as used in the PHP programming language. | |||
*/ | |||
// this is where we define an array: | // this is where we define an array: | ||
Line 39: | Line 42: | ||
echo "<hr>"; | echo "<hr>"; | ||
/ | /* this is how we "dump" an array to see it (blech, ugly). When we dump an array we see the index, the type and the element. Take a look at the sample of part of var_dump below: | ||
... [0]=> string(7) "fighter" [1]=> string(6) "cleric"... | |||
We can see at index zero we have a string which is 7 characters, "fighter". At index one we have a string which is 6 characters, "cleric" | |||
*/ | |||
var_dump($classes); | var_dump($classes); | ||
echo "<hr>"; | echo "<hr>"; | ||
// this is how we access one element in an array | // this is how we access one element in an array. In this case we are selecting the element at index 2 | ||
echo $classes[2]; | echo $classes[2]; | ||
echo "<hr>"; | echo "<hr>"; | ||
// this is how we count elements in an array | // this is how we count elements in an array. | ||
echo count($classes); | echo count($classes); | ||
Line 60: | Line 69: | ||
var_dump($classes); | var_dump($classes); | ||
echo "<hr>"; | echo "<hr>"; | ||
// this is how we delete an item from an array | |||
// this is how we delete an item from an array. | |||
unset($classes[1]); | unset($classes[1]); | ||
Line 66: | Line 76: | ||
echo "<hr>"; | echo "<hr>"; | ||
// loop through an array. | // We often loop through an array. This is also called iterating through an array. | ||
foreach ($classes as $i) { | foreach ($classes as $i) { | ||
echo " | echo "$i is an item in our array<br>"; | ||
# Looping through an array is a very powerful technique, especially when we combine PHP and HTML. | |||
# echo "Please type a name for a $i : <input type=\"text\">"; | # echo "Please type a name for a $i : <input type=\"text\">"; | ||
} | } | ||
echo "<hr>"; | echo "<hr>"; | ||
// let's see if there is a specific element in our array: | // let's see if there is a specific element in our array: | ||
Latest revision as of 20:31, 12 October 2017
|
|
|
|
|
|
|
|
|