Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-OOP简单计算器制作_Php_Html_Css_Oop - Fatal编程技术网

PHP-OOP简单计算器制作

PHP-OOP简单计算器制作,php,html,css,oop,Php,Html,Css,Oop,我是PHP面向对象编程的初学者。我想做一个简单的计算器,在这里我可以输入两个数字并根据我的函数计算它。我可以在OOP中只输入两个数字作为参数。但是我如何在中输入两个数字,并在和变量之间建立连接并显示输出?作为初学者,请原谅我的错误。 提前谢谢 到目前为止,我尝试的是: index.php: <?php require 'functions.php'; ?> <!DOCTYPE html> <html lang="en"> <head>

我是PHP面向对象编程的初学者。我想做一个简单的计算器,在这里我可以输入两个数字并根据我的
函数计算它。我可以在OOP中只输入两个数字作为参数。但是我如何在
中输入两个数字,并在
变量之间建立连接并显示输出?作为初学者,请原谅我的错误。
提前谢谢

到目前为止,我尝试的是:

index.php:

<?php 
  require 'functions.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" >
    <title>PHP OOP | Calculator</title>
    <link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
 <div class="wrapper">
     <div class="form_area">
         <form action="" method="POST">
             <table>
                <tr>
                    <td>First Number :</td>
                    <td><input type="number" name="numOne" placeholder="Input Your First Number"></td>
                </tr>
                <tr>
                    <td>Second Number :</td>
                    <td><input type="number" name="numTwo" placeholder="Input Your Second Number"></td>
                </tr>
                <tr>
                    <td><input type="reset" name="reset" value="Reset"></td>
                    <td><input type="submit" name="submit" value="Calculate"></td>
                </tr>
            </table>
        </form>
    </div>
    <div class="output_area">
        <?php
            if(null !==($_POST['numOne'] && $_POST['numTwo'])){
                $resultsOne = new NumberCalculation();
                echo "Added Results = ".$resultsOne->add()."<br>";
            }
        ?>
      </div>
   </div>
  </body>
 </html>
<?php

class NumberCalculation{
public $numOne;
public $numTwo;

public function __construct($firstNum, $secNum){
    $this->numOne = $firstNum;
    $this->numTwo = $secNum;
}
public function add(){
    return $this->numOne + $this->numTwo;
}
public function subtruct(){
    return $this->numOne - $this->numTwo;   
}
public function multiple(){
    return $this->numOne * $this->numTwo;   
}
public function divide(){
    return $this->numOne / $this->numTwo;   
}
}
global $numOne, $numTwo;

$resultsOne = new NumberCalculation($numOne,$numTwo);
?>
class NumberCalculation{
            public $numOne;
            public $numTwo;
            private $result;

                public function __construct($firstNum, $secNum, $funcName){
                    $this->numOne = $firstNum;
                    $this->numTwo = $secNum;
                    $this->$funcName();                 
                }
                public function add(){
                    $this->result = $this->numOne + $this->numTwo;
                }
                public function subtruct(){
                    $this->result = $this->numOne - $this->numTwo;   
                }
                public function multiple(){
                    $this->result = $this->numOne * $this->numTwo;   
                }
                public function divide(){
                    $this->result = $this->numOne / $this->numTwo;   
                }

                public function result(){
                    return $this->result;   
                }
         }



     $numOne=3; $numTwo=2; $funcName='multiple';
     $resultsOne = new NumberCalculation($numOne,$numTwo,$funcName);
     echo $resultsOne->result();

PHP OOP |计算器
第一个号码:
第二个号码:
functions.php:

<?php 
  require 'functions.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" >
    <title>PHP OOP | Calculator</title>
    <link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
 <div class="wrapper">
     <div class="form_area">
         <form action="" method="POST">
             <table>
                <tr>
                    <td>First Number :</td>
                    <td><input type="number" name="numOne" placeholder="Input Your First Number"></td>
                </tr>
                <tr>
                    <td>Second Number :</td>
                    <td><input type="number" name="numTwo" placeholder="Input Your Second Number"></td>
                </tr>
                <tr>
                    <td><input type="reset" name="reset" value="Reset"></td>
                    <td><input type="submit" name="submit" value="Calculate"></td>
                </tr>
            </table>
        </form>
    </div>
    <div class="output_area">
        <?php
            if(null !==($_POST['numOne'] && $_POST['numTwo'])){
                $resultsOne = new NumberCalculation();
                echo "Added Results = ".$resultsOne->add()."<br>";
            }
        ?>
      </div>
   </div>
  </body>
 </html>
<?php

class NumberCalculation{
public $numOne;
public $numTwo;

public function __construct($firstNum, $secNum){
    $this->numOne = $firstNum;
    $this->numTwo = $secNum;
}
public function add(){
    return $this->numOne + $this->numTwo;
}
public function subtruct(){
    return $this->numOne - $this->numTwo;   
}
public function multiple(){
    return $this->numOne * $this->numTwo;   
}
public function divide(){
    return $this->numOne / $this->numTwo;   
}
}
global $numOne, $numTwo;

