3

I ran this in chrome browser but it didn't show the expected results... It showed the exact same code which was written

<!DOCTYPE html>
<html>
<body>

<?php
echo "My first PHP script!";
?>

</body>
</html>
SPm
  • 83
  • 2
  • 10
  • file extension must be `.php` to run php code. In case you want to treat `.html` as `.php` extension then you have to a bit extra settings.[How do I add PHP code/file to HTML(.html) files?](https://stackoverflow.com/questions/11312316/how-do-i-add-php-code-file-to-html-html-files) – Alive to die - Anant Feb 08 '21 at 05:09
  • no, the extension is actually .php itself – SPm Feb 08 '21 at 05:11
  • then check below answer. May be you haven't installed any server to run php files locally – Alive to die - Anant Feb 08 '21 at 05:13

2 Answers2

0

To run PHP code in your browser, you need a server like XAMP, WAMP, etc. I recommend XAMPP as it is quite easy to use. Download XAMPP from here: https://www.apachefriends.org/index.html

After the installation is done, go the the htdocs folder (C:\xampp\htdocs in windows) and paste your .php file there. After that, open XAMPP control panel and start Apache. Open http://localhost/test.php (assuming the filename is test.php) in your browser and see the result. Done!

AD-1
  • 306
  • 1
  • 17
-1

Before working on PHP Codes. You should know how to run PHP codes in browser.

If you dont have any server then please install some server like XAMPP, WAMP, etc.

Also, If you have installed the server then might be the file you are running on browser has html extension. It should be a .php extension.

& should run using

http://localhost/test.php (If its in htdocs folder)

I suggest you to see the below documentation before working on PHP.

https://codeofaninja.com/2013/06/how-to-run-a-php-script.html

John Doe
  • 1,401
  • 1
  • 3
  • 14