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?

青灯夜游
Release: 2023-03-14 07:56:02
Original
6149 people have browsed it

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);
Copy after login

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);
Copy after login

##3

. Input data through the user

<?php
/**
 * 提示用户输入,类似Python
 */
fwrite(STDOUT,&#39;please input:&#39;);
echo &#39;your input is:&#39;.fgets(STDIN);
Copy after login

The user’s input cannot be empty

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

$fs = true;

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

$name = trim(fgets(STDIN));

}while(!$name);

echo &#39;您输入的信息是:&#39;.$name."\r\n";
Copy after login
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!

Related labels:
php
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template