2011-05-19 104 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