dimanche 24 juillet 2016

Undefined index when accessing an array index

I am trying to modify a user counter code, that prints a message stored in an array. The visitor counter is taken as the array index

<?php
session_start();
$counter_name = "counter.txt";

$age = array("url1","url2","url3");
// Check if a text file exists. If not create one and initialize it to zero.
if (!file_exists($counter_name)) {
  $f = fopen($counter_name, "w");
  fwrite($f,"0");
  fclose($f);
}
// Read the current value of our counter file
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
$counterVal++;
$f = fopen($counter_name, "w");
fwrite($f, $counterVal);
fclose($f); 

echo "hi Hello";
echo $counterVal;   
$me = $age[$counterVal];
echo $me;
?>

But I get the following error,

hi Hello1
PHP Notice:  Undefined index: 1
 in xxx/glob.php on line 23

what can be the possible error as index 1 has a value asociated with it.

Aucun commentaire:

Enregistrer un commentaire