2011-05-19 69 views
0

我这样做了,但当我试图在Internet Explorer上查看它时,我看不到PHP部分。计算一个圆

这是我做的代码:

<html> 
<head><title>Practise</title></head> 
<body> 

<form method="post"> 
Circumference of a Circle or the Area: <br> 

The Radius of the circle: <input type="text" name="radius"><br> 

<input type="submit" value="Submit">  

<?php 

$rad = (float) $_POST['radius']; 
$cir = $rad * 2 * pi(); 
$area = pow($rad, 2) * pi(); 

echo "The circumference of the circle is:" $cir.; 
echo "The area of the circle is:" $area.; 
?> 

</body> 

</html> 

请注明了错误的代码。谢谢!

+1

@Edo:请编辑您的问题,粘贴真正的代码,选择它,然后在编辑器中点击“{}”按钮。它会产生可读的代码和html。 – Mat 2011-05-19 06:23:50

回答

1

两个回声线应该是:

echo "The circumference of the circle is:".$cir; 
echo "The area of the circle is:".$area; 

连接操作符(点)去要合并的字符串之间。

由于语法错误,您的当前代码未执行。

+0

谢谢!更正 – Edo 2011-05-19 06:44:30

+0

@Edo:太好了!你可能想接受我的答案(或passcod的,或约翰格林的 - 他们都解决了同样的问题)。 – cbrandolino 2011-05-19 06:46:37

0

这是更好的:第一

... 
<form method="post" action="this-page.php"> 

Circumference of a Circle or the Area: <br /> 

The Radius of the circle: <input type="text" name="radius" /> <br /> 

<input type="submit" value="Submit" /> 

</form> 

<?php 

if (array_key_exists("radius", $_POST)) { 

    $rad = (float) $_POST['radius']; 
    $cir = $rad * 2 * pi(); 
    $area = pow($rad, 2) * pi(); 

    echo "The circumference of the circle is: $cir."; 
    echo "The area of the circle is: $area."; 
} 
?> 
... 
+0

除了额外的parens(以及imo,使用PHP的字符串扩展和使用isset()而不是array_key_exists() - 这些参数是脱离主题的)的可恶做法之外。 – 2011-05-19 06:35:00

1

那么你得到的字符串连接错误:

$result = "String " $var.; // Wrong 
$result = "String " . $var; // Right 
$result = "String $var"; // Right too. 
$result = "String ", $var; // Also right. 

那么你真的应该做一些输入检查:

if (!empty($_POST['radius']) { 
    // ... 
} 

还有一个封闭</form>标记丢失,以及上的action="..."属性标记 - 虽然这应该默认为页面本身。

最后它的 '实践',而不是 '实践' ... :)

+0

我需要把这个放在哪里? 谢谢! – Edo 2011-05-19 06:43:43

+0

为什么“练习”? “练习”(名词)和“练习”(动词)都是正确的。 ''虽然丢失,并且窗体的'action'属性丢失。至于显示字符串,也可以使用'echo“字符串”$ var;'。 – binaryLV 2011-05-19 06:45:46

+0

不知道那第三种形式。编辑您的意见。 – 2011-05-19 06:54:05

0

你的回声被打破:

echo 'The circumference of the circle is:'.$cir.'.'; 
echo 'The area of the circle is:'.$area.'.'; 
+0

谢谢!!!!!!!!! – Edo 2011-05-19 06:55:41