2016-03-01 61 views
-1

好的,所以我正在練習網站編程,我已經學習了PHP和HTML的基礎知識。作爲我的第一個任務,我決定嘗試一些中等難度的東西, - 創建一個coinflip程序。在我的文件中,我有HTML代碼和PHP代碼,但是當我在瀏覽器中運行此代碼時,它似乎顯示了PHP代碼,而不是專門告訴它的內容。無論我做錯了什麼,我都不知道。請幫助我指出錯誤以及如何解決問題,如果您知道,謝謝。在一個文件中的PHP和HTML

HTML & PHP:

<!DOCTYPE html> 
<html lang="en"> 
<body> 
<link rel="stylesheet" type="text/css" href="coinflip.css"> 
<title>Coinflip | 50/50!</title> 

<h1><Coinflip</h1> 
<div class="circle"> 

    <?php 
    $flip = 0; 
    $winner = rand(0,1) 
    if ($winner = 0) { 
     echo "<p>The Coin Landed On Tails!</p>" 
    } 
    else { 
     echo "<p>The Coin Landed On Heads!</p>" 
    } 
    ?> 

</div> 

</body> 
</html> 

CSS:

.h1 { 
    font-family: Arial, Verdana, sans-serif; 
    text-align: center; 
    color: black; 
} 
.cirlce { 
    border-radius: 50%; 
    width: 200px; 
    height: 200px; 
} 

我還沒有加入圈子等呢,幫助將不勝感激的內部文本。

的jsfiddle:https://jsfiddle.net/6nLwL08k/

回答

1

您需要將文件保存爲.php而不是.html。否則,php不會被服務器處理。

另外,正如您在註釋中指出的,您沒有安裝服務器,您將需要安裝支持PHP的服務器。 PHP是服務器端語言,由服務器程序(如apache)處理。我個人使用xampp,並一直很滿意。 Here is an xampp install tutorial

安裝XAMPP,將您的文件在htdocs文件夾,然後訪問localhost/myfile.php

而且,@PVL在評論中指出的那樣,你是missing semi-colons,並在你的PHP有一些更多的錯誤:

  1. PHP語句都必須以一個semicolor(;)結束,所以:

$winner = rand(0,1)應該是winner = rand(0,1);

echo "<p>The Coin Landed On Tails!</p>" & echo "<p>The Coin Landed On Heads!</p>"應以分號結尾。

  • 隨着if ($winner = 0) {你是一個值分配給$winner,不檢查,如果該值爲0。你else聲明將永遠不會被調用。爲了比較值,可以使用=====代替(Difference between == and ===) :
  • if ($winner == 0) {

    你的PHP現在應該是這樣的:

    <?php 
        $flip = 0; 
        $winner = rand(0,1); 
        if ($winner == 0) { 
         echo "<p>The Coin Landed On Tails!</p>"; 
        } 
        else { 
         echo "<p>The Coin Landed On Heads!</p>"; 
        } 
        ?> 
    
    +0

    所以用.php擴展名保存的文件就像.htm/.html文件一樣,只是它用於識別php? –

    +0

    是在'.php'文件中php和html代碼正確解釋,但在'。html' php代碼無法正常工作 –

    +0

    正如@Anant所說,是的,一個'.php'文件將呈現與html文件相同的文件,除了其中的php文件將在服務器上處理。 –

    0

    您必須更改文件以.php和上傳到現場服務器或MAMP或WAMP並在可啓用php的本地服務器上運行它。 HTML不是動態的,所以它不需要顯示服務器。