What does this code do? I need a description of how it works

Asked

Viewed 73 times

-5

$links=array("Apache Server" =>"www.apague,org", "Apress" =>"www.appress.org", "PHP" =>"www.php.net");
echo "Links ";
$cnt=0;
foreach($links as $key => $value) {
   echo $value .' -$cnt <br/>';
   $cnt++;
}

I need to know that the code does the specified code, and if possible a step by step description of its operation.

  • 2

    It would be interesting to search a little to improve the question, this is a loop of a simple array

  • 1

    An array is created and then every value of it is printed on the screen (foreach), $cnt displays the element number ...

  • 2

    Nothing more than a simple array loop, see more information here http://php.net/manual/en/control-structures.foreach.php

  • 3

    I am voting to close this question as out of scope because the quality is bad and will take an answer where we would have to make long explanations, so that the user understands, since it demonstrates not even know what code is dealing with.

  • SO-PT é um site de perguntas e respostas para programadores profissionais e entusiastas[...], I think that enthusiasts qualifies well the AP. -6 in 12 minutes is a bit hasty, let’s be flexible. I’m sorry who gave down, [IMHO].

  • 2

    I know the quality of the question is not the best, and it’s extremely amateur, but I’m starting with php and I was left with some doubts as to what would be the result of that code. Actually, I was expecting the contents of the array, but I still had some questions and wanted to confirm. I think this forum can also be used for that. Thanks for the answers. Greetings to all.

  • 1

    What was the specific question? take advantage and read Stack Overflow in Portuguese is a forum?

Show 2 more comments

2 answers

3


The vector $links receives 3 positions, each with a website address. The variable $cnt is just a pointer.

The foreach, is a loop function, which does (for each position in the vector $links) the impression of $value (which, in each iteration, contains the respective content, in this case a website).

And the stretch '-$cnt < br />' should be in double quotes ("), to be printed correctly.

I fixed some things in the code, see:

$links = array("Apache Server" => "www.apache.org", "Apress" => "www.appress.org", "PHP" => "www.php.net");

echo "Links: <br>";
$cnt = 0;
foreach ($links as $key => $value) {
    echo $value . " -$cnt <br/>";
    $cnt++;
}

So the result is:

Links:
www.apache.org -0
www.appress.org -1
www.php.net -2


I would do this exhibition like this:

$links = array("Apache Server" => "www.apache.org", 
                   "Apress" => "www.appress.org", 
                   "PHP" => "www.php.net");

$cnt = 0;
foreach ($links as $key => $value) {
    echo "Link[" . $cnt++ . "] " . $key . ' = ' . $value . " <br/>";
}

Because the information would be more intuitive and didactic:

Link[0] Apache Server = www.apache.org
Link[1] Hurry = www.appress.org
Link[2] PHP = www.php.net


I hope I helped. Hugs.

  • Thank you very much for the reply. It was very helpful. Regards!

0

I believe your problem is in foreach($links as $key => $value) {

I will organize the code:

$links = array(
      "Apache Server" => "www.apache.org", 
             "Apress" => "www.appress.org", 
                "PHP" => "www.php.net"
);

$cnt = 0;

foreach($links as $key => $value) {
   echo $value .' -$cnt <br/>';
   $cnt++;
}

Notice that there is a similarity between:

$key => $value

With this excerpt from array():

"Apache Server" => "www.apacue.org"

I think only by => could identify the values, in this case, of the $key and of $value.

If you thought:

$key = "Apache Server"
$value = "www.apache.org"

That’s correct, now just imagine the same processed for all three existing elements.

But beware!

Is not always $alguma => $coisa!

You can also use:

<?

 foreach($links as $value) {
       echo $value .' -$cnt <br/>';
       $cnt++;
 }

?>

Whenever there is only ONE variable, such as $value (but could be any name) will return only the result!

That is, it will only contain: "www.apache.org", "www.appress.org", that is, everything after the =>, even that would be sufficient in this case.

But there’s also a problem!

In case the $cnt is among ', this makes PHP does not display what the variable contains.

Imagine this:

<?php

$nome = 'Inkeliz';

echo '$nome';
// Irá exibir: $nome

echo $nome;
// Irá exibir: Inkeliz

echo "$nome";
// Irá exibir: Inkeliz

?>

For the reason of $cnt be among ' it is not displayed his number, for that just change to:

  echo $value .' -'.$cnt.' <br/>';
// Equivalente ao Segundo método mostrado.

Another possibility is:

  echo $value ." -$cnt <br/>";
// Equivalente ao terceiro método mostrado.
  • Thank you very much Inkeliz, thank you for your time to help me. Regards

Browser other questions tagged

You are not signed in. Login or sign up in order to post.