How to run php script linux

In PHP, there is a provision for one to successfully execute PHP codes on the command prompt and terminal. To do this, you should do the following.

  1. You must have successfully installed PHP, either stand-alone or in a bundle, and it must be working. To check if it is installed and is the right version, change the directory into the folder where you installed it, like c:/php, and use the following command on your terminal or cmd:
php -v

If you don’t have PHP installed, you can install it as shown below.

On Windows

  • Download it from the official site.
  • Extract the file and store it in a folder, preferably named php, in your C just like so: c:/php
  • Next, place this folder in your environment variable path so that it can be recognized by the command line.

On Linux

To install on Linux, use the command:

apt-get install php5-common libapache2-mod-php5 php5-cli

You can change the php5 to your version of choice.

On MAC

You can run the command below on your terminal to install PHP.

curl -s https://php-osx.liip.ch/install.sh | bash -s 7.3

Now that you have PHP installed and running, we can execute our scripts on the command line or terminal.

Executing your scripts

  • First, you create a PHP file like this one:

file_name.php, prog1.php

  • Next, head to your terminal or command prompt and change the directory into the folder containing your PHP files. Let’s say something like this:

D:\php_project

  • Lastly, enter the following command into your cmd or terminal to run the script file named prog1.php:
php prog1.php

The complete code to run our sample script will be as follows.

D:\php_project\php prog1.php

And with that, you have successfully run your PHP script on the command line or terminal, depending on which you use.

Let’s see the image of the execution of my sample prog1.php script on my cmd.

How to run php script linux

One last thing: you can also start your development server on the cmd and see your code run on the browser. To do that, enter this code in your command line or terminal.

php -S localhost:port -t your_folder/

Where your_folder stands for the folder containing the php code to be executed.

You should have something like the image below if you have your browser and cmd opened up.

CONTRIBUTOR

How to run php script linux
NDUKWE CHIDERA K.

Home / Running PHP scripts as shell scripts

As well as running PHP scripts via a web server such as Apache, you can also run PHP scripts from the command line (it’s also possible to write GUI apps with PHP using PHP-GTK, but I’ll maybe look at that another time). This post looks at the two ways you can run a PHP script from a *nix based computer (Linux, UNIX, BSD, OSX) and the single way you can do it from Windows.

Using a shebang

UNIX shell scripts are set as executable and the first line in the file contains a “shebang” line which shows the path to the executable which will parse the script.

If your distro’s PHP binary is at e.g. /usr/bin/php you would add this to the first line of your script:

#!/usr/bin/php

and then have your PHP script under it.

To run a “hello world script” you’d do this:

#!/usr/bin/php
<?php
  echo "hello worldn";

The script then needs to be marked as executable. To set it so it can be run as anyone do this:

chmod 0755 /path/to/script.php

To make it so only you can run it (and so no one else can even read it) do this:

chmod 0700 /path/to/script.php

Read the chmod manpage for more details about file permissions if you don’t know about them. I have a copy of the chmod manpage here.

Calling the PHP binary, passing the script filename

To avoid permission issues, or to run the PHP script from the command line on Windows (the above method does not work on Windows unless run via cygwin), you need to execute the PHP binary passing the script filename as the parameter.

If you are in the same directory as the script, the script is named e.g. myscript.php, and the PHP binary is at /usr/bin/php you would do this to run it:

/usr/bin/php myscript.php

If the PHP binary is in your executable path (it usually will be on most Linux distros) then you should just need to run “php” and then the script:

php myscript.php

If you’re running it from cron it’s always a good idea to put the full path to both:

/usr/bin/php /path/to/myscript.php

When using this method you don’t need to have the shebang line at the top of the script.

An example of doing this on Windows:

c:phpphp.exe c:pathtomyscript.php

Conclusion

It’s easy to run a PHP script from the command line, which is useful for batch processing or database cleanup for your website. Combined with automatic processing via the cron system and you have a powerful tool at your disposal if you are a PHP programmer and/or your website is programmed in PHP.

How do I run a PHP script in Linux terminal?

You just follow the steps to run PHP program using command line..
Open terminal or command line window..
Goto the specified folder or directory where php files are present..
Then we can run php code using the following command: php file_name.php..

How do I run a PHP file?

php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.

How do I start a PHP script?

Run Your First PHP Script.
Go to XAMPP server directory. I'm using Windows, so my root server directory is “C:\xampp\htdocs\”..
Create hello.php. Create a file and name it “ hello.php “.
Code Inside hello. php. ... .
Open New Tab. Run it by opening a new tab in your browser..
Load hello.php. ... .
Output. ... .
Create a Database. ... .
Create a Table..

How do I run PHP in bash?

You can execute PHP from the command line in 2 ways:.
php yourfile. php..
php -r 'print("Hello world");'.