Is there any way to test PHP locally without installing a server? [duplicate]
Asked Answered
R

10

58

I'm looking for something like http://phpfiddle.org/, but completely local. I don't want to commit to installing something as complex as Apache, then PHP on top of that, just to try out code when I'm offline. Is there anything that can run PHP 5.5 on the local machine without installing an entire server underneath it?

Racehorse answered 1/2, 2014 at 8:11 Comment(5)
php.net/manual/en/features.commandline.webserver.php ?Melton
uniformserver.com - runs on a flash driveFascista
You can test your php program by PHP CLI.Thingumajig
The purpose of / and reason for using a complete server is to get as close to a production environment as possible so you're sure in the end that your code works in the real world. Without Apache there to toss out request errors you'll be handicapped as far as debugging.Erstwhile
Using VirtualBox to run a VM that replicates the production environment and versions on your local development box is pretty common practise these days: it allows you to run PHP , MySQL, etc in a sandbox on your development machineInterclavicle
S
120

There's no need for a server if using PHP 5.5+ - it has a built-in server (http://www.php.net/manual/en/features.commandline.webserver.php)

Just use:

$ cd ~/public_html
$ php -S localhost:8000
Shipyard answered 1/2, 2014 at 8:18 Comment(6)
is this the same on Windows?Racehorse
Yes , you just need to install php on your system and the server is bundled with php 5.5+Shipyard
fyi, PHP 5.4+ has it - well it does here.Phenocryst
This worked well for me on Mac, thanks! I'm also looking for a solution to run WordPress locally without installing MAMP or XAMPP. Admin rights required at work to install software, and IT is outsourced/offsite and difficult to work with. Is there such thing as a portable MySQL?Skewness
Portable sql = sqlite - but wordpress doesn't work on it as far as I know. This is just for testing scripts and other simple stuff - it's not a fully fledged serverShipyard
Just use Ctrl-C to stop it afterwards.Nonunionism
H
6

As a minimalistic solution, on the command line you can also start php in interactive shell with php -a that will execute the commands you enter line by line. I often use it for testing small snippets of code.

Hands answered 1/2, 2014 at 8:23 Comment(6)
I did this in XAMPP but it wouldn't run anything I typedRacehorse
Wow, that's odd. What happens when you type echo "Hello\n"; and then ENTER?Hands
It creates a blank new line after echo "Hello\n"; and then does nothing. If I press Ctrl+C, it re-prints all the code I typed, but doesn't run any of it, and brings me back to the general shellRacehorse
You're right, it seems it doesn't work properly on windows. See: #17362709Hands
Oh do I have to type the <??Racehorse
Unfortunately I don't know, I'm not using windows. On Linux/OSX I don't have toHands
B
3

You can download a portable webserver http://www.usbwebserver.net/en/ and use this script under it https://github.com/websiteduck/Run-PHP-Code

Blat answered 3/7, 2015 at 20:29 Comment(0)
T
2

You can install either XAMPP or WAMP server locally if you find it complicated to configure PHP for Apache.

Toughie answered 1/2, 2014 at 8:17 Comment(1)
Alright, I have XAMPP running and got the shell open, but when I use php -a for the interactive shell, nothing I type does anything :/Racehorse
H
2

You should try phpsh as well. It is a php interactive shell from the facebook developer folks with history, tab completion and quick access to documentation.

The project is maintained on github.

Hands answered 5/2, 2014 at 8:59 Comment(0)
H
2

Use psysh which is a wonderful tool for the purpose you described.

If your project is on Laravel, then it's "built in", as tinker, so you can invoke it as php artisan tinker.

Hands answered 25/3, 2016 at 11:43 Comment(0)
F
2

For Windows Users:

Check out the ezPHP GitHub project. Per the project description...

EzPHP is an alternative to Xamp/Wamp. EzPHP is the easiest way to setup a PHP development environment for learning PHP programming on Windows.

The scope of this project is to provide a single .exe file that will get you a PHP developing server.

Link - https://github.com/marcomilon/ezphp

Setting it up was simple. Download the exphp.exe file and drop it in a folder. When you run the exe, it will launch the server and generate a public_html folder and index.php file. Follow the instructions in the command window and navigate to http://localhost:8080. Now you can start developing in the public_html folder and refresh your browser to see your changes.

Firebrat answered 28/1, 2019 at 13:53 Comment(1)
The linked site does not have the exphp.exe file. Where or how can it be downloaded?Incommodity
S
2

This is what I do for simple pages:

  • Download php in zip and extract (PHP 8.0.2 ~25MB)

  • Then run

    > php.exe path\to\your\index.php>path\to\the\output\index.html
    
  • Open the result index.html with your favorite browser

You have to find a workaround for your _GETs though.

Silicify answered 22/2, 2021 at 21:9 Comment(0)
J
1

I really dont think so. but it isnt so complex as you think.

if you are on windows - just download: http://www.wampserver.com/ - it will install the whole server for you (mysql&phpmyadmin,php5).

on linux - got to google: install lamp to [your-linux] -- and follow the simple instructions

Jennie answered 1/2, 2014 at 8:13 Comment(0)
K
0

If you use a development environment, like Aptana Studio, you might as well click on the Run As, and run it in your preferred browser. You need WAMP/XAMPP to be installed and running in order to do so.

Katharynkathe answered 1/3, 2016 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.