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

-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 makes the code indicated, and if possible a step-by-step description of how it works.

    
asked by anonymous 15.02.2016 / 17:26

2 answers

3

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

The foreach is a loop function, which makes (for each position in the $links vector) the impression of $value (which contains, respectively in each iteration, its contents, in this case a site) .

And the '-$cnt < br />' excerpt should be enclosed in double quotation marks, "), 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 display 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] Apress =   www.appress.org
Link [2] PHP = www.php.net

I hope I have helped. Hugs.

    
15.02.2016 / 18:03
0

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

I'll 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 that only => could identify what the values are, in this case, $key and $value .

If you thought:

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

That's right, now just imagine the same rendered for all three existing elements.

But, attention!

It's 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 only return the result!

That is, it will only contain: "www.apache.org", "www.appress.org", that is, anything after => , even that would suffice in this case.

But there's also a problem!

In case the $cnt is between ' , this causes PHP not to 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

?>

Because the $cnt is between ' its number is not displayed, so 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.
    
15.02.2016 / 19:30