10

I am sorry if this question was answered.

Why can't I run php code directly without using terminal on mac.What I mean when you double click on html file it automatically opens in the browser but not in the case of php.If I try to double click on php it opens with some text-editor.

Any help would be helpful.

4
  • 1
    php requires a php-interpreter which are usually only availible on webservers. Take a look at XAMPP, you can host a local webserver and execute your php scripts on your local machine. Commented Nov 13, 2013 at 10:39
  • But mac comes with php installed.I can run the php file but I need to manually type in the file name[path name]. Commented Nov 13, 2013 at 10:45
  • 1
    You should take a computer course first, learn what is executable and what not and learn about default application of files and then at last learn about PHP code how it is executed and what is reading/executing the code (Definitely not the browser). Commented Nov 13, 2013 at 10:48
  • @Houssni Dude of course it runs on server side. Commented Nov 13, 2013 at 10:50

7 Answers 7

55

Try this (for mac),

  1. Open terminal

  2. cd to folder

  3. Start php server - php -S 127.0.0.1:8000

  4. Open browser and enter - http://localhost:8000/file-name.php

Sign up to request clarification or add additional context in comments.

Comments

14

I think you don't understand what PHP is ...

HTML is a markup-language, that can directly be understood by the browser. If the browser opens the file, it can do something with the content.

As PHP is a programming-language, you need a parser. This parser is your PHP executable. This program can understand PHP and does nothing more, than running the code and giving something as result. This result may be an HTML webpage, an image or whatever.

Since you said, you're using a mac, here's a quick introduction on how to set up your personal webserver:

On Mac OSX, PHP and Apache (that's what I use in this example) is already installed and pre-configured. You can just start using it like this:

Go into your system preferences and verify that Web Sharing is enabled.

Open the Finder and go to /Library/WebServer/Documents/localhost. All files that are in there are processed by the local webserver (Apache and PHP, if you want to know that). Place your file in there and open your webserver and call http://localhost/YourFile.php and it will call the file YourFile.php and show you what the output of the script is.

EDIT:

If you are using PHP for scripts, like bash-scripts, see the answer @andreas-baumgart provided.

3 Comments

Dude I know Php is a server side,but I was new to mac so.By the way Web sharing is removed just need to enable using sudo apachectl start
@rak, but you wrote your question as someone, not knowing what a webserver is ... or that you need it to open your website in a browser.
I was using mac for the first time so I thought it should work when we double click it as server was on.
9

To run PHP in MAC, one should start the built-in Apache Web server and also enable the PHP already installed.

This can be done with the following steps.

  1. Go to /etc/apache2/httpd.conf and change the permission to sudo chmod 777 httpd.conf

  2. Then open the above file to uncomment the line #LoadModule php5_module libexec/apache2/libphp5.so

  3. To start the apache built-in server, use the command sudo apachectl start in the terminal.

Now .php files can be created and run from the terminal using php -f filename.php and it can also be run on a browser using http://localhost/filename.php

Comments

6

You cannot execute plain PHP scripts as they are no executable programs but source code. As such they contain just the receipt for an interpreter to create executable code. In order to run your PHP script you need to pass it to the PHP interpreter. In your scenario you can archive that by providing a shebang.

To run your script on double click try this:

  1. Make the script executable using chmod +x yourscript.php
  2. Prepending the according Shebang to the files content: #!/usr/bin/env php.
  3. Select a PHP file in Finder, hit CMD-i and change "Open With" to "Terminal.app".

Comments

4

Late response, but was looking into doing this for myself, this coming up as one of the results in my searching wanted to provide 2 solutions since I ultimately came to both on my own.

Solution #1

The simple way is to go a round about way by writing a wrapper file to execute the script you're working on. Create a file with the following code:

#!/usr/bin/php
<?php

include('name-of-php-script.php');

?>

Save it as wrapper.command The name wrapper isn't important, but the command extension tells Finder that this is a shell script to open up in Terminal. The file itself just executes whatever php script is in the include.

Solution #2

The specific inquiry requires a bit of work.

First make sure that the 1st line of the php script is:

#!/usr/bin/php

This is where the preinstalled version of PHP is installed on Mac OS X. You can always verify by running this command in terminal:

whereis php

Once you've added the Shebang line to the php script you've readied it for automatic execution.

To make it double clickable executeable you have to do the following: Right click on the PHP script and click Get Info. Click where it says Open With, click the default option to see all the available options. Select Other...

Switch where it says Enable: from Recommended Applications to All Applications, and click the checkbox for Always Open With. Choose Terminal as the application. Finally, you have to click the button that says Change All...

OS X will verify you want it to set Terminal as the default application to open .php files

This will make every php file open up in terminal by default, but unless they contain the #!/usr/bin/php line they won't actually run.

Comments

3

Try MAMP

MAMP 4 brings even more opportunities for web developers. We are now supporting MySQL 5.6 and Nginx is now fully integrated. Server starting times have been improved.

enter image description here

Comments

0

Because .php files are not 'executable' per se, instead they are just text files with a PHP extension.

You need to run the php interpreter against the file to execute on it's contents.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.