58

Could anyone please tell me how to run a php file locally on my system.
Currently I am using a server to run files.
I know both php & Apache to be installed.
I need to see out put of this program, for example:

<?php 
$a=5;
$b=10;
$c=$a+$b;
print $c;
?>

Can you please tell how I can run these files of if I need anything more.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
logan
  • 7,946
  • 36
  • 114
  • 185
  • 1
    is the file `.php`. Did you put the files in the htdocs directory of apache? How do you access the file, through `http://localhost/yourscript.php`? – Rene Pot Dec 20 '11 at 18:39
  • Do you want to run the script on a local Apache server on your computer? Or you prefer to run the script without a web server (i.e. outside a browser, like a normal executable program)? Is your computer a Windows or a Linux machine? – lorenzo-s Dec 20 '11 at 18:39
  • 13
    Install just php and in command line : `php -f path/to/file.php` – piotrekkr Dec 20 '11 at 18:40
  • possible duplicate of [PHP installation on windows](http://stackoverflow.com/questions/5153042/php-installation-on-windows) – mario Dec 20 '11 at 18:40
  • 2
    May be worth your time to look in to [WAMP](http://www.wampserver.com/en/) (If you're running windows) or [LAMP](http://en.wikipedia.org/wiki/LAMP_%28software_bundle%29) for *nix – Brad Christie Dec 20 '11 at 18:40
  • @logan: What is your operating system? On the basis of your question's content I suppose it is Windows. Am I right? – Tadeck Dec 20 '11 at 18:42
  • @ Topener, i placed sample.php in htdocs, then tried http://localhost/sample.php but it only shows code of php file. – logan Dec 20 '11 at 19:14
  • @piotrekkr, Isn't `-f` optional? – Pacerier Feb 03 '15 at 15:51
  • @piotrekkr , obviously.. Its Unix command .. -f indicates force.. – logan Feb 03 '15 at 15:58
  • 1
    @logan It's not `force`, more `file to read and execute` ( `man php` tells `-f file Parse and execute file`) and yes it is optional. – piotrekkr Feb 03 '15 at 21:02
  • This [run php on windows](https://www.youtube.com/watch?v=PUwRinHHTCo) (localhost) video is pretty much the best tutorial for that. – InfiniteStack Aug 02 '22 at 02:43

7 Answers7

80

php have a easy way to run a light server:
first cd into php file directory, then

php -S 127.0.0.1:8000

then you can run php

peter zhang
  • 1,247
  • 1
  • 10
  • 19
  • 1
    installed php with `sudo apt install php7.2-cli` on ubuntu 18.04. – monkut Aug 16 '19 at 01:33
  • 3
    for what it is worth, unless your file is named `index.php` you will have to add the file path to the URL (i.e. `http://localhost:8000/MyFile.php`) – Tom Howard Nov 03 '21 at 01:32
44

You have to run a web server (e.g. Apache) and browse to your localhost, mostly likely on port 80.

What you really ought to do is install an all-in-one package like XAMPP, it bundles Apache, MySQL PHP, and Perl (if you were so inclined) as well as a few other tools that work with Apache and MySQL - plus it's cross platform (that's what the 'X' in 'XAMPP' stands for).

Once you install XAMPP (and there is an installer, so it shouldn't be hard) open up the control panel for XAMPP and then click the "Start" button next to Apache - note that on applications that require a database, you'll also need to start MySQL (and you'll be able to interface with it through phpMyAdmin). Once you've started Apache, you can browse to http://localhost.

Again, regardless of whether or not you choose XAMPP (which I would recommend), you should just have to start Apache.

Pacerier
  • 86,231
  • 106
  • 366
  • 634
Bhaxy
  • 5,346
  • 10
  • 39
  • 41
42

In short:

  1. Install WAMP

  2. Put this file to C:\wamp\www\ProjectName\filename.php

  3. Go to browser: http://localhost/ProjectName/filename.php

SlavaNov
  • 2,447
  • 1
  • 18
  • 26
11

I just put the content in the question in a file called test.php and ran php test.php. (In the folder where the test.php is.)

$ php foo.php                                                                                                                                                                                                                                                                                                                                    
15
user3603181
  • 111
  • 1
  • 2
5

If you have apache running, put your file in server folder for html files and then call it from web-browser (Like http://localhost/myfile.php ).

ProdoElmit
  • 1,067
  • 9
  • 22
  • 6
    it is not working. Apache is running & i placed my sample.php in htdocs then tried, http://localhost/sample.php but it shows only code. – logan Dec 20 '11 at 19:07
0

Running PHP script directly in browser:

Here are all steps (in short) to run PHP program in XAMPP

Step 1: First of all, open the Apache Friends website, The download and install XAMPP for Windows.

Step 2: Open the XAMPP Program Control Panel and start “Apache Web Server”. (NB: If your PHP scripts need MySQL database to work, Start “MySQL” service as well)

Step 3: Create a new folder inside “htdocs” folder. For example: “php-project”

Path looks like: C:/xampp/htdocs/php-project/

Step 4: Open any text editor and create a new file and write a simple PHP program then Save it with .php extension as “example.php”.

Path looks like: C:/xampp/htdocs/php-project/example.php

<?php
echo “Hello Level 5 Developers”;
?>

Step 5: Go to the browser and type “localhost/php-project/example.php” in the address bar and press enter then it gets executed.

Step 6: After that, you can see the output of your first PHP program like this:

Hello Level 5 Developers

Gadrawin
  • 87
  • 1
  • 7
-1

3 easy steps to run your PHP program is:

  1. The easiest way is to install MAMP!

  2. Do a 2-minute setup of MAMP.

  3. Open the localhost server in your browser at the created port to see your program up and runing!