'How to run PHP locally on MacOS

I need a php sandbox on my local.

But I have to make just one class in PHP.

In NODE.JS I can do the following:

test.js

// my test js script
console.log('hello world')

I can run it like this:

~$ node test.js
~$ hello world
~$ 

I would like to do something like this:

test.php

echo('hello world');

I would like to run it like this:

~$ php test.php
~$ hello world
~$ 

Is there any way to do this, without installing apache and everything? I just like to have a php sandbox on my mac.

php


Solution 1:[1]

OSX comes with php installed :

$ which php # => /usr/bin/php

So you can run your php file. But don't forget to include your code between <?php ... ?>.

// test.php
<?php
echo("Hello world")
?>

So now you can run in your terminal:

$ php test.php
$ Hello world%

Apache is not needed.

Solution 2:[2]

download and install xampp or any alternative that runs an Apache server on local machine should do the trick.

edit: Just googled a little myself, apparently you can just download php and run the web server from cli now, $ php -S localhost:8000

Solution 3:[3]

First of all, you have to check if php is installed on your mac. Use the following command to check this :

php --version

Since PHP is installed by default on mac, you should get something like this:

PHP 7.4.10 (cli) (built: Sep 3 2020 18:19:30) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.10, Copyright (c), by Zend Technologies

Once you've done that, you can create a PHP file containing just one line :

<?php echo "Hello world\n"; ?>

Output:

Hello World

You only need PHP. You do not need anything else.

Solution 4:[4]

Apache and php are already installed on MacOS

Alternatively You can use docker or just find sandbox on-line, e.g. https://sandbox.onlinephpfunctions.com/

Solution 5:[5]

I had a similar problem when I came into Mac, and before using Homebrew my temporary solution is mamp. Simply a quick solution that helped me work.

then there are articles that explain well how to create a lamp en mac development environment: Setup Local (L)AMP Stack on Mac with Homebrew

Solution 6:[6]

Try opening a localhost. You can use XAMPP etc...

Solution 7:[7]

You can install only pah with Homebrew.

Make sure the brew is updated

brew update
brew upgrade

Install PHP 7.2

brew install php72

Wait for the installation to finish, you’re now running on PHP 7.2. You may run $ php -v to check the current PHP version on your machine.

now create file test.php

<?php
 echo "hello";

and run

php path/to/test.php

Solution 8:[8]

I would recommend Docker...

Since it's running in a virtual machine already, it's the perfect sandbox. And you can add new services or change the setup, like trying new PHP versions, different webservers, or other operating system quite easily.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Renaud Kern
Solution 2
Solution 3 Sara Yusri
Solution 4 Pinks Not Dead
Solution 5 Jatniel
Solution 6 maDeveloper
Solution 7
Solution 8 Honk der Hase