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.