搜尋
首頁後端開發php教程按照书上的写了个计算图形面积的程序,但是不执行计算,请问哪里出错了

《细说php》第8章的最后一个例子。可以创建表单,但是点击计算之后出错误。请各位大侠指点迷津
接口:class__Shape.php
    interface Shape{
     function area();
     function perimeter();
    }
?>
矩形:class__Rect.php
class Rect implements Shape{
private $width;
private $length;

function __construct($size=""){
$this->width=$size["width"];
$this->length=$size["length"];
}
function area(){
return $this->length * $this->width;
}
function perimeter(){
return 2 * ($this->width + $this->length);
}
}
?>
三角形:class__Triangle.php
class Triangle implements Shape{
private $length1;
private $length2;
private $length3;

function __construct($size=""){
$this->length1 = $size["length1"];
$this->length2 = $size["length2"];
$this->length3 = $size["length3"];
}
function area(){
$s = ($this->length1 + $this->length2 + $this->length3)/2;
return sqrt($s * ($s - $this->length1) * ($s - $this->length2) * ($s - $this->length3));
}
function perimeter(){
return $this->length1 + $this->length2 + $this->length3;
}
}
?>
圆形:class__Circle.php
class Circle implements Shape{
private $radius;

function __construce($size=""){
$this->radius = $size["radius"];
}
function area(){
return pi() * $this->radius * $this->radius;
}
function perimeter(){
return 2* pi() * $this->radius;
}
}
?>
表格:class__Form.php
class Form{
private $formName;
private $request;
private $action;
private $method;
private $target;

function __construct($formName,$request,$action="index.php",$method="get",$target="_self"){
$this->formName=$formName;
$this->request=$request;
$this->action=$action;
$this->method=$method;
$this->target=$target;
}
function __toString(){
$str="

";
$str.="";
$str.="";
break;
case 2:
$str.="";
$str.="";
$str.="";
break;
case 3:
$str.="";
break;
}
$str.="";
$str.="request["action"].">";
$str.="request["action"].">";
$str.="

".$this->formName."

";
$str.="
action."method=".$this->method."target=$this->target";

switch ($this->request["action"]){
case 1:
$str.="
矩形长度: ";
$str.="request["length"].">
矩形宽度: ";
$str.="request["width"].">
三角形边长1: ";
$str.="request["length1"].">
三角形边长2: ";
$str.="request["length2"].">
三角形边长3: ";
$str.="request["length3"].">
圆形半径: ";
$str.="request["radius"].">
";

return $str;
}
}
?>
控制程序:index.php


图形计算器


function __autoload($className){
include ('class__'.ucfirst($className).'.php');
}
?>

图形周长和面积计算器



矩形||
三角形||
圆形


switch ($_REQUEST["action"]){
case 1:
$form=new Form("矩形",$_REQUEST,"index.php");
echo $form;
break;
case 2:
$form=new Form("三角形",$_REQUEST,"index.php");
echo $form;
break;
case 3:
$form=new Form("圆形",$_REQUEST,"index.php");
echo $form;
break;
default:
echo "请选择一个图形
";
}

if(isset($_REQUEST["act"])){
switch($_REQUEST["act"]){
case 1:
$shape=new Rect($_REQUEST);
break;
case 2:
$shape=new Triangle($_REQUEST);
break;
case 3:
$shape=new Circle($_REQUEST);
break;
}
echo "面积为:".$shape->area()."
";
echo "周长为:".$shape->perimeter()."
";
}
?>



回复讨论(解决方案)

没人看?都还没睡醒吗?

class__Form.php 中
$str.="

action."method=".$this->method."target=$this->target";
应为
$str.="
action." method=".$this->method." target=$this->target";
少抄了空格

