Jump to content

Php Increment


tinfanide

Recommended Posts

 <?phpif(!isset($n)){$n += 1;}else{$n++;}; echo $n."<br />"; $sender = $_POST['sender'];$email = $_POST['email'];$website = $_POST['website'];$message = $_POST['message'];$subject = "testing PHP Mail ".$n;$headers = "At ".$website."\r\n";$headers .= "From: ".$sender; if(mail($email,$subject,stripslashes($message),$headers)){echo "Mail ".$n." was sent to you."."<br />"; $n+=1;echo $n."<br />"; } else {echo "Mail failed.";}; ?> 

I don't know why the variable $n does not increment by 1 each time after the mail is sent.

Link to comment
Share on other sites

you should initialsi $n first vefore you use it

if(!isset($n)){$n += 1;} else{$n++;};
something like
if(!isset($n)){$n = 1;}else{$n++;};

you are using airthmatic oprator in such a variable which does not exist.

Link to comment
Share on other sites

if(!isset($n)){$n = 1;}else{$n++;};
It still remains at 1 each time I send a mail.No increment when I send another email.
<?php$count_my_page = ("mail_sent.txt");$hits = file($count_my_page);$hits[0] ++;$fp = fopen($count_my_page , "w");fputs($fp , "$hits[0]");fclose($fp); $sender = $_POST['sender'];$email = $_POST['email'];$website = $_POST['website'];$message = $_POST['message'];$subject = "testing PHP Mail ".$hits[0];$headers = "At ".$website."\r\n";$headers .= "From: ".$sender;if(mail($email,$subject,stripslashes($message),$headers)){  echo "Mail ".$hits[0]." was sent to you."."<br />";} else {echo "Mail failed.";}; ?>

I've rather done stupidly to deal with it... but proven workable. But for the writing into files part,I wonder what $hits[0]?$hits is a variable and [0] is the array indexSo what does $hits[0] mean? The text file itself? And I tried it the first timeThe counter starts at 1and increments by 1 each time I send a mailSo I wonder where the variable is set to 1 at first?

Link to comment
Share on other sites

file() reads the file and returns it in array. which is stored in $hits . $hits[0] is giving you the first element from the file.

Link to comment
Share on other sites

But the txt file has no content on the server.So no first element can be stored in the variable.And I wonder why the numer starts from 1, not 0.

Link to comment
Share on other sites

$hits[0] ++;

$hits[0] gets the first line of the file. If the file was just created, this first (and only) line would be an empty string.Over THAT string, the ++ is applied. Because ++ is supposed to work on numbers, it converts the string to a number. An empty string is equal to the number 0. ++ then increments it by 1, and places the new value back into $hits[0].From that line on, any time you call $hits[0], you'll be getting the new value, which is the number 1.

Link to comment
Share on other sites

Then how about hits[1]? Does it get the second line?Is hits[0] an array? 0 the index?And why is it linked to file()?And what's the PHP term of this? I may look it up online.

Link to comment
Share on other sites

Then how about hits[1]? Does it get the second line?
if there is second line then value of $hits[1] will be the string of second line. if ithere is no such second line and you try to use $hits[1] it will throw a notice and with NULL value.
Is hits[0] an array? 0 the index?
more appropiately $hits is array, $hits[0] is a element of the array and yes 0 is the index which is pointing to a element
And why is it linked to file()?
file() returns an array in success or FALSE on failure when you write $hits=file('file.txt'); it will open the file and return either a array or boolean FALSE and $hits will hold that return
Link to comment
Share on other sites

if there is second line then value of $hits[1] will be the string of second line. if ithere is no such second line and you try to use $hits[1] it will throw a notice and with NULL value. more appropiately $hits is array, $hits[0] is a element of the array and yes 0 is the index which is pointing to a element file() returns an array in success or FALSE on failure when you write $hits=file('file.txt'); it will open the file and return either a array or boolean FALSE and $hits will hold that return
http://www.w3schools.com/php/func_filesystem_file.asp Yes, it converts lines of the file into an array. It reminds me of batch tokens...It seems that PHP5 has got loads of various methods... lots to learn in PHP
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...