Table of Contents
Use the getopt() function
use the $argv variable
Home Backend Development PHP Tutorial How to make a PHP script accept input of options and values ​​when executed in cli mode

How to make a PHP script accept input of options and values ​​when executed in cli mode

Jul 30, 2016 pm 01:31 PM
php test

Users who are used to Linux systems should know that many commands in Linux support the input of options and values, such as rm -f hello.txt, ls -al, netstat -tnl, etc. So what? How to make PHP scripts also support the input of options and values ​​when executed in cli mode? There are two methods:

Use the getopt() function

Suppose now you need a PHP script to support the input of three options:
-a: Do not accept value input
-b: accept value input
-c: Accept value input, and the value is optional

According to the above requirements, you can write a PHP script:

<code><span><span>function</span><span>main</span><span>(<span>$args</span>)</span> {</span>
    var_dump(<span>$args</span>);
}

<span>/*
 * 选项字符后面不带冒号,表示该选项不支持值输入
 * 选项字符后面带一个冒号,表示该选项支持值输入
 * 选项字符后面带两个冒号,表示该选项支持值输入,且值可选
 */</span><span>$args</span> = getopt(<span>'ab:c::'</span>);
main(<span>$args</span>);</code>
Copy after login

Then use the command to execute the script. Here, Linux is used as an example, and assuming the script is named test.php, note. No need for -f option between /php and test.php:
./php test.php -a -b=valueOfB -c=valueOfC

Output:
How to make a PHP script accept input of options and values ​​when executed in cli mode

because the value of c option is optional , let’s try what happens if option c does not pass a value:
How to make a PHP script accept input of options and values ​​when executed in cli mode
As you can see, the array value corresponding to the option without a value is false

One thing to note here is that the options and values ​​are connected using the equal sign "=", or you can omit the equal sign, and you cannot use spaces. to separate options and values, such as -b valueOfB, otherwise the value may not be obtained.

getopt() also supports options starting with two dashes, such as --a. For details, please refer to the second parameter $longopts of getopt()

use the $argv variable

if only If you simply want to pass some values ​​to the script, you can use the variable $argv. For detailed information about this variable, please refer to: http://php.net/manual/zh/reserved.variables.argv.php

PHP script:

<code>var_dump(<span>$argv</span>);</code>
Copy after login

Execute command./php test.php valueOfA valueOfB valueOfC

Output:
How to make a PHP script accept input of options and values ​​when executed in cli mode

Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.

The above introduces how to make the PHP script accept input of options and values ​​when executed in cli mode, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

Huawei Watch GT 5 smartwatch gets update with new features Huawei Watch GT 5 smartwatch gets update with new features Oct 03, 2024 am 06:25 AM

Huawei Watch GT 5 smartwatch gets update with new features

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

PHP Program to Count Vowels in a String

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

How do you parse and process HTML/XML in PHP?

Top 10 PHP CMS Platforms For Developers in 2024 Top 10 PHP CMS Platforms For Developers in 2024 Dec 05, 2024 am 10:29 AM

Top 10 PHP CMS Platforms For Developers in 2024

How to Add Elements to the End of an Array in PHP How to Add Elements to the End of an Array in PHP Feb 07, 2025 am 11:17 AM

How to Add Elements to the End of an Array in PHP

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

7 PHP Functions I Regret I Didn't Know Before

See all articles