$resultsOne = new NumberCalculation($numOne,$numTwo);
?>
class NumberCalculation{
            public $numOne;
            public $numTwo;
            private $result;

                public function __construct($firstNum, $secNum, $funcName){
                    $this->numOne = $firstNum;
                    $this->numTwo = $secNum;
                    $this->$funcName();                 
                }
                public function add(){
                    $this->result = $this->numOne + $this->numTwo;
                }
                public function subtruct(){
                    $this->result = $this->numOne - $this->numTwo;   
                }
                public function multiple(){
                    $this->result = $this->numOne * $this->numTwo;   
                }
                public function divide(){
                    $this->result = $this->numOne / $this->numTwo;   
                }

                public function result(){
                    return $this->result;   
                }
         }



     $numOne=3; $numTwo=2; $funcName='multiple';
     $resultsOne = new NumberCalculation($numOne,$numTwo,$funcName);
     echo $resultsOne->result();

你差点就成功了。将
$\u POST
变量中的输入放入
数值计算
类构造函数中

$resultsOne = new NumberCalculation($_POST['numOne'],$_POST['numTwo']);
echo "Added Results = ".$resultsOne->add()."<br>";

您没有传递表单发布的值。像这样做

<?php
    if(null !==($_POST['numOne'] && $_POST['numTwo'])){
        // pass your values like below
        $resultsOne = new NumberCalculation($_POST['numOne'], $_POST['numTwo']); 
        echo "Added Results = ".$resultsOne->add()."<br>";
    }
?>

将第三个参数(
$funcName
)添加到函数以供全局使用。您只需要传递动作名称,如add、multiple、divide等

functions.php:

<?php 
  require 'functions.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" >
    <title>PHP OOP | Calculator</title>
    <link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
 <div class="wrapper">
     <div class="form_area">
         <form action="" method="POST">
             <table>
                <tr>
                    <td>First Number :</td>
                    <td><input type="number" name="numOne" placeholder="Input Your First Number"></td>
                </tr>
                <tr>
                    <td>Second Number :</td>
                    <td><input type="number" name="numTwo" placeholder="Input Your Second Number"></td>
                </tr>
                <tr>
                    <td><input type="reset" name="reset" value="Reset"></td>
                    <td><input type="submit" name="submit" value="Calculate"></td>
                </tr>
            </table>
        </form>
    </div>
    <div class="output_area">
        <?php
            if(null !==($_POST['numOne'] && $_POST['numTwo'])){
                $resultsOne = new NumberCalculation();
                echo "Added Results = ".$resultsOne->add()."<br>";
            }
        ?>
      </div>
   </div>
  </body>
 </html>
<?php

class NumberCalculation{
public $numOne;
public $numTwo;

public function __construct($firstNum, $secNum){
    $this->numOne = $firstNum;
    $this->numTwo = $secNum;
}
public function add(){
    return $this->numOne + $this->numTwo;
}
public function subtruct(){
    return $this->numOne - $this->numTwo;   
}
public function multiple(){
    return $this->numOne * $this->numTwo;   
}
public function divide(){
    return $this->numOne / $this->numTwo;   
}
}
global $numOne, $numTwo;

$resultsOne = new NumberCalculation($numOne,$numTwo);
?>
class NumberCalculation{
            public $numOne;
            public $numTwo;
            private $result;

                public function __construct($firstNum, $secNum, $funcName){
                    $this->numOne = $firstNum;
                    $this->numTwo = $secNum;
                    $this->$funcName();                 
                }
                public function add(){
                    $this->result = $this->numOne + $this->numTwo;
                }
                public function subtruct(){
                    $this->result = $this->numOne - $this->numTwo;   
                }
                public function multiple(){
                    $this->result = $this->numOne * $this->numTwo;   
                }
                public function divide(){
                    $this->result = $this->numOne / $this->numTwo;   
                }

                public function result(){
                    return $this->result;   
                }
         }



     $numOne=3; $numTwo=2; $funcName='multiple';
     $resultsOne = new NumberCalculation($numOne,$numTwo,$funcName);
     echo $resultsOne->result();

你有什么问题?我的问题是我可以输入两个数字,但我可以显示输出吗?你应该在“数字计算”上传递一个“numOne”或“numTwo”类以显示输出,就像您在函数底部所做的那样。php文件您忘记在构造函数中传递两个数字或者不想在
NumberCalculation
中传递参数。我想给出两个输入,然后“计算”按钮将计算它们。我正在查找此项。您能告诉我
isset
null==
isset
检查
变量
是否已设置,而
为空==只是一个简单的比较。