我是一個初學者PHP面向對象編程。我想做一個簡單的計算器,我可以輸入兩個數字,並根據我的function
計算它。我可以在OOP作爲參數只輸入兩個數字輸入。但是如何在<form>
中輸入兩個數字並在<form>
和variable
並顯示輸出?作爲初學者,請原諒我的錯誤。
謝謝先進。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>
的functions.php:
<?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);
?>
你有什麼問題? –
我的問題是,我可以輸入兩個數字,但我可以顯示輸出? –
你應該在「NumberCalculation」類上傳遞一個「numOne」或「numTwo」來顯示輸出,就像你在functions.php文件底部所做的那樣 –