简体   繁体   English

else,elseif,else语句不能在php中使用fwrite

[英]else, elseif,else statement is not working with fwrite in php

i want to write expiry date in a text file with respect to condition but when i echo in browser, it works fine but at the same time it is not writing in text file. 我想在文本文件中写关于条件的有效期,但是当我在浏览器中回显时,它工作正常,但同时它不是在文本文件中写。 Here is my code: 这是我的代码:

condition is based on user selection, problem is that instead of writing expiry date in expiry.cfg, it write only condition like 1,2 or 3. please guide how to resolve this issue. 条件是基于用户选择的,问题是不是在expiry.cfg中写入到期日期,而是只写入1,2或3的条件。请指导如何解决此问题。

$expiry1 = date('Y-m-d H:i:s', strtotime('1 Month'));
$expiry2 = date('Y-m-d H:i:s', strtotime('3 Month'));
$expiry3 = date('Y-m-d H:i:s', strtotime('6 Month'));

$condition = 1;

if ($condition == "1") {
    echo $expiry1;
} elseif($condition == "2") {
    echo $expiry2;
} else{
    echo $expiry3;
}
$myfile = fopen("/var/etc/expiry.cfg", "a") or die("Unable to open file!");
fwrite($myfile,$condition);
fclose($myfile);```

Try this, 尝试这个,

<?php
$expiry = [];
$expiry[1] = date('Y-m-d H:i:s', strtotime('1 Month'));
$expiry[2] = date('Y-m-d H:i:s', strtotime('3 Month'));
$expiry[3] = date('Y-m-d H:i:s', strtotime('6 Month'));

$condition = 1;

if (isset($expiry[$condition])) {
    $myfile = fopen("/var/etc/expiry.cfg", "a") or die("Unable to open file!");
    fwrite($myfile, $expiry[$condition]);
    fclose($myfile);
} else {
    echo 'Invalid condition';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM