New and Clueless With PHP Project

Hello! I’m new to coding, Codecademy, computer science, everything. I am trying my hand at PHP using VS Code, (I heard PHP is one of the more employable languages and its complexity can help with learning other languages) and I was following along with a tutorial to create a calculator, but every time I run the program, it only repeats the code in the terminal. I renamed the file from Calculator.php to Calculator.html which enabled me to open it in my browser (Brave) and it finally looked like it was working, but it wouldn’t spit out an answer when I clicked on the “Calculate” button and VS Code didn’t seem to know what to do with it. What am I doing wrong? Should I just give up on PHP? I appreciate any feedback that you can provide!
Here’s the code: (I don’t even know if I’m doing this right)

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title of the document</title> </head> <body> <form> <input type="text" name="num1" placeholder="Number 1"> <input type="text" name="num2" placeholder="Number 2"> <select name="operator"> <option>None</option> <option>Add</option> <option>Subtract</option> <option>Multiply</option> <option>Divide</option> </select> <br> <button type="submit" name="submit" value="submit">Calculate</button> </form> <p>The answer is:</p> <?php if (isset($_GET['submit'])) { $result1 = $_GET['num1']; $result2 = $_GET['num2']; $operator = $_GET['operator']; switch ($operator) { case "None": echo "You need to select a method!"; break; case "Add": echo $result1 + $result2; break; case "Subtract": echo $result1 - $result2; break; case "Multiply": echo $result1 * $result2; break; case "Divide": echo $result1 / $result2; break; } } ?> </body> </html>

Hello,

Your code works, you’re not getting an answer most likely because you don’t have a web server on your computer.

Browsers know three things: HTML, CSS, and JavaScript. But they can’t interpret PHP.

In order to do that, you’ll need to install a web server (most likely Apache or Nginx).

You can use XAMPP or MAMP, easy to begin with, you just need to download ; )

3 Likes

I went with XAMPP and will get back to you if I can get the code to run. Thank you for your time!

I got it working, thank you so much!

2 Likes