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

  1. Open the XAMPP website.
  2. Click XAMPP for Windows.
  3. Double-click the downloaded file.
  4. Click Yes when prompted. This will open the XAMPP setup window.
  5. Click Next. until install location option.
  6. 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.
  7. Click OK.
  8. Click Next.
  9. Uncheck the "Learn more about Bitnami" box, then click Next.
  10. Begin installing XAMPP. Click Next
  11. Click Finish when prompted.
  12. Select a language.
  13. Click Save. Doing so opens the main Control Panel page.
  14. 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.
  15. 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:

  1. Open Terminal
  2. First install homebrew by pasting in:
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  3. Then paste:
    brew cask install mamp
  4. MAMP should now be in your Applications folder.

In Terminal, paste-in

wget <https://www.apachefriends.org/xampp-files/7.2.2/xampp-linux-x64-7.2.2-0-installer.run>
chmod +x xampp-linux-x64-5.6.33-0-installer.run
./xampp-linux-x64-7.2.2-0-installer.run


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.

  1. Go to: Preference > Languages & Frameworks > PHP
  2. Click ...
  3. Click +
  4. In PHP Executable double click the folder icon.
  5. Navigate to your XAMPP or MAMP
  6. Select bin > php
  7. Select a php version
  8. In the bin directory select php-cgi

Set Local PHP

You can now open your PHP page in a web browser as demonstrated below.

Local PHP Test