/    Sign up×
Community /Pin to ProfileBookmark

number in a file, auto. go up one

Say I have the number “1” in a file, What command would I use to make that number go up to the next number?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@pcthugMay 02.2006 — Here is a basic example of such:[code=php]
<?php
$file = 1;
$increasenumber = ++$file;
//$increasenumber will make $file(1) go up to the next number
echo $increasenumber;
?>[/code]
Copy linkTweet thisAlerts:
@SpectreReturnsMay 02.2006 — Do you want to record the new number too?
Copy linkTweet thisAlerts:
@Heavy_MetalauthorMay 02.2006 — pcthug, it doesn't seem like that would make the number in the file change to the next number (I.E. 1 going to 2, 500 going to 501, no matter what the number is)

spectrereturns, i dont want to record the new number, i want to change the number in the file, from whatever it is to the next number, like auto_increamate in a DB
Copy linkTweet thisAlerts:
@Heavy_MetalauthorMay 03.2006 — also can anyone tell me what this [code=php]$num = $read1 + $read2 + $read3 + $read4;[/code] is all about? I saw this variable in a code that i downloaded to look at and I dont understand why they added variables inside a variable and what that would do... can someone please explain?
Copy linkTweet thisAlerts:
@balloonbuffoonMay 03.2006 — [code=php] $num = $read1 + $read2 + $read3 + $read4; [/code]This sets the variable $num to the sum of the values $read1, $read2, $read3, and $read4.

For your incrementing file thing, you could do the following:
[code=php]$filename = "count.txt";
$org = file_get_contents($filename);
$org++;
file_put_contents($filename,$org);[/code]
You must have PHP 5, since it uses [I]file_put_contents[/I], but that can be switched out for more code that does the same thing and will work with earlier versions of PHP.

Good Luck,

--Steve
Copy linkTweet thisAlerts:
@NogDogMay 03.2006 — For non-PHP5:
[code=php]
<?php
$file = "count.txt";
$num = file_get_contents($file) or die ("Unable to read file.");
$num = trim($num);
$fh = fopen($file, 'w') or die("Unable to open file for writing");
fwrite($fh, ++$num) or die("File write failed");
fclose($fh);
?>
[/code]
×

Success!

Help @Heavy_Metal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...