0

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.

3
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by pritaeas
0

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.

0

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)
0
<?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.

0

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';
}	  
?>

Edited by pritaeas: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.