Posts

Showing posts from November, 2015

Arrays 7/7

Image
Deleting Array Elements Finally, you can remove elements using unset : <?php $array = array ( "red" , "blue" , "green" ); unset ( $array [ 2 ]); ?> You can even delete the whole array: <?php unset ( $array ); ?> Instructions Go ahead and remove  "Python"  from the $languages  array using  unset () . ? Hint Test how well you're learning and fill in gaps with Codecademy Pro. Learn more. Use the example in the instructions as a guide!

Arrays 6/7

Modifying Array Elements An item in an array can be changed by specifying its position and providing a new value, like this: <?php $myArray = array ( "red" , "blue" , "yellow" ); echo $myArray [ 1 ]; // outputs "blue" $myArray [ 1 ] = "green" ; echo $myArray [ 1 ]; // outputs "green" ?> First we create a new array  $myArray  with a list of colors. Then we output the item at position 1. Since items are numbered starting from 0,  "blue"  is at position 1 Next we change the item at position 1 to "green" . Now when we output the item at position 1, we get  "green" . Instructions In  line 8 , there's an array named  $languages . 1. Change an item in the  $languages  array. You can take your pick! 2. Then use  echo  to output  $languages . ? Hint Test how well you're learning and fill in gaps with Codecademy Pro. Learn more. Modifying array

Arrays 5/7

Access by Offset with { } PHP is a very flexible language. When accessing arrays by offset, you can actually use two different types of syntax: the  [] syntax we've covered, or you can use curly braces ( { } ). You use the curly braces just like you use the square brackets: <?php $myArray = array ( "do" , "re" , "mi" ); print $myArray { 2 }; // prints "mi"; ?> Both forms are equivalent, and using one or the other is totally up to you! Instructions Go ahead and update your previous code to use the  { }  syntax instead of  [] . ? Hint Test how well you're learning and fill in gaps with Codecademy Pro. Learn more. All you need to do is replace  [  with  {  and ]  with  } .

Arrays 4/7

Access by Offset with [ ] Each item in an array is numbered starting from 0. For example, when we create an array: <?php $myArray = array ( "do" , "re" , "mi" ); ?> Each item is numbered starting from 0, like this: +------+------+------+ | "do" | "re" | "mi" | +------+------+------+ 0 1 2 The item  "do"  is in position 0, the item  "re" is in position 1, and so on. Therefore, we can access a particular item of the array using its position, like this: <?php $myArray = array ( "do" , "re" , "mi" ); echo $myArray [ 0 ] // outputs "do" ?> First we create an array named  $myArray Then we use  echo  to output the first item in $myArray . Since items are numbered starting from 0,  "do"  is at position 0. Instructions In  line 8 , there's an array named  $tens . Use echo  to output the third

Arrays 3/7

Creating Your First Array Nice work! Now let's try creating an array from scratch. Instructions Create an array called  $friends  and put the names of three of your friends in it. Since each friend's name is a string, make sure to write it between quotes. Click "Stuck? Get a hint!" for an example. ? Hint Test how well you're learning and fill in gaps with Codecademy Pro. Learn more. Remember, array syntax looks like this: $arrayName = array ( "do" , "re" , "mi" );

Arrays 2/7

Array Syntax Have you noticed something familiar at the start of our array? That's right, it starts in the same way as a variable, with the  $  sign, and then a name, followed by  = . However, this is when things start to get different. When declaring an array, we have to use  array () . This basically tells PHP that $array  is an array and not something else, such as a regular old variable. By now, I am sure you have noticed the text inside the  (  and  ) . This is just the items in our array. So, currently, our array has the items "Egg," "Tomato," and "Beans" in it. You can add any type of information to an array, and you do it in much the same way as when declaring variables. Use  ""  when adding strings, and just enter the number when adding integers. You must always remember, however, that each item in an array  must  be separated by a comma:  , . Instructions Add two more items to our array,  "Chips" and

Arrays 1/7

What's an Array? An array is a list of items, a bit like a shopping list. It allows you to store more than one item in only one variable. Think of it like this. When writing your shopping list, you could use a separate piece of paper for each item you need to buy (a variable). However this is silly and unneeded—could you imagine how hard it would be to carry all that paper around with you? So, you use one piece of paper for all of your items. This one piece of paper is your array. In the editor do you see the bit of text that starts with  $array  = ? That is our array. Don't worry about all the details just yet, we will explain in more detail later. For now, just see if you can work out what is happening. Instructions Once you are ready to move on, click Save & Submit Code. ? Hint Test how well you're learning and fill in gaps with Codecademy Pro. Learn more. Have you clicked Submit? There is no need to do anything else in this exercise.

Control Flow: Switch 6/6

All On Your Own! It's time to show what you have learned about switch es so far! Instructions Create a switch statement with  3  different case s and a  default  case if you wish. Feel free to use the alternative syntax as well! ? Hint Test how well you're learning and fill in gaps with Codecademy Pro. Learn more. Feel free to check back to the previous exercises, but here's a small reminder: switch (X) { case X: break ; default : }