搜索
首页后端开发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 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在浏览器关闭时过期。

使用数据库存储会话的优点是什么?使用数据库存储会话的优点是什么?Apr 24, 2025 am 12:16 AM

使用数据库存储会话的主要优势包括持久性、可扩展性和安全性。1.持久性:即使服务器重启,会话数据也能保持不变。2.可扩展性:适用于分布式系统,确保会话数据在多服务器间同步。3.安全性:数据库提供加密存储,保护敏感信息。

您如何在PHP中实现自定义会话处理?您如何在PHP中实现自定义会话处理?Apr 24, 2025 am 12:16 AM

在PHP中实现自定义会话处理可以通过实现SessionHandlerInterface接口来完成。具体步骤包括:1)创建实现SessionHandlerInterface的类,如CustomSessionHandler;2)重写接口中的方法(如open,close,read,write,destroy,gc)来定义会话数据的生命周期和存储方式;3)在PHP脚本中注册自定义会话处理器并启动会话。这样可以将数据存储在MySQL、Redis等介质中,提升性能、安全性和可扩展性。

什么是会话ID?什么是会话ID?Apr 24, 2025 am 12:13 AM

SessionID是网络应用程序中用来跟踪用户会话状态的机制。1.它是一个随机生成的字符串,用于在用户与服务器之间的多次交互中保持用户的身份信息。2.服务器生成并通过cookie或URL参数发送给客户端,帮助在用户的多次请求中识别和关联这些请求。3.生成通常使用随机算法保证唯一性和不可预测性。4.在实际开发中,可以使用内存数据库如Redis来存储session数据,提升性能和安全性。

您如何在无状态环境(例如API)中处理会议?您如何在无状态环境(例如API)中处理会议?Apr 24, 2025 am 12:12 AM

在无状态环境如API中管理会话可以通过使用JWT或cookies来实现。1.JWT适合无状态和可扩展性,但大数据时体积大。2.Cookies更传统且易实现,但需谨慎配置以确保安全性。

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

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具