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"
?>
  1. First we create a new array $myArray with a list of colors.
  2. Then we output the item at position 1. Since items are numbered starting from 0, "blue" is at position 1
  3. Next we change the item at position 1 to"green".
  4. 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 elements is as easy as selecting them with [] or {} notation and assigning them a new value with =!
Arrays 6/7
<html>
  <head>
    <title>Modifying Elements</title>
  </head>
  <body>
    <p>
      <?php
        $languages = array("HTML/CSS",
        "JavaScript", "PHP", "Python", "Ruby");

        $languages[0] = "Jan";

        echo $languages[0];

      ?>
    </p>
  </body>
</html>

Comments

Popular posts from this blog

MEGA 暫存檔案刪除

IOS GCD多執行緒

利用CMD指令強制刪除