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.

11
  • 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, 2011 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, 2011 at 18:39
  • 13
    Install just php and in command line : php -f path/to/file.php
    – piotrekkr
    Dec 20, 2011 at 18:40
  • 2
    May be worth your time to look in to WAMP (If you're running windows) or LAMP for *nix Dec 20, 2011 at 18:40
  • 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 3, 2015 at 21:02

7 Answers 7

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

2
  • 1
    installed php with sudo apt install php7.2-cli on ubuntu 18.04.
    – monkut
    Aug 16, 2019 at 1: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 3, 2021 at 1: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.

1
  • 2
    @Bhaxy, Is XAMPP portable?
    – Pacerier
    Feb 3, 2015 at 15:45
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

0
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
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 ).

1
  • 6
    it is not working. Apache is running & i placed my sample.php in htdocs then tried, localhost/sample.php but it shows only code.
    – logan
    Dec 20, 2011 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

-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!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.