This forum is now read-only. Please use our new forums at discuss.codecademy.com.

For Loops in PHP Forum

View Course » View Exercise

649 points
5135d7cd3b23c16332003427_929523233
Submitted by
Robert Kristian Kjell Alm
about 4 years ago

1.5 loops + Arrays general questions about the syntax.

Hello everyone, and i apologize for my english.
in the following code bellow from the exercise i point out two things that i really need a small explaination:

<html>
  <head>
    <link rel="stylesheet" href="stylesheet.css" />
    <title>Codecademy Languages</title>
  </head>
  <body>
    <h1>Languages you can learn on Codecademy:</h1>
    <div class="wrapper">
      <ul>
        <?php
          $langs = array("JavaScript",
          "HTML/CSS", "PHP",
          "Python", "Ruby");

          foreach ($langs as $lang) {
              echo "<li>$lang</li>";
          }

          unset($lang);
        ?>
      </ul>
    </div>
  </body>
</html>

so...

  1. what about the unset($lang) in the end of the Php code?

  2. How we echo the this numbers in the screen? (i ask because i notice that is an UL list tag, and we dont have any counter in our echo statement).

Thank you for your time :)


5 votes

permalink

As far as I understand (but I'm by no means a PHP expert) the unset doesn’t do any difference in this particular example. See this SO thread.

About the numbers: the <li> items are numbered by the stylesheet (read the stylesheet.css in the exercise):

ul {
  list-style-type: decimal-leading-zero;
}

This is a good example of bad, non-semantic HTML. Sure, you can number an <ul> with CSS, but an <ul> is for unordered or unnumbered lists (see specification), and that’s why you really should use <ol> (ordered list) instead if you’re gonna number them.

One of the many reasons the unsemantic approach is a bad idea is accessibility. Blind or sight-impaired people use screen readers (text-to-speech software), which will read the numbers aloud to you if it’s an <ol>, but probably not if it’s a styled <ul> – CSS is largely for the ones who can see.

3117 points
Picture
Submitted by
Alex J
about 4 years ago

3 Comments

5135d7cd3b23c16332003427_929523233 Robert Kristian Kjell Alm about 4 years ago

It makes sence now. Thank you. :)

5110136bf758055ae9000c0e_634330561 AJ almost 4 years ago

Thanks for your reply Alex J. I am not clear if unset is not necessary then why it is used in the exercise. There should be clear justification for using any JS Terms. The course creator should have explained it.

Picture Alex J over 3 years ago

PHP, not JS, but otherwise I agree with you :)


0 votes

permalink

unset, deletes $lang variable.

If you print $lang before unset, it exists and print Ruby.
If you print after unset... the program said you 'that variable is undefined'


foreach sentence, reads each element of the array.

Like:

for( $a = 0; $a < $myArrayLength; $a++){
    echo $myArray[$a];
}

538 points
919ddc943ca721fa46eca5228ea37ee3?s=140&d=retro
Submitted by
Ignacio
over 3 years ago