1

I am having trouble with some PHP code working with HTML. I am making a simple program that just lets a user press a button to call a bunch of PHP practice code that I have made. Just basic PHP stuff.

I am new to PHP, and my HTML only returns the PHP in plain text.

My PHP code,

<body>
<?php
  public function randomNumberComparsionAND() {
    $_randomNum = rand(1, 100);
    echo "The random number: " + $_randomNum + "\n";

    if ($_randomNum % 4 == 0 && $_randomNum % 3 == 0) {
      echo "The Number is evenly divisible by 3 and 4\n";
    }
    echo "The number is not evenly divisible by 3 and 4\n";
  }

  public function randomNumberComparsionOR() {
    $_randomNum = rand(1, 100);
    echo "The random number: " + $_randomNum + "\n";

    if ($_randomNum % 4 == 0 || $_randomNum % 3 == 0) {
      echo "The Number is evenly divisible by 3 or 4\n";
    }
    echo "The number is not evenly divisible by 3 or 4\n";
  }

  public function rollDice() {
    $_dice1 = rand(1, 6);
    $_dice2 = rand(1, 6);

    if ($_dice1 == $_dice2) {
      echo "The two dice are equal!\n";
    }
    echo "The two dice are not equal!\n";
  }

  randomNumberComparsionAND();

  randomNumberComparsionOR();

  rollDice();


?>
</body>


My HTML code,

<head>
    <title>PHP Test</title>

    <style>
       body {
          background-color: lightgrey;
       }
    </style>
</head>

<body>
<center>
  <h1>Foo Test</h1>

  <form action="php/index.php" method="get">
    <label>Press Button to get PHP:</label><br><br>
    <input type="submit" name="form">
  </form>
</center>

<script>
</script>
</body>

Output:

enter image description here

The Error,

enter image description here

Expected results:

To execute the PHP code within the methods after clicking the submit button.

EDIT: Xampp is working fine now.

Compiler v2
  • 3,509
  • 10
  • 31
  • 55

2 Answers2

2

The title of your post is wrong. It should be something like "How to prevent My php code from displaying on the screen/browser".

Below are possible scenario.

If your PHP code is being shown in the browser, it implies that your server has not been setup to serve PHP scripts. Below are among few things you will need to do.

1.)PHP Xampp or Wamp Installation:. I prefer Xampp. First step is to ensure that PHP is installed and running correctly. You can download and Install Xampp if you have not done so. An easy way to check if php is installed is to run php -v from a command line and see if returns version information or any errors

2.) Restarting your Server: If you have alter any files prior to this event, you will need to restart your server

3.) PHP File Extension Name: Ensure that you properly save your code as .php file extension name. Code save as .html or .txt will not be executable.

4.) In case if you are using Xampp. Ensure that your php files resides on htdocs folder if you are using xampp Eg

C:\xampp\htdocs\your-php-projects.

5.)Misconfiguration: This may be the last thing to check. But if you care about it, You can also check for misconfigurations.

For example: In Apache’s httpd.conf file, you will need to make sure that the line Eg as case may be "LoadModule php5_module" has been uncommented and that there is no semi-colon (;) at the beginning of the line.

screenshot

Nancy Moore
  • 2,322
  • 2
  • 21
  • 38
  • Check my question for Xampp error. – Compiler v2 Mar 14 '19 at 00:53
  • What line would "LoadModule php5_module" be on in the httpd.conf file? – Compiler v2 Mar 14 '19 at 00:59
  • You will need to just run the app as an administrator. Right click the xampp control panel> run as administrator. – Nancy Moore Mar 14 '19 at 01:03
  • Hello Sir Compiler, have you run the xampp control panel as an Administrator – Nancy Moore Mar 14 '19 at 01:05
  • Which option do I select from the main screen? I have got rid of the error by running Xampp as admin. – Compiler v2 Mar 14 '19 at 01:09
  • if you are on windows(7) then click on windows icon at the bottom left, locate xampp Icon from there. right click on xampp Icon. select run as adminstrator – Nancy Moore Mar 14 '19 at 01:14
  • I am running windows 10, and I have got it running as administrator. I just need to know how to run the server to execute my PHP code. – Compiler v2 Mar 14 '19 at 01:19
  • I have attached screenshot. You will see arrows point on start button for Apache and Mysql database. Click them. then copy your php file to htdoc directory. see my answer 3 and 4. to test the server is running. type this at url http://localhost/index.php – Nancy Moore Mar 14 '19 at 01:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189998/discussion-between-nancy-mooree-and-compiler-v2). – Nancy Moore Mar 14 '19 at 07:47
0

A little addition to your PHP code.

You cannot use access modifiers for your functions unless those functions are inside a class. So, remove all your access modifiers in your functions.

From,

public function randomNumberComparsionAND(){ 

To

function randomNumberComparsionAND() {

And so on for the other two functions.

I know that this might not straight address your issue but will surely help your understanding.

Cheers.

Anjana Silva
  • 8,353
  • 4
  • 51
  • 54