Learning PHP on Your Computer Using MAMP on Localhost

Programmers

Learning PHP on Your Computer Using MAMP on LocalhostWhen I first started learning PHP years ago, I didn’t know about the easy-to-use program MAMP. Instead I coded everything in Dreamweaver and then uploaded the file to my web server to test it. But there’s an easier way to learn PHP – on your computer (localhost).

What is MAMP and what does it do?

MAMP is essentially a program you download that makes your computer into a server – with the ability to run PHP and mySQL. Once installed, MAMP will automatically make files under a certain folder (your choice) accessible like a website for you. You’ll be able to open up a browser and type in localhost/folderName and it will pull up your files just like a normal website.

It’s also free! Click on the link below to go to the MAMP website. You can choose between the free or pro versions, and just download the main link on the left. Don’t worry about downloading anything under the additional PHP versions block.

Download MAMP

Setting the preferences

After installing the downloaded file above (follow the prompts), you’ll want to open up the Preferences on the left side of the small box.mamp

Within the preferences, go to the web server on the far right. You’ll want to set the document root, as shown below. I created a folder specifically for my all my localhost websites – yes, you can have more than one. Underneath my folder titled “Localhost Websites”, are multiple additional folders, each one its own distinct website files.

mamp preferences

Once you’ve set up a document root, you’re ready to test it out!

Testing your new site

Let’s take a look and see if you’ve got it working! Open up a file in your preferred coding program. I use Coda, but whatever you’d like to use, write the following coding into your file.

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

Save the file as index.php under the folder you set as the document root above. The folder setup should be:

Folder Set as Document Root in MAMP Preferences / index.php

Example: WebsiteFolder/index.php

Next, go to the MAMP window we were just in before and click on Start Server.

mamp server

It will most likely ask for your computer’s admin password, and then you’ll see something like the window above: the Server will change to Stop Server, and the Apache Server and mySQL server will both have a filled in boxes.

A window will also open up in your browser (this can be changed under preferences). If it doesn’t happen automatically, by click the Open Start Page link in the MAMP window.

Now you’re ready! In your browser, open up a new window and type in localhost/. (You can also simply click the link.)

You should see Hello World! on the page. If you don’t, or see something else (like a list of files), check back to make sure your file system is correct.

Comments