Posted on
Web Development

Running PHP scripts from the command line

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Comprehensive Guide to Running PHP Scripts from the Command Line for Web Developers

Introduction

As web technologies continue to evolve at a rapid pace, familiarizing oneself with the underlying tools that power the web becomes crucial. For developers working in PHP (which still powers a major chunk of the web), learning to run PHP scripts from the Linux Bash terminal is an essential skill. It not only enhances productivity but also opens up a new dimension of possibilities for testing, debugging, and automating web applications.

In this comprehensive guide, we will explore the nuances of executing PHP scripts directly from the command line, a skill beneficial for both beginner and seasoned web developers. This ability is particularly useful when working on server-side scripts, cron jobs, or even when doing simple tests without the overhead of a web server.

Setting up Your Environment

Before diving into the command line, it's important to ensure you have PHP installed on your Linux system. Most modern Linux distributions come with PHP readily available, and it can easily be installed via the package manager. For Ubuntu systems, you can install PHP using:

sudo apt-get update
sudo apt-get install php-cli

For CentOS and RHEL systems using dnf or yum, perform:

sudo dnf install php-cli

And for openSUSE using zypper:

sudo zypper install php7

To confirm the installation, run:

php -v

This command should output the installed version of PHP. Ensure that php-cli (Command Line Interface) is particularly mentioned or installed, as it is specifically designed for running scripts directly in the command line.

Running a Basic PHP Script

Begin by creating a simple PHP file. Open your text editor and type the following:

<?php
echo "Hello, this is my first CLI script!";
?>

Save this file as hello.php. To run this script from the terminal, navigate to the directory containing your script and type:

php hello.php

This command should output:

Hello, this is my first CLI script!

Exploring PHP CLI Options

The PHP CLI offers various options that can be utilized to make scripting easier. Some of the useful command-line options are:

  • -f: Specifies the file to run.

  • -r: Run PHP code directly from the command line without tags <?php ... ?>.

  • -i: Displays PHP information identical to the phpinfo() function.

  • -a: Runs PHP interactively.

To run a single line of PHP code without creating a file, you can use:

php -r 'echo strtoupper("hello world");'

This will output HELLO WORLD.

Interactive Shell

If you want to test PHP snippets rapidly without executing full scripts, PHP’s interactive shell mode is extremely useful. Simply type:

php -a

You will enter an interactive mode where you can type PHP code directly and see the output immediately.

Running Scripts with Arguments

Passing arguments to scripts is straightforward. Modify your hello.php to the following:

<?php
if ($argc > 1) {
    echo "Hello, " . $argv[1];
} else {
    echo "Hello, world!";
}
?>

Here, $argc counts the arguments, and $argv is an array containing them. You can run this script with an argument like so:

php hello.php John

It will output:

Hello, John

Scheduling Scripts with Cron

Cron jobs are perfect for scheduling scripts to run at specific intervals. Open the crontab with:

crontab -e

Add a line like the following to run your PHP script every day at midnight:

0 0 * * * /usr/bin/php /path/to/your/hello.php

Be sure to replace /path/to/your/hello.php with the actual path to your PHP script.

Conclusion

Running PHP scripts from the command line is a powerful technique that every web developer should be comfortable with. By mastering these skills, you can enhance your testing, perform maintenance tasks, automate actions, and much more. Linux, combined with PHP CLI, provides a robust environment for developing and testing real-world web applications efficiently. Happy scripting!


Whether you're honing old skills or diving into new tools, mastering the command line is a step forward in your development career. Continue exploring, learning, and creating!

Further Reading

For further reading on running PHP scripts from the command line and related topics, you might find the following resources helpful:

These resources will help broaden your understanding and enhance your skills in managing PHP scripts via the command line.