Local PHP
Sometimes it’s nice to test out ideas on your own or just as a means of Staging before uploading to your web server. Here are a few ways you can run php locally to do some debugging.
PHP REPL
If you are running macOS then you can run a local PHP REPL (Read, Evaluate, Print, Loop) in the Terminal with this command. If you are on windows or linux, you will need to install PHP (see below)
php -a
You can now type in individual lines of PHP code to see if they work, a great tool for debugging. Try
echo 'Hello, World!';
echo 1 + 1;
You can also run php scripts with:
require_once('PATH/TO/FILENAME.php')
Installing PHP
- Open the XAMPP website.
- Click XAMPP for Windows.
- Double-click the downloaded file.
- Click Yes when prompted. This will open the XAMPP setup window.
- Click Next. until install location option.
-
Select an installation location. Click the folder-shaped icon to the right of the current installation destination, then click a folder on your computer.
- If you have the UAC activated on your computer, avoid installing XAMPP in your hard drive's folder (e.g., OS (C:)).
- You can select a folder (e.g., Desktop) and then click Make New Folder to create a new folder and select it as the installation destination.
- Click OK.
- Click Next.
- Uncheck the "Learn more about Bitnami" box, then click Next.
- Begin installing XAMPP. Click Next
- Click Finish when prompted.
- Select a language.
- Click Save. Doing so opens the main Control Panel page.
-
Start XAMPP from its installation point. If you need to open the XAMPP Control Panel in the future, you can do so by opening the folder in which you installed XAMPP, right-clicking the orange-and-white
xampp-control icon, clicking Run as administrator, and clicking Yes when prompted.
- When you do this, you'll see red X marks to the left of each server type (e.g., "Apache"). Clicking one of these will prompt you to click Yes if you want to install the server type's software on your computer.
- Counterintuitively, double-clicking the xampp_start icon doesn't start XAMPP.
-
Resolve issues with Apache refusing to run. On some Windows 10 computers, Apache won't run due to a "blocked port". This can happen for a couple of reasons, but there's a relatively
easy fix
- Click Config to the right of the "Apache" heading.
- Click Apache (httpd.conf) in the menu.
- Scroll down to the "Listen 80" section (you can press Ctrl+F and type in listen 80 to find it faster).
- Replace 80 with any open port (e.g., 81 or 9080).
- Press Ctrl+S to save the changes, then exit the text editor.
- Restart XAMPP by clicking Quit and then re-opening it in administrator mode from its folder.
macOS comes with a built-in version of PHP. However, in order to use it with PHPStorm you will need a different version. The easiest way will be to install MAMP
To install MAMP:
- Open Terminal
- First install homebrew by pasting in:
- Then paste:
-
MAMP should now be in your
Applications
folder.
In Terminal, paste-in
PHPStorm
Now you have PHP installed on your machine, you can run your php scripts and webpages in PHPStorm. Here is a step by step and see the GIF below.
- Go to:
Preference
>
Languages & Frameworks
>
PHP
- Click
...
- Click +
- In
PHP Executable
double click the folder icon. - Navigate to your
XAMPP
orMAMP
- Select
bin
>
php
- Select a php version
- In the
bin
directory selectphp-cgi
You can now open your PHP page in a web browser as demonstrated below.