class__Circle.php 中
function __construce($size=""){
应为
function __construct($size=""){
抄错了一个字符

$str.="<form action=".$this->action."method=".$this->method."target=$this->target";


这句话就有问题了,action是index.php,而method是参数,却直接和index连到一起了。。。
index.phpmethod=
改成这样:
index.php?method=
function __construct($formName,$request,$action="index.php",$method="get",$target="_self"){
$this->formName=$formName;
$this->request=$request;
$this->action=$action.'?';
$this->method=$method;
$this->target=$target;
}

另外,觉得你的那个圆形貌似有问题,都没传参数。
纯肉眼再看,瞎猜,有问题再说~

竟然有人先回复了,还没看到。。。惭愧

class__Form.php 中
$str.="

action."method=".$this->method."target=$this->target";
应为
$str.="
action." method=".$this->method." target=$this->ta……

我修改了之后,圆形和三角形都能输出,可是矩形不能输出

不会吧?我测试的时候没有发现“矩形”有问题呀

不会吧?我测试的时候没有发现“矩形”有问题呀
能成功创建表单,但是点“计算”之后没反应呢。另外,
$str.="request["action"].">";
$str.="request["action"].">";
这两个是干什么用的啊?

如果你不是在 windows 下测试,那么你需要注意文件名中的大小写
class__Rect.php 不能是 class__rect.php 或其他

action 用于控制表单加载
act 用于控制类加载

如果你不是在 windows 下测试,那么你需要注意文件名中的大小写
class__Rect.php 不能是 class__rect.php 或其他

action 用于控制表单加载
act 用于控制类加载
是windows,大小写也区分了,在你的电脑上测试没问题吗?

我测试肯定没有问题,不然我怎么告诉你哪儿有问题呢?

引用 8 楼 xuzuning 的回复:如果你不是在 windows 下测试,那么你需要注意文件名中的大小写
class__Rect.php 不能是 class__rect.php 或其他

action 用于控制表单加载
act 用于控制类加载
是windows,大小写也区分了,在你的电脑上测试没问题吗?
你的代码我也可以运行,包括矩形

我又重新写了一遍,这次三角形又不输出了。晕了,问题到底出在哪呢?
class__form.php:
//表格类,根据不同的选择,创建不同的表格,可选择的有矩形,圆形和三角形。
class Form{
//声明表格的成员属性
private $formName;//表单的名称
private $request;//表单提供的变量,数组类型
private $action;//数据提交到的页面
private $method;//提交的方式
private $target;//新页面打开的方式,默认为_self

function __construct($formName,$request,$action="index.php",$method="get",$target="_self"){
$this->formName=$formName;
$this->request=$request;
$this->action=$action;
$this->method=$method;
$this->target=$target;
}
function __toString(){
$str="

";
break;
case 2:
$str.="";
break;
case 3:
$str.="";
break;
}
$str.="";
$str.="request["action"].">";
$str.="request["action"].">";
$str.="

计算".$this->formName."的面积和周长

";
$str.="action." method=".$this->method." target=".$this->target.">";

switch($this->request["action"]){
case 1:
$str.="
矩形长度: ";
$str.="request["length"].">";
$str.="
矩形宽度: ";
$str.="request["width"].">";
$str.="
三角形第一条边长: ";
$str.="request["length1"].">";
$str.="
三角形第二条边长: ";
$str.="request["length2"].">";
$str.="
三角形第三条边长: ";
$str.="request["length3"].">";
$str.="
圆形半径: ";
$str.="request["radius"].">";
$str.="
";

return $str;
}
}
?>
index.php:


<br> 计算图形的面积和周长 <br>


function __autoload($className){//包含到类时,自动加载到本页
include ("class__".ucfirst($className).".php");//自动加载相应的类所在的文件
}
?>

矩形||
三角形||
圆形


switch($_REQUEST["action"]){
case 1:
$form=new Form("矩形",$_REQUEST,"index.php");
echo $form;
break;
case 2:
$form=new Form("三角形",$_REQUEST,"index.php");
echo $form;
break;
case 3:
$form=new Form("圆形",$_REQUEST,"index.php");
echo $form;
break;
}

if(isset($_REQUEST["act"])){
switch ($_REQUEST["act"]){
case 1:
$shape=new Rect($_REQUEST);
break;
case 2:
$shape=new Triangle($_REQUEST);
break;
case 3:
$shape=new Circle($_REQUEST);
break;
}
echo "面积:".$shape->area()."
";
echo "周长:".$shape->perimeter()."
";
}
?>


其他的代码没有变,到底问题出在哪啊??

class__Form.php

<?phpclass Form{private $formName;private $request;private $action;private $method;private $target;function __construct($formName,$request,$action="index.php",$method="get",$target="_self"){$this->formName=$formName;$this->request=$request;$this->action=$action;$this->method=$method;$this->target=$target;}function __toString(){$str="<table align=center border=5 width=800>";$str.="<caption><h2 id="this-formName">".$this->formName."</h2><caption>";$str.="<form action='$this->action' method='$this->method' target=$this->target";switch ($this->request["action"]){case 1:$str.="<tr><th>矩形长度:</th><td>";$str.="<input type='text' name='length' value=".$this->request["length"]."></td></tr>";$str.="<tr><th>矩形宽度:</th><td>";$str.="<input type='text' name='width' value=".$this->request["width"]."></td></tr>";break;case 2:$str.="<tr><th>三角形边长1:</th><td>";$str.="<input type=text name='length1' value=".$this->request["length1"]."></td></tr>";$str.="<tr><th>三角形边长2:</th><td>";$str.="<input type=text name='length2' value=".$this->request["length2"]."></td></tr>";$str.="<tr><th>三角形边长3:</th><td>";$str.="<input type=text name='length3' value=".$this->request["length3"]."></td></tr>";break;case 3:$str.="<tr><th>圆形半径:</th><td>";$str.="<input type=text name='radius' value=".$this->request["radius"]."></td></tr>";break;}$str.="<tr><td align=center colspan=2><input type=submit value='计算'></td></tr>";$str.="<input type=hidden name='act' value=".$this->request["action"].">";$str.="<input type=hidden name='action' value=".$this->request["action"].">";$str.="</form></table>";return $str;}}?>
index.php
<html><head><title>图形计算器</title></head><body><?php function __autoload($className){include ('class__'.ucfirst($className).'.php');}?><center><h2 id="图形周长和面积计算器">图形周长和面积计算器</h2><hr><a href="index.php?action=1">矩形</a>||<a href="index.php?action=2">三角形</a>||<a href="index.php?action=3">圆形</a><br /></center><?php switch ($_REQUEST["action"]){case 1:$form=new Form("矩形",$_REQUEST,"index.php");echo $form;break;case 2:$form=new Form("三角形",$_REQUEST,"index.php");echo $form;break;case 3:$form=new Form("圆形",$_REQUEST,"index.php");echo $form;break;default:echo "请选择一个图形<br />";}if(isset($_REQUEST["act"])){switch($_REQUEST["act"]){case 1:$shape=new Rect($_REQUEST);break;case 2:$shape=new Triangle($_REQUEST);break;case 3:$shape=new Circle($_REQUEST);break;}echo "面积为:".$shape->area()."<br>";echo "周长为:".$shape->perimeter()."<br>";}?></body></html>

我这个眼神啊!搞定。犯了超级低级的错误。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
簡單地說明PHP會話的概念。簡單地說明PHP會話的概念。Apr 26, 2025 am 12:09 AM

phpsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIdStoredInAcookie.here'showtomanageThemeffectionaly:1)startAsessionWithSessionWwithSession_start()和stordoredAtain $ _session.2)

您如何循環中存儲在PHP會話中的所有值?您如何循環中存儲在PHP會話中的所有值?Apr 26, 2025 am 12:06 AM

在PHP中,遍歷會話數據可以通過以下步驟實現:1.使用session_start()啟動會話。 2.通過foreach循環遍歷$_SESSION數組中的所有鍵值對。 3.處理複雜數據結構時,使用is_array()或is_object()函數,並用print_r()輸出詳細信息。 4.優化遍歷時,可採用分頁處理,避免一次性處理大量數據。這將幫助你在實際項目中更有效地管理和使用PHP會話數據。

說明如何使用會話進行用戶身份驗證。說明如何使用會話進行用戶身份驗證。Apr 26, 2025 am 12:04 AM

會話通過服務器端的狀態管理機制實現用戶認證。 1)會話創建並生成唯一ID,2)ID通過cookies傳遞,3)服務器存儲並通過ID訪問會話數據,4)實現用戶認證和狀態管理,提升應用安全性和用戶體驗。

