Home > Backend Development > PHP Tutorial > How Do I Pass Variables to PHP Scripts From the Command Line?

How Do I Pass Variables to PHP Scripts From the Command Line?

Linda Hamilton
Release: 2024-11-17 13:10:01
Original
490 people have browsed it

How Do I Pass Variables to PHP Scripts From the Command Line?

Using PHP from the Command Line: Passing Variables to Scripts

Running PHP scripts from the command line can be a convenient way to automate tasks. However, passing variables to these scripts can be confusing. This article will explain how to effectively pass variables to PHP scripts invoked from the command line.

The Issue

When attempting to pass variables using the URL query string format, you may encounter an error like this: "Could not open input file: myfile.php?type=daily." This is because the query string format is only valid for web-accessed pages.

解决方案

To pass variables to PHP scripts from the command line, you'll need to utilize the $argv array. Here's how:

1. Retrieve Variables from $argv

When running a PHP script from the command line, the parameters passed to it are stored in the $argv array. The first element ($argv[0]) is the script's filename, while subsequent elements contain the parameters. To retrieve the value of a specific parameter, use its index in the array. For example, to get the value of the "type" parameter, you would use $argv[1].

Code:

<?php
// Check if the script was called from the command line
if (defined('STDIN')) {
  // Get the type parameter from $argv
  $type = $argv[1];
}
?>
Copy after login

2. Handling Web Access as Well

If the script is also used as a web page, you can check whether it's called from the command line or not and handle the variable passing accordingly:

<?php
if (defined('STDIN')) {
  // Running from command line
  $type = $argv[1];
} else {
  // Running as a web page
  $type = $_GET['type'];
}
?>
Copy after login

3. Using Wget or a Shell Script

If you want to access the script using a URL-like syntax while running it from the command line, you can use a shell script or Wget to execute the query remotely:

Shell Script:

#!/bin/sh
wget http://location.to/myfile.php?type=daily
Copy after login

By following these methods, you can effectively pass variables to PHP scripts running from the command line, enabling you to automate tasks and process data efficiently.

The above is the detailed content of How Do I Pass Variables to PHP Scripts From the Command Line?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template