Run PHP from the command line

193,446

Solution 1

Environment variables are set in /etc/environment. You will find the $PATH variable in this file. This variable stores the path to binaries in various locations.

To add /opt/lampp/bin to the location searched for binary files, just append this path preceded by a : to the path variable.

For example, if the $PATH variable was:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

add /opt/lampp/bin to the end of it, so that it becomes:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/lampp/bin

After doing this, do a source /etc/environment.

Solution 2

To open an interactive php shell, just type in a terminal:

php -a

As for opening a file, just:

php filename.php

Solution 3

As an alternative to /opt/lampp/bin/php, to run a php script from the command line, you just need to install php5-cli:

sudo apt-get install php5-cli

And run your script with:

php myscript.php

editor's note: depending on your version, you may need to install php7.0-cli etc instead

Share:
193,446

Related videos on Youtube

sud_the_devil
Author by

sud_the_devil

Updated on September 18, 2022

Comments

  • sud_the_devil
    sud_the_devil over 1 year

    I have installed XAMPP v1.8.3 for my PHP development. I am new to Ubuntu, so I don't know how to set environment variable for PHP which is located at /opt/lampp/bin/php.

    I can run PHP scripts from localhost just fine, but I wanted to run them from the command line as well.

    I want to set this variable for every user, since I am the only one who uses this system.

  • jobin
    jobin about 10 years
    @sud_the_devil: Just edited my answer.
  • sud_the_devil
    sud_the_devil about 10 years
    I opened the file by issuing sudo subl /opt/lampp/bin/php and then appended the location as you asked me to do . Here's the link to the opened file. PATH Do I need to restart my system after that??
  • jobin
    jobin about 10 years
    No, you don't need to restart your system. Can you paste the output of echo $PATH after you have added the path and done a source /etc/environment and ls /opt/lampp/bin/php?
  • sud_the_devil
    sud_the_devil about 10 years
    This is the output of echo $PATH: /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr‌​/sbin:/usr/bin:/sbin‌​:/bin:/usr/games:/us‌​r/local/games And this is the output of ls /opt/lampp/bin/php: /opt/lampp/bin/php
  • Srihari
    Srihari about 10 years
    @sud_the_devil you are required to edit the environment file and not the php executable. Your command should be sudo subl /etc/environment
  • Radu Rădeanu
    Radu Rădeanu about 10 years
    @sud_the_devil Add only /opt/lampp/bin, not /opt/lampp/bin/php which is the executable file.
  • sud_the_devil
    sud_the_devil about 10 years
    @ Srihari @RaduRădeanu Thank you guys, my bad ... It works now
  • Wutaz
    Wutaz about 10 years
    php5-cli is in a different package.
  • Radu Rădeanu
    Radu Rădeanu about 10 years
    The OP problem is: I don't know how to set environment variable for php which is located at /opt/lampp/bin/php .
  • e-sushi
    e-sushi almost 10 years
    @Wutaz On what planet? (hint)
  • Martin Thoma
    Martin Thoma about 9 years
    @darent Thanks. This was the first search result I got from Google and php -a was exactly what I looked for.
  • Garri Sumalapao Farol
    Garri Sumalapao Farol almost 7 years
    Doesn't work. Instead of execution a script it just prints file content like cat does
  • Garri Sumalapao Farol
    Garri Sumalapao Farol almost 7 years
    No, wrong.It just prints file content like cat does. No execution
  • RN Kushwaha
    RN Kushwaha over 6 years
    @Green It seems that you don't have proper set up of php on your system.
  • Garri Sumalapao Farol
    Garri Sumalapao Farol over 6 years
    It seems that you have to type -f between php and filename to make it work => php -f file.php