/    Sign up×
Community /Pin to ProfileBookmark

fwrite not working correctly in if statement

Okay, here’s what I’ve got. I’m only trying to write a header on my text file if it’s the first time that it’s been opened. This will be opened in excel so I’m wanting to use that line to name the fields. I’m trying this but it coninues to write the header each time something is entered. Anyone have any idea?

[code=php]
$fulldate = $_POST[‘fulldate’];
$writeheader=”Escalated Issue,Order Number,Sent By
“;
$writedata = “$escalation,$order-$line,$emailaddress
“;
$headertest=”1”;

if(!is_writable(“testfile$fulldate.txt”))
{
$headertest=”2″;
}

$handle = fopen(“testfile$fulldate.txt”, “a”);
if($headertest=”2″)
{
fwrite($handle, $writeheader);
}
fwrite($handle, $writedata);
fclose($handle);
[/code]

I’ve also tried this to no avail:

[code=php]
$textfilename=”test$fulldate.txt”;

if(!file_exists($textfilename))
{
$headertest=”2″;
}

$handle = fopen($textfilename, “a”);
if($headertest=”2″)
{
fwrite($handle, $writeheader);
$headertest=”0″;
}
fwrite($handle, $writedata);
fclose($handle);
[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@Tony_shuJul 15.2004 — PHP (as well as PERL when dealing with numbers) prefers [CODE]==[/CODE] whether its a number or a string when your trying to see if a variable is exactly equal. You don't need to use [CODE]"[/CODE] if your checking if its a number. If you only use 1 "=", then you're saying if ($headertest = 2) {

...really means:

if set $headertest = 2 (not check if it equals but check if you successfully defined it)[/QUOTE]



[code=php]if($headertest == 2) { #will return true if the variable is exactly the number 2
if ($headertest == "2") { #will return true if the variable is exactly the string 2
if ($headertest == "two") { #will return true if the variable is exactly the word two
if ($headertest == "two2") { #will return true if the variable is exactly the string two2[/code]
×

Success!

Help @ephmynus 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.19,
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,
)...