PHP CLI


PHP CLI Interview with follow-up questions

1. What is PHP CLI and what are its uses?

PHP CLI (Command Line Interface) is the PHP runtime invoked directly from a terminal, without a web server. It produces output to stdout rather than to an HTTP response.

Common uses:

  • Artisan / console commands — Laravel's php artisan and Symfony's php bin/console are PHP CLI programs. Most modern frameworks expose application management this way.
  • Cron jobs and scheduled tasks — long-running or periodic background tasks (database cleanup, sending queued emails, generating reports).
  • Queue workersphp artisan queue:work processes job queues in the background; this is how Laravel Horizon and similar tools operate.
  • Data import/export — ETL scripts, CSV ingestion, and database seeding.
  • Build and deployment scripts — code generation, cache warming, migration running.
  • Interactive REPL — tools like PsySH provide a PHP REPL for debugging and exploration.
  • Unit test runners — PHPUnit runs entirely in the CLI.

Behavioral differences from web PHP:

  • No $_GET, $_POST, or $_SERVER HTTP variables (except a few server-level ones).
  • php.ini directives like max_execution_time default to 0 (unlimited) in CLI mode.
  • Output buffering is off by default.
  • Error messages go to stderr.
↑ Back to top

Follow-up 1

Can you explain how to execute a PHP script using CLI?

To execute a PHP script using CLI, you need to open a command prompt or terminal and navigate to the directory where the PHP script is located. Then, you can use the php command followed by the name of the script file to run it. For example, if your script is named script.php, you can run it using the following command:

php script.php

Follow-up 2

What is the difference between PHP CLI and PHP CGI?

PHP CLI and PHP CGI are both command line tools for running PHP scripts, but they have some differences:

  • PHP CLI is designed for running PHP scripts directly from the command line, while PHP CGI is designed for running PHP scripts as CGI (Common Gateway Interface) programs.
  • PHP CLI is typically used for command line scripting and automation, while PHP CGI is typically used for running PHP scripts in a web server environment.
  • PHP CLI provides a more lightweight and efficient way to run PHP scripts, while PHP CGI requires the overhead of a web server.
  • PHP CLI can be used to execute PHP scripts without the need for a web server or a web browser, while PHP CGI requires a web server to handle the HTTP requests.

Follow-up 3

How can you pass arguments to a PHP script using CLI?

You can pass arguments to a PHP script using CLI by specifying them after the script name when running the script. The arguments can be accessed within the script using the $argv array. The first argument, $argv[0], contains the name of the script itself, while subsequent arguments can be accessed using $argv[1], $argv[2], and so on. Here's an example:


You can then run the script with arguments like this:

php script.php argument1 argument2

2. How can you check the PHP CLI version installed on your system?

Run the following command in your terminal:

php -v

Sample output:

PHP 8.4.1 (cli) (built: Nov 21 2024 10:00:00) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.1, Copyright (c), by Zend Technologies

What to note in the output:

  • The version number (e.g., 8.4.1)
  • NTS (Non-Thread Safe) vs ZTS (Zend Thread Safe) — NTS is standard for most setups
  • Whether OPcache is compiled in

If you have multiple PHP versions installed (common with tools like phpenv, asdf, or on macOS via Homebrew), the active version depends on your $PATH. Check which binary is being used with:

which php
php -v

To check a specific version binary:

/usr/bin/php8.4 -v
↑ Back to top

Follow-up 1

What command would you use to update the PHP CLI version?

To update the PHP CLI version, you would need to update your PHP installation. The specific command to update PHP depends on the package manager you are using.

For example, if you are using apt package manager on Ubuntu, you can use the following command to update PHP:

sudo apt update
sudo apt upgrade php-cli

If you are using Homebrew on macOS, you can use the following command to update PHP:

brew update
brew upgrade php

Please note that the exact command may vary depending on your operating system and package manager.

Follow-up 2

How can you switch between different PHP CLI versions?

To switch between different PHP CLI versions, you can use a version manager like phpbrew or Docker.

  1. Using phpbrew:

    • Install phpbrew by following the instructions in the official documentation.
    • Once installed, you can list the available PHP versions by running phpbrew known.
    • To switch to a specific PHP version, use the command phpbrew use. For example, phpbrew use 7.4.0.
  2. Using Docker:

    • Install Docker on your system if you haven't already.
    • Pull the desired PHP version image from Docker Hub using the command docker pull php:. For example, docker pull php:7.4.0.
    • Run a container using the pulled image with the command docker run -it php:. For example, docker run -it php:7.4.0.

These are just two examples of how you can switch between different PHP CLI versions. There are other tools and methods available depending on your specific requirements.

3. What are the common PHP CLI commands that you use frequently?

Frequently used PHP CLI flags and commands:

php -v                          # Show PHP version
php -m                          # List loaded extensions/modules
php -i                          # Print phpinfo() output (configuration dump)
php -i | grep "memory_limit"    # Search configuration for a specific directive
php -r 'echo PHP_VERSION;'      # Execute a single PHP expression
php script.php                  # Run a PHP file
php -l script.php               # Lint (syntax-check) a file without executing it
php -S localhost:8000            # Start the built-in development web server
php -S localhost:8000 -t public/ # Serve from a specific document root
php artisan migrate              # Run Laravel migrations (Artisan uses PHP CLI)
php bin/console cache:clear      # Symfony console command
./vendor/bin/phpunit             # Run PHPUnit test suite
./vendor/bin/phpstan analyse src/ # Static analysis with PHPStan

