限制php if语句以计算上次写入

限制php if语句以计算上次写入,php,if-statement,Php,If Statement,我试图让几个php if语句只在最后一个fwrite运行时运行。我只想使用$output输出一个$key。以下是我的代码片段: $cnt = 1; fwrite($fh, $phptag); $cnt = 2; // The last fwrite that I want checked fwrite($fh, $key); // Check if write has been successful if ($fwrite !== false && $cnt == 2) {

我试图让几个php if语句只在最后一个fwrite运行时运行。我只想使用$output输出一个$key。以下是我的代码片段:

$cnt = 1;
fwrite($fh, $phptag);
$cnt = 2;
// The last fwrite that I want checked
fwrite($fh, $key);
// Check if write has been successful
if ($fwrite !== false && $cnt == 2) {
  $output = json_encode(array( // create JSON data
    'type'=>'success', 
    'text' => 'Congratulations !  The salt key has been successfully created.'
  ));
  exit($output); // exit script outputting json data
}
// Check if write has been unsuccessful
if ($fwrite === false && $cnt == 2) {
  $output = json_encode(array( // create JSON data
   'type'=>'error', 
    'text' => 'There has been an error in creating the salt key.'
  ));
  exit($output); // exit script outputting json data
}
我尝试使用$cnt来控制何时运行,但是$output从未发送回ajaxdone语句,只是没有定义。我做错了什么

更新代码:

fwrite($fh, $phptag);
// The last fwrite that I want checked
fwrite($fh, $key);
// Check if write has been successful
if (fwrite($fh, $key)) { // Check if write has been successful
  $output = json_encode(array( // create JSON data
    'type'=>'success', 
    'text' => 'Congratulations !  The salt key has been successfully created.'
  ));
}
else {  // Check if write has been unsuccessful
  $output = json_encode(array( // create JSON data
    'type'=>'error', 
    'text' => 'There has been an error in creating the salt key.'
  ));
}
exit($output); // exit script outputting json data
fclose($fh);
更新#2:


你为什么用退出而不是返回?我想这是你的问题


您可以避免未成功检查,因为如果写入未成功,您将始终获得错误输出

为什么要使用exit而不是return?我想这是你的问题


您可以避免未成功检查,因为如果写入未成功,您将始终获得错误输出

fwrite
返回写入文件的字节数,如果出现错误,返回
FALSE
。所以你可以检查一下

if (fwrite($fh, $key)) {
    $output = json_encode(array( // create JSON data
        'type'=>'success', 
        'text' => 'Congratulations !  The salt key has been successfully created.'
      ));
} else {
    $output = json_encode(array( // create JSON data
        'type'=>'error', 
        'text' => 'There has been an error in creating the salt key.'
    ));
}
exit($output);

fwrite
返回写入文件的字节数,如果出现错误,返回
FALSE
。所以你可以检查一下

if (fwrite($fh, $key)) {
    $output = json_encode(array( // create JSON data
        'type'=>'success', 
        'text' => 'Congratulations !  The salt key has been successfully created.'
      ));
} else {
    $output = json_encode(array( // create JSON data
        'type'=>'error', 
        'text' => 'There has been an error in creating the salt key.'
    ));
}
exit($output);

您在哪里设置
$fwrite
?fwrite只使用fopen函数写入文件-这没有问题。您在哪里设置
$fwrite
?fwrite只使用fopen函数写入文件-这没有问题。
return
只能在函数中使用。@Barmar实际上它在全局范围中使用。
return
可以只能在函数中使用。@Barmar实际上在全局范围中使用了它。Hmmmm。。。我试图解决的最初问题是,fwrite的输出发生了两次——这就是我试图避免的。你的代码不会用两个fwrite语句运行两次吗?你在哪里看到两个
fwrite
语句?哦,好吧,我看到你的目标是$key-这很有意义,但它仍然给出了未定义的-我会在上面发布更新的代码,只是为了确定。所有这些都告诉你没有写入文件的错误,比如权限失败(但这通常会在打开文件时被检测到)。什么是未定义的?文本是由ajax完成或失败函数写入html的(这就是未定义的出现的地方-注意:在我尝试进行此调整之前,它正在写出文本,所以我非常确定ajax是正确的。嗯……好吧,我试图解决的原始问题是,fwrite的输出发生了两次-这就是我试图避免的。您的代码不会运行两次吗tements?您在哪里看到两个
fwrite
语句?哦,好吧,我看到您的目标是$key-这是有道理的,但它仍然是未定义的-我将在上面发布更新的代码,只是为了确保。所有这些都告诉您写入文件时没有错误,例如权限失败(但这通常会在打开文件时被检测到)。什么是未定义的?文本是由ajax完成或失败函数写入html的(这就是未定义的出现的地方-注意:在我尝试进行此调整之前,它正在写入文本,所以我很确定ajax是正确的。