Did you know PHP has its own server?
PHP is a server side language, meaning that unlike HTML, you can’t just open up a file with your browser and see it, you need a server. Fear not, when you downloaded PHP you got a built in local server and it’s simple to use.
This is a Mac based tutorial. Skip to the end to see the TL:DR version.
STEP ONE: Create the folder and file
Create a folder on your desktop, and then inside that folder create an index.php file. You can call the folder whatever you want, but for this tutorial, I called it “php-test”. However, the file must be called index.php for this to work, since the server knows to parse that file automatically.
If you already have a project file with an index.php in it, throw it on the desktop and use that.
STEP TWO: Open up Terminal and change your directory
If you’ve never used Terminal, don’t worry, this is not a difficult move. Use Spotlight Search ‘Terminal’ or go to:
Once you’re in Terminal, type (or cut and copy, let’s be honest) the following command to change the directory to our php-test folder on the desktop:
cd ~/Desktop/php-test
(press enter after each command like this)
After you hit enter, the little string of characters to the left of the “$” should change to indicate that you are in a new directory. If you’re using your own pre-made project, swap out “php-test” with your folder name. And be sure the folder name does not include spaces in it (replace them with “-” for now). By the way, “directory” is another way to say folder.
STEP 3: Add some PHP
If you’re using your real PHP project, skip this step. But if you’re using our empty index.php, use your favorite text editor to add PHP to it. Or, if you’re impatient, copy this line into the terminal:
echo "<?php echo 'hello world';" >> index.php
All that does is put:
<?php echo 'hello world';
into your index.php. That way there’s something to see when you open it up.
STEP 3: Activate the server
A cool phrase for a simple command. Put this into your terminal:
php -S localhost:8080
What that does is say, “Hey PHP, activate a server for this folder, and let us see what it outputs when we go to ‘localhost:8080’ in a browser.” To which PHP replies:
So as long as you leave this terminal session open, your server will run. To stop the server, PHP tells you press Ctrl-C while in Terminal.
STEP 5: Visit your site!
Now open whatever browser you want and put:
localhost:8080
into the address bar and you will see your beautiful site:
Troubleshooting
First off, don’t panic. 9 times out of 10, code errors are just happy little typos, so go back and make sure you copied everything in correctly. If that doesn’t work, remember this ancient programming proverb
“copy the error message into Stack Overflow”
If that doesn’t work, God has clearly abandoned you.
Kidding! Keep googling and searching. Never forget in that in programming, there is always a solution and you can find it.
Above and Beyond
This was a very, very basic overview of this feature, why not poke around and see why this works? What’s a “localhost” and why is it 8080? Does the folder have to be on the desktop for this to work? (hint: no). Do you have to restart the server every time you make a change, or refresh the browser?
This is a good way to dip your toe in the server pool, after you get this, why not try installing a local Apache server for real? If you have any questions, comment below and we can Google it together.
Happy coding everyone, you can do this.
TL:DR
Make a folder “php-test” on your desktop and put the file index.php inside
In your terminal do:
cd ~/Desktop/php-test
(change directory)
echo "<?php echo 'hello world';" >> index.php
(add PHP to index.php)
php -S localhost:8080
(activate the server)
Then go to “localhost:8080” in a browser to see the file