954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

error

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 ";
      }
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.

davy_yg
Posting Whiz
377 posts since May 2011
Reputation Points: 10
Solved Threads: 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.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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)
cossay
Junior Poster
128 posts since Nov 2010
Reputation Points: 17
Solved Threads: 22
 
<?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.

davy_yg
Posting Whiz
377 posts since May 2011
Reputation Points: 10
Solved Threads: 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';
}	  
?>
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: