我一直在使用php中的OOP计算器但是我有一个问题:每当我保存它并在firefox中运行它就会显示这样的代码
堆栈溢出要我添加更多描述,所以等等等等等等等等
我一直在使用的代码是
<html>
<body>
<?php
$page = $GET['page'];
class calc{
var $number1;
var $number2;
function add($number,$number2)
{
$result = $number1 + $number2;
echo("The sum of $number1 and $number2 is $result<br><br>");
echo("$number1 + $number 2 = $result");
exit;
}
function subtract($number1,$number2)
{
$result = $number1 - $number2;
echo("$number1 subtract $number2 is $result<br><br>");
echo("$number1 - $number2 = $result");
exit;
}
function multiply($number1,$number2)
{
$result = $number1 * $number2;
echo("$number1 multiplied by $number2 is $result<br><br>");
echo("$number1 * $number2 = $result");
exit;
}
function divide($number1,$number2)
{
$result = $number1 / $number2;
echo("$number1 divided by $number2 is $result<br><br>");
echo("$number1 / $number2 = $result");
exit;
}
}
$calc = new calc();
?>
<title>php object oriented calculator</title>
<form name="calc" action="?page=calc" method="POST">
Number 1: <input type=text name=value1><br>
Number 2: <input type=text name=value2><br>
Operation: <input type=radio name=oper value="add">Addition <input type=radio name=oper value="subtract">Subtraction <input type=radio name=oper value="divide">Division <input type=radio name=oper value="multiply">Multiplication</input><br>
<input type=submit value="Calculate">
</form>
<?php
if($page == "calc"){
$number1 = $_POST['value1'];
$number2 = $_POST['value2'];
$oper = $_POST['oper'];
if(!$number1){
echo("You must enter number 1!");
exit;
}
if(!$number2){
echo("You must enter number 2!");
exit;
}
if(!$oper){
echo("You must select an operation to do with the numbers!");
exit;
}
if(!eregi("0-9", $number1)){
echo("Number 1 MUST be numbers!");
exit;
}
if(!eregi("0-9", $number2)){
echo("Number 2 MUST be numbers!");
exit;
}
if($oper == "add"){
$calc->add($number1,$number2);
}
if $oper =="subtract"){
$calc->subtract($number1,$number2);
}
if $oper = "multiply"){
$calc->multiply($number1,$number2);
}
if $oper = "divide"){
$calc->divide($number1,$number2);
}
}
?>
</body>
</html>
答案 0 :(得分:2)
您不能通过在浏览器中打开文件来运行PHP。现在你正在直接加载文件 - file:///C:/Documents...
。那不行。您需要WAMP,XAMPP或类似程序(或手动下载并安装Apache,PHP,MySQL,phpMyAdmin等)才能运行PHP。
安装完成后,您可以将脚本文件放在www
/ htdocs
文件夹中,然后加载localhost/yourFile.php
。
希望这有帮助!
答案 1 :(得分:1)
您的浏览器不知道如何解释PHP。您需要在计算机上安装PHP来解释PHP并通过服务器将其结果传递给您的浏览器。
正如Anon所说,XAMPP和WAMP是安装所需技术的良好集成解决方案。
答案 2 :(得分:0)
我对您的代码进行了一些调试更改。在这里试试:
代码:http://phpfiddle.org/api/raw/g9x-m73
执行:http://phpfiddle.org/api/run/g9x-m73
代码在php小提琴中由于某种原因没有运行,但你可以在你的系统上试试。
答案 3 :(得分:0)
<?php