Designing Solutions Through Programming block 1 Lesson Notes - October 12 2017: Difference between revisions
Mr. MacKenty (talk | contribs) |
Mr. MacKenty (talk | contribs) |
||
(2 intermediate revisions 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: | ||
Line 86: | Line 98: | ||
} | } | ||
echo "<hr>"; | echo "<hr>"; | ||
// let's find the index of something we want to search for: | // let's find the index of something we want to search for: | ||
$indexOfSearchTerm = array_search($searchTerm,$classes); | |||
echo "You are searching for $searchTerm. It looks like this element has been found at index $indexOfSearchTerm."; | |||
echo "<hr>"; | echo "<hr>"; | ||
Line 96: | Line 108: | ||
$randomlySelectedClass = array_rand($classes,1); | $randomlySelectedClass = array_rand($classes,1); | ||
echo $randomlySelectedClass; | echo "We have randomly selected an index from our array. This will most likely change every time you refresh the page. It is possible the same index will be | ||
chosen again. The random index chosen is: $randomlySelectedClass"; | |||
echo "<hr>"; | |||
// let's echo the class we just chose from our array of classes: | // let's echo the class we just chose from our array of classes: | ||
echo $classes[$randomlySelectedClass]; | |||
echo "If we access the element at index $randomlySelectedClass, we find the element $classes[$randomlySelectedClass]. | |||
This will most likely change every time you refresh the page. It is possible the same index will be chosen again."; | |||
Latest revision as of 20:31, 12 October 2017
|
|
|
|
|
|
|
|
|