Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double click and run the Php script on mac

Tags:

html

php

macos

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.

like image 979
TryingToLearn Avatar asked Nov 13 '13 10:11

TryingToLearn


2 Answers

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

like image 83
Thushara Buddhika Avatar answered Oct 19 '22 22:10

Thushara Buddhika


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.

like image 26
SimonSimCity Avatar answered Oct 19 '22 22:10

SimonSimCity