-1

Okay, so I'm practicing website programming and I have learnt the basics of PHP and HTML. As my first task I decided to try something with medium difficulty, - create a coinflip program. Within my file I have HTML code and PHP code, however when I run this code in the browser, it appears to show the PHP code and not to specifically what I told it to. Whether i've done something wrong I don't know. Please help me point out the errors and where to fix the problem if you know, thanks.

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; 
}

I have yet to add text inside of the circle etc yet, help would be appreciated.

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

Matt Jones
  • 13
  • 1
  • 7

2 Answers2

1

You need to save the file as a .php instead of a .html. Otherwise, the php is not processed by the server.

Also, as you noted in the comments you do not have a server installed, you will need to install a server that supports PHP. PHP is a server-side language, and is processed by a server program, such as apache. I personally use xampp and have been very happy with it. Here is an xampp install tutorial

Install XAMPP, place your files in the htdocs folder, then visit localhost/myfile.php

Also, as @PVL noted in the comments, you are missing semi-colons, and have a few more errors in your php:

  1. PHP statements must end with a semicolor(;), so:

$winner = rand(0,1) should be winner = rand(0,1);

echo "<p>The Coin Landed On Tails!</p>" & echo "<p>The Coin Landed On Heads!</p>" should both end with a semi-colon.

  1. With if ($winner = 0) { you are assigning a value to $winner, not checking if the value is 0. Your else statement will never be called. To compare the values, use == or === instead (Difference between == and === ) :

if ($winner == 0) {

Your PHP should now look like:

  <?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>";
    }
  ?>
Community
  • 1
  • 1
Jacob G
  • 13,762
  • 3
  • 47
  • 67
0

You must change the file to a .php and upload to a live server or MAMP or WAMP and run it on a local server that can enable php. HTML is not dynamic so it doesn't need a server to show up.