counting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paulmac1966
Forum Newbie
Posts: 16
Joined: Mon Feb 13, 2006 9:37 am

counting

Post 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
paulmac1966
Forum Newbie
Posts: 16
Joined: Mon Feb 13, 2006 9:37 am

Post by paulmac1966 »

I have cracked it

should have used

$count = $count + 1
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

you can crack it even further by using

Code: Select all

tags around your code next time you post something
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

or you could have put:

$count++;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$count ;

works..
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Why are you using system (command line calls) when you can use the file handling functions built into PHP?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

feyd wrote:$count ;

works..
Wait a tick...

You mean...

Code: Select all

$count ;
is the same as

Code: Select all

$count++;
...8O :?:
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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?]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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++;
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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()
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

damnit.. the pluses got removed by quick-reply.. I really have to fix that... :evil:
paulmac1966
Forum Newbie
Posts: 16
Joined: Mon Feb 13, 2006 9:37 am

Post 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
Post Reply