舉一個如何在PHP會話中存儲用戶名的示例。舉一個如何在PHP會話中存儲用戶名的示例。Apr 26, 2025 am 12:03 AM

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

哪些常見問題會導致PHP會話失敗?哪些常見問題會導致PHP會話失敗?Apr 25, 2025 am 12:16 AM

PHPSession失效的原因包括配置錯誤、Cookie問題和Session過期。 1.配置錯誤:檢查並設置正確的session.save_path。 2.Cookie問題:確保Cookie設置正確。 3.Session過期:調整session.gc_maxlifetime值以延長會話時間。

您如何在PHP中調試與會話相關的問題?您如何在PHP中調試與會話相關的問題?Apr 25, 2025 am 12:12 AM

在PHP中調試會話問題的方法包括:1.檢查會話是否正確啟動;2.驗證會話ID的傳遞;3.檢查會話數據的存儲和讀取;4.查看服務器配置。通過輸出會話ID和數據、查看會話文件內容等方法,可以有效診斷和解決會話相關的問題。

如果session_start()被多次調用會發生什麼?如果session_start()被多次調用會發生什麼?Apr 25, 2025 am 12:06 AM

多次調用session_start()會導致警告信息和可能的數據覆蓋。 1)PHP會發出警告,提示session已啟動。 2)可能導致session數據意外覆蓋。 3)使用session_status()檢查session狀態,避免重複調用。

您如何在PHP中配置會話壽命?您如何在PHP中配置會話壽命?Apr 25, 2025 am 12:05 AM

在PHP中配置會話生命週期可以通過設置session.gc_maxlifetime和session.cookie_lifetime來實現。 1)session.gc_maxlifetime控制服務器端會話數據的存活時間,2)session.cookie_lifetime控制客戶端cookie的生命週期,設置為0時cookie在瀏覽器關閉時過期。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中