I wrote the following codes:

<?php

$nama = ('a' => 'John', 'b' => 'James', 'c' => 'Ray', 'd' => 'Lime', 'e' =>  'Kit')

sort $nama
reset($nama)
   
$i=1;
do
      {
      $i++;
      echo "The key is " . $i . "and the value is “ .$i <br />";
      }
while ($i<=5);	
Echo "newline";
 
?>

this error appears:

Parse error: syntax error, unexpected T_DOUBLE_ARROW in C:\xampp\htdocs\php_exercise\exercise3.php on line 4

Why is it?

Thanks.

Your code is sloppy, lines 4, 6 and 7 are not terminated with a semi-colon. Line 4 is missing array . Line 13 has a wrong double quote.

Make the following changes to your code:
1. One line, write

$nama = array('a' => 'John', 'b' => 'James', 'c' => 'Ray', 'd' => 'Lime', 'e' =>  'Kit');

instead of

$nama = ('a' => 'John', 'b' => 'James', 'c' => 'Ray', 'd' => 'Lime', 'e' => 'Kit')

2. On line 6, write

sort($nama);

instead of

sort $nama

3. Online 7, write

reset($nama);

instead of

reset($nama)
<?php

$nama = array('a' => 'John', 'b' => 'James', 'c' => 'Ray', 'd' => 'Lime', 'e' => 'Kit');

//sort ($nama);
//reset($nama);
   
$i=1;
do
      {
      echo "The key is " .$i. " and the value is " .$nama[$i];
	  $i++;
	  }	  
while ($i<=5);	
Echo "newline";
 
?>

The following error appears:

Notice: Undefined offset: 1 in C:\xampp\htdocs\php_exercise\exercise3.php on line 12
The key is 1 and the value is
Notice: Undefined offset: 2 in C:\xampp\htdocs\php_exercise\exercise3.php on line 12
The key is 2 and the value is
Notice: Undefined offset: 3 in C:\xampp\htdocs\php_exercise\exercise3.php on line 12
The key is 3 and the value is
Notice: Undefined offset: 4 in C:\xampp\htdocs\php_exercise\exercise3.php on line 12
The key is 4 and the value is
Notice: Undefined offset: 5 in C:\xampp\htdocs\php_exercise\exercise3.php on line 12
The key is 5 and the value is newline

Any clue? Thanks.

Yes, the keys are not 1 to 5, but a to e.

<?php 
$nama = array('a' => 'John', 'b' => 'James', 'c' => 'Ray', 'd' => 'Lime', 'e' => 'Kit');
 
//sort ($nama);
//reset($nama);
 
foreach ($nama as $key => $value)
{
  echo 'The key is ' . $key . ' and the value is ' . $value . '<br/>\n';
}	  
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.