Interview context: Interviewers working with Laravel or Symfony will expect you to know php artisan and php bin/console commands. Being familiar with php -l for CI linting and php -S for quick local testing is also valued.

↑ Back to top

Follow-up 1

Are there any commands specific to PHP CLI that are not available in other interfaces?

Yes, there are some commands specific to PHP CLI that are not available in other interfaces. For example:

  • php -a: This command starts an interactive shell where you can enter and execute PHP code interactively.
  • php -l: This command is used to check the syntax of a PHP script without executing it.
  • php -S localhost:8000: This command starts a built-in web server for testing PHP scripts locally.
  • php -d =: This command allows you to override PHP configuration settings from the command line.

Follow-up 2

Can you explain the purpose of each command?

  • php -v: This command is used to check the version of PHP installed on the system.
  • php -m: This command lists all the installed PHP modules.
  • php -i: This command displays the PHP configuration information.
  • php -r 'echo "Hello, World!";': This command allows you to execute a single line of PHP code.
  • php: This command is used to execute a PHP script from the command line.

4. How can you run a PHP script in the background using PHP CLI?

To run a PHP script in the background on Unix/Linux systems, redirect output and use & to detach the process:

nohup php /path/to/script.php > /var/log/myscript.log 2>&1 &
  • nohup prevents the process from being killed when the terminal session ends
  • > /var/log/myscript.log redirects stdout to a log file
  • 2>&1 merges stderr into stdout
  • & sends the process to the background; the shell immediately returns with the process ID (PID)

Check the running process:

ps aux | grep script.php

Modern alternatives preferred in production:

  1. Supervisor — a process manager that keeps PHP workers running, restarts them on failure, and manages log rotation. Used commonly with Laravel queue workers (php artisan queue:work).

  2. Systemd service — define a .service unit to manage the PHP process as a system daemon with proper start/stop/restart semantics.

  3. Cron jobs — for scheduled tasks, use crontab:

    * * * * * /usr/bin/php /path/to/script.php >> /var/log/script.log 2>&1
    
  4. Framework task schedulers — Laravel's php artisan schedule:run called by a single cron entry manages all scheduled tasks in code.

In production, nohup & is a quick workaround; Supervisor or systemd is the correct tool for persistent background processes.

↑ Back to top

Follow-up 1

What are the advantages of running a script in the background?

Running a script in the background has several advantages:

  1. Non-blocking: When a script is run in the background, it does not block the execution of other scripts or processes. This allows you to continue working on other tasks while the script is running.

  2. Long-running tasks: Background scripts are useful for tasks that take a long time to complete, such as data processing, file conversion, or sending emails. You can start the script and let it run in the background without having to keep the terminal open.

  3. Automation: Background scripts can be scheduled to run at specific times or intervals using cron jobs or task schedulers. This allows you to automate repetitive tasks without manual intervention.

  4. Improved performance: By running a script in the background, you can utilize system resources more efficiently. For example, you can run multiple instances of the script simultaneously to process data in parallel.

Follow-up 2

How can you check the status of a background process?

To check the status of a background process, you can use the ps command followed by the process ID (PID) of the script. Here's an example:

ps -p 

Replace `` with the actual process ID of the script. This command will display information about the process, including its status, CPU usage, memory usage, and more.

Alternatively, you can use the pgrep command to find the process ID of a script by its name. Here's an example:

pgrep -f /path/to/your/script.php

This command will return the process ID of the script if it is running. If the script is not running, it will not return any output.

5. Can you explain how error reporting works in PHP CLI?

In PHP CLI, error output goes to stderr by default, and errors are displayed in the terminal rather than returned in an HTTP response.

Controlling error reporting in CLI scripts:

↑ Back to top

Follow-up 1

How can you customize the level of error reporting in PHP CLI?

To customize the level of error reporting in PHP CLI, you can use the error_reporting directive in the PHP configuration file (php.ini) or set it programmatically using the error_reporting() function.

In the PHP configuration file (php.ini), you can set the error_reporting directive to a specific error reporting level. For example, to display only fatal errors, you can set it to E_ERROR.

Alternatively, you can use the error_reporting() function in your PHP script to set the error reporting level programmatically. For example, to display all types of errors except notices, you can use the following code:

error_reporting(E_ALL & ~E_NOTICE);

By customizing the error reporting level, you can control which types of errors are displayed and which ones are ignored.

Follow-up 2

How does error reporting in PHP CLI differ from error reporting in a web server environment?

Error reporting in PHP CLI differs from error reporting in a web server environment in a few ways:

  1. Displayed Output: In PHP CLI, error messages and warnings are displayed directly in the command line interface, while in a web server environment, they are typically logged to a file or displayed in the browser.

  2. Configuration: Error reporting in PHP CLI is controlled by the PHP configuration file (php.ini) or programmatically using the error_reporting() function. In a web server environment, error reporting is typically configured through the web server configuration or using directives in the PHP script.

  3. Error Types: PHP CLI displays all types of errors by default, including notices, warnings, and fatal errors. In a web server environment, the error reporting level can be customized to display specific types of errors.

Overall, error reporting in PHP CLI provides a convenient way to quickly identify and debug issues when running PHP scripts from the command line.

Live mock interview

Mock interview: PHP CLI

Intermediate ~5 min Your own free AI key

Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.