Run PHP from the command line
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
Related videos on Youtube
sud_the_devil
Updated on September 18, 2022Comments
-
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 about 10 years@sud_the_devil: Just edited my answer.
-
sud_the_devil about 10 yearsI 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 about 10 yearsNo, you don't need to restart your system. Can you paste the output of
echo $PATH
after you have added the path and done asource /etc/environment
andls /opt/lampp/bin/php
? -
sud_the_devil about 10 yearsThis is the output of echo $PATH: /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games And this is the output of ls /opt/lampp/bin/php: /opt/lampp/bin/php
-
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 about 10 years@sud_the_devil Add only
/opt/lampp/bin
, not/opt/lampp/bin/php
which is the executable file. -
sud_the_devil about 10 years@ Srihari @RaduRădeanu Thank you guys, my bad ... It works now
-
Wutaz about 10 years
php5-cli
is in a different package. -
Radu Rădeanu about 10 yearsThe OP problem is: I don't know how to set environment variable for php which is located at /opt/lampp/bin/php .
-
e-sushi almost 10 years@Wutaz On what planet? (hint)
-
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 almost 7 yearsDoesn't work. Instead of execution a script it just prints file content like
cat
does -
Garri Sumalapao Farol almost 7 yearsNo, wrong.It just prints file content like
cat
does. No execution -
RN Kushwaha over 6 years@Green It seems that you don't have proper set up of php on your system.
-
Garri Sumalapao Farol over 6 yearsIt seems that you have to type
-f
betweenphp
and filename to make it work =>php -f file.php