Forum Moderators: coopster

Message Too Old, No Replies

unexpected T VARIABLE

Have trouble figuring this one out...

         

kyle42

2:47 am on Jul 5, 2010 (gmt 0)

10+ Year Member



I know I'm over looking something real easy here.. But I keep getting Parse error: syntax error, unexpected T_VARIABLE on line 6. Any help would be appreciated. Thanks!

<?php
$count_my_page = (“hitcounter.txt”);
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , “w”);
fputs($fp , “$hits[0]“);
fclose($fp);
echo $hits[0];
?>

Matthew1980

7:24 am on Jul 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there kyle42,

I'm guessing that your not using a plain text editor for doing this, because the double quote don't look right, to me they look like MS smart quotes:-

<?php
$count_my_page = (hitcounter.txt);
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , w);
fputs($fp , $hits[0]);
fclose($fp);
echo $hits[0];
?>


Read rocknbils comments in this: [webmasterworld.com ] post to see what I think is causing the error. Once you have edited those out you should be able to run this without error.

Also, not picking fault at all, but if this file just contains numerical data, it would be easier to use file_get_contents, then you wouldn't need the array part of it, you could store the int in a simple string, and just increment that, and write back to file, just a thought there :)

Cheers,
MRb

rocknbil

6:39 pm on Jul 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, the quotes should be fixed, but if that were it, it would error on line 2. :-) Here's your problem I think, change this

fputs($fp , “$hits[0]“);

to this. It's the quotes around the array member.

fputs($fp , $hits[0]);

Once you fix that, apply the more efficient methods above, unless you have a reason to read this file line by line.

Edit: Wait a sec. Why are you even using an array here? An array is for a list of values, if this is as it looks, a hit counter with a single value, no need to use an array at all(?)

<?php
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits++;
$fp = fopen($count_my_page , "w");
fputs($fp, $hits);
fclose($fp);
echo $hits;
?>

'Course file_get_contents is more efficient if it's just the one value.

Matthew1980

7:08 pm on Jul 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<Forehead-Slap>

Well, the quotes should be fixed, but if that were it, it would error on line 2. :-)

Doh, I never even noticed that - thanks for noticing that Rocknbil, I'm glad we came to the same conclusion about negating the array by using file_get_contents(), well I did post that before my morning tea!

Cheers,
MRb