Home » Php » html – can't seem to get php working in MAMP

html – can't seem to get php working in MAMP

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m trying to learn php and step one is getting php working in some capacity. I’m attempting to use MAMP but I’m having some trouble.

Specifically: if I create a file with the below code and save it as index.html in MAMP’s “Document Root” directory, I get a blank page when pointing my browser at http://localhost:8888/index.html.

Code:

<html>
<body>

    <?php
    echo "Hello World!";
    ?>

</body>
</head>

Alternatively, if I put a bit of php into its own file (say test.php) and then point my browser at this file, it just displays the full text of the file in the browser.

Any ideas what I might be doing wrong?

How to&Answers:

You must save a file with PHP inside it with a .php extension. So you would need to name it index.php instead of index.html. Simple fix.

Answer:

I had the similar issue.

Make a new file in TextWrangler or Komodo, or whatever, and add the folllowing code:

AddType application/x-httpd-php .html .htm
AddHandler application/x-httpd-php .html .htm

You’re going to save the file as .htaccess (with the dot in the front; this is the file name).
Save it in /Applications/MAMP/htdocs. This is the same place you’ll save your php and html files. This .htaccess will be an invisible file; you will not see it in Finder, tho you can if you cd to it in Terminal, or searching w/in Finder and choosing the File Visibility type under Kind.

Now try going to localhost:8888/ and you should see all of the available files there. And with this newly created .htaccess file, you can now embed php inside an html file too.

Answer:

In MAMP, edit the file:

/Applications/MAMP/conf/apache/httpd.conf

and then search for ‘#AddHandler type-map’ (exclude quotes). Below that, add,

AddHandler application/x-httpd-php .php .html

Save the file and stop and re-start MAMP. Php parsing will occur in files ending with the extensions: .php and .html.

Answer:

So, this just worked for me:

instead of having:

MAMP/htdocs/folder-that-contains-all-files/

put all your files directly in the htdocs folder!

so:

MAMP/htdocs/all your files including index.php etc.

Hope that helps!