Several ways to pass in parameters when running in php command line mode
Oct 14, 2016 am 10:20 AMURL method
<strong>$param = array(); if ($argc > 1) { parse_str ( $argv [1], $param ); foreach ( $param as $k => $v ) { echo "define $k=$v\n"; $param[$k] = $v; } } print_r($param); //運(yùn)行示例 php sync_t_search.php "name=sjk&city=bj"</strong>
2. System built-in variables
<strong>//當(dāng)傳入多個(gè)或多個(gè)參數(shù)可選時(shí)有很大的局限性 $argv #0為當(dāng)前執(zhí)行的文件名 $argc #參數(shù)個(gè)數(shù)</strong>
3. System function getopt
<strong>//1、個(gè)人總結(jié):如果只傳一個(gè)參數(shù),:(必選)/::(可選)都可 //2、如果傳多個(gè)參數(shù)則要區(qū)分可選必選,建議盡量用可選參數(shù)并且后面的值不要傳入空格 //3、可選參數(shù):標(biāo)識(shí)符存在且有值時(shí),下標(biāo)才會(huì)存在 //4、必選參數(shù):只要標(biāo)識(shí)符存在,下標(biāo)就會(huì)存在(如果取不到值為false) /** * php song.php -h"a b" || php song.php -h"a b" 不受空格影響(建議保持一致都不加空格) array(1) { ["h"]=> string(3) "a b" } php song.php -h -a array(1) { ["h"]=> string(2) "-a" } */ var_dump(getopt('h:a::')); //必選參數(shù),如果參加之間有空格要加引號(hào), 選項(xiàng)后面有空格也不會(huì)影響取值,會(huì)把他后面的值都當(dāng)做參數(shù)的值 /** php song.php -asong array(1) { ["a"]=> string(4) "song" } */ var_dump(getopt('a::h:')); //可選參數(shù),參數(shù)必須緊跟在選項(xiàng)后面,否則獲取不到</strong>
4. System function getopt
<strong>//標(biāo)準(zhǔn)輸出 fwrite(STDOUT, "Enter your name: "); echo "\n"; //標(biāo)準(zhǔn)輸入 $name = trim(fgets(STDIN)); fwrite(STDOUT, "Hello, $name!");</strong>
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 AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Grass Wonder Build Guide | Uma Musume Pretty Derby
1 months ago
By Jack chen
Roblox: 99 Nights In The Forest - All Badges And How To Unlock Them
4 weeks ago
By DDD
Uma Musume Pretty Derby Banner Schedule (July 2025)
1 months ago
By Jack chen
RimWorld Odyssey Temperature Guide for Ships and Gravtech
3 weeks ago
By Jack chen
Windows Security is blank or not showing options
1 months ago
By 下次還敢

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)