counting
Moderator: General Moderators
-
paulmac1966
- Forum Newbie
- Posts: 16
- Joined: Mon Feb 13, 2006 9:37 am
counting
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
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
you can crack it even further by using
Code: Select all
tags around your code next time you post something- Skittlewidth
- Forum Contributor
- Posts: 389
- Joined: Wed Nov 06, 2002 9:18 am
- Location: Kent, UK
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
Wait a tick...feyd wrote:$count ;
works..
You mean...
Code: Select all
$count ;Code: Select all
$count++;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?]
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
This code is a bit unclear to me...
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
<?php
$count=`cat /tmp/webcount.txt`;
$count++1;
system("echo $count > /tmp/webcount.txt");
echo "You Are Visitor No $count";
?>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++;No it would execute that string on the command line, cat simply reads the file:Everah wrote:Wouldn't this set the value of $count to a string ('cat /tmp/webcount.txt')?
Code: Select all
cat - concatenate files and print on the standard output-
paulmac1966
- Forum Newbie
- Posts: 16
- Joined: Mon Feb 13, 2006 9:37 am
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
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