Home >Backend Development >PHP Problem >What are the three ways to pass parameters to php?

What are the three ways to pass parameters to php?

青灯夜游
青灯夜游Original
2021-11-29 13:53:496150browse

Three ways to pass parameters to php: 1. Use the "$argv" or "$argc" variable to pass parameters; 2. Use the getopt() function to pass in parameters, the syntax "getopt(' a:b:')"; 3. Use fwrite() and fgets() to pass in parameters through user input data.

What are the three ways to pass parameters to php?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Three ways to pass in parameters in php

1. Use $argv or $argc parameters to receive

<?php
/**
 * 使用 $argc $argv 接受参数
 */
echo "接收到{$argc}个参数";
print_r($argv);

The following are the results of the test

2. Use getopt function (this method is recommended)

<?php
/**
 * 使用 getopt函数
 */
$param_arr = getopt(&#39;a:b:&#39;);
print_r($param_arr);

##3

. Input data through the user

<?php
/**
 * 提示用户输入,类似Python
 */
fwrite(STDOUT,'please input:');
echo 'your input is:'.fgets(STDIN);
The user’s input cannot be empty

<?php
/**
 * 提示用户输入,类似Python
 */

$fs = true;

do{
    if($fs){
        fwrite(STDOUT,'请输入您的博客名:');
        $fs = false;
    }else{
        fwrite(STDOUT,'抱歉,博客名不能为空,请重新输入您的博客名:');
    }

$name = trim(fgets(STDIN));

}while(!$name);

echo '您输入的信息是:'.$name."\r\n";
Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of What are the three ways to pass parameters to php?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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