Page 1 of 1

counting

Posted: Mon Feb 13, 2006 9:48 am
by paulmac1966
Hi

Can anyone tell me what i am doing wrong, i am trying to convert a working perl cgi script to PHP

<?
$count=`cat /tmp/webcount.txt`;
$count++1;
system("echo $count > /tmp/webcount.txt");

echo "You Are Visitor No $count";
?>

It should read the value stored in /tmp/webcount.txt and then add 1 to it then write the new value
to the /tmp/webcount.txt file.

i know in perl i would have to "chop" the $count variable to remove the \n (newline) char
is this the same in php ? if so how do i do it.

The system command works, but it is not writing the correct value to the text file.
I know this is probably not the best way of doing it in php, but i am used to shell and perl scripting.

Thanks in advance to anyone who can help

Posted: Mon Feb 13, 2006 10:09 am
by paulmac1966
I have cracked it

should have used

$count = $count + 1

Posted: Mon Feb 13, 2006 10:21 am
by patrikG
you can crack it even further by using

Code: Select all

tags around your code next time you post something

Posted: Mon Feb 13, 2006 10:21 am
by Skittlewidth
or you could have put:

$count++;

Posted: Mon Feb 13, 2006 10:22 am
by feyd
$count ;

works..

Posted: Mon Feb 13, 2006 11:43 am
by josh
Why are you using system (command line calls) when you can use the file handling functions built into PHP?

Posted: Mon Feb 13, 2006 12:08 pm
by jayshields
feyd wrote:$count ;

works..
Wait a tick...

You mean...

Code: Select all

$count ;
is the same as

Code: Select all

$count++;
...8O :?:

Posted: Mon Feb 13, 2006 12:23 pm
by josh

Code: Select all

$count=0;
$count;
var_dump($count);

Code: Select all

php ./test.php
int(0)

Not sure what he means, but that does not increment the variable, all that would do is access the variable to return it to [nowhere?]

Posted: Mon Feb 13, 2006 12:48 pm
by RobertGonzalez
This code is a bit unclear to me...

Code: Select all

<?php
$count=`cat /tmp/webcount.txt`;
$count++1;
system("echo $count > /tmp/webcount.txt");

echo "You Are Visitor No $count";
?>
Wouldn't this set the value of $count to a string ('cat /tmp/webcount.txt')?

It would seem that if the developer wants to get information out of the file referenced by that string he would have to use some filesystem functions to do what he wanted, wouldn't he? After getting the integer from the file, then incrementing it, he would then be able to execute a system() call.

As far as the adding, there are a few ways...

Code: Select all

$count = $count + 1;
/*... OR ...*/
$count += 1;
/*... OR ...*/
$count++;

Posted: Mon Feb 13, 2006 1:02 pm
by josh
Everah wrote:Wouldn't this set the value of $count to a string ('cat /tmp/webcount.txt')?
No it would execute that string on the command line, cat simply reads the file:

Code: Select all

cat - concatenate files and print on the standard output
but yes, he should be using fopen() fread() and fwrite()

Posted: Mon Feb 13, 2006 1:35 pm
by feyd
damnit.. the pluses got removed by quick-reply.. I really have to fix that... :evil:

Posted: Tue Feb 14, 2006 2:09 am
by paulmac1966
Thanks for all the replies

I am using system calls because i am used to writing shell scripts in korn, bash etc, so i dont need to learn php
file commands as i allready know the shell commands (being lazy).

When i write perl scripts i use the system calls to write and read text files, much easier and simpler that
having to open a file give it a file handle then close it.

Thanks