Home  >  Article  >  Backend Development  >  PHP 制作了一个计算器,出了点有关问题,求解

PHP 制作了一个计算器,出了点有关问题,求解

WBOY
WBOYOriginal
2016-06-13 12:50:31735browse

PHP 制作了一个计算器,出了点问题,求解!
这是我做0输入的处理代码:

===============================================================

if($_POST['num0']){
    $num=$_POST['num0'];
   if(is_dir('txt')){
            $open=fopen("txt/caculator.txt","r+");
            if(is_file("txt/caculator.txt")){
                $a=file_get_contents("txt/caculator.txt");
                $e=(int)$a;
                $b=$e."0";
                echo $b;
                file_put_contents("txt/caculator.txt",$b); 
                fclose($open);
            }
           }
                }
=================================================================
这是表单:
=================================================================




=================================================================
全部代码在二楼。
主要意思就是 先在localhost下创建文件夹txt,再在文件夹txt下创建文件caculator.txt
caculator2.txt; caculator3.txt; 单击对应数字将数字显示到文本框,并存储到caculator.txt,
然后单击运算符,将对应运算符存储到caculator3.txt中,并将caculator.txt中的字符串存入caculator2.txt,然后清空caculator.txt,这是单击数字按钮输入第二个运算数字,并将第二个运算
数字存入caculator.txt中,当单击等于号,先对caculator3.txt中存入的运算符进行判断,然后进行
相应的运算。
其他都没问题,都可以实现,问题出现在输入0的时候,程序不会把0存入到文件中,导致0的输入不能实现,
不知道什么问题,求解答。初步估计是数据类型的问题。。。
php file 存储
------解决方案--------------------
你的问题在于:
if($_POST['num0']){
因为 $_POST['num0'] 等于 0,所以条件恒不成立。该分支不会进入!
应改作
if($_POST['num0'] == '0'){

你的程序设计的很不合理,需要做大幅度调整
文件操作函数的使用也欠妥
既然是用 file_get_contents、file_put_contents 读写文件,那么就不需要 fopen、fclose 再来做摆设了
file_put_contents 有一个 FILE_APPEND 参数可以附加内容到文件,而无需读出后人工追加

如果需要,可以你一个调整后的代码供参考
------解决方案--------------------
用AJAX,然后动态PHP编译就得了,实际把一大堆公司放进去计算好了。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn