How do PHP read the command line entry as a string?

Asked

Viewed 6,534 times

1

In the Python, I know that to read the data entered by the command line we use the function raw_input.

Example:

Python script:

print 'digite algo para inicializar'

resultado = raw_input()

print 'O resultado é ' + resultado

Command line:

> python script.py
digite algo para inicializar
> teste
o resultado é teste

What about PHP? How to make PHP read the command line entry?

Observing: I want to capture the input not on the script startup, but "in the middle" of it, as in the python, demonstrated above.

2 answers

2

For data entry in php use "argv": $argv[0].

Follow an example for entering a single parameter:

<?php

if ($argc != 2 || in_array($argv[1], array('texto1', 'texto 2', 'texto3', 'texto4'))) {
     echo $argv[0]; 
} else {
     echo $argv[1];
}

 ?>

http://php.net/manual/en/reserved.variables.argv.php

Here are more details about the command line: http://php.net/manual/en/features.commandline.php

And here are some examples of usage: https://stackoverflow.com/questions/11048835/php-pass-parameters-from-command-line-to-a-php-script

  • -1. This approach is used for parameters passed at script initialization. In this case, we are talking about the reading that occurs with the script already initialized.

  • argv entries can be used at any stage of the script. It is an array variable reserved for inputs.

  • Note: I want to capture the input not on the script startup, but "in the middle" of it, as in the python example shown above.

  • As I said, it can be used at any stage... it is independent of the method for recerber inputs.

  • Ivan, I changed the question, since apparently it wasn’t clear what I wanted. I know the $argv can be used, but are parameters passed to the script execution. It does not "take" the captured values as entries (as in the case of a "Do you want to delete the files? [y|n]". I want this capture to occur after the script is initialized, and not passing parameters.

0


This can be done through the function fgets combined with the constant STDIN.

Behold:

$line = fgets(STDIN);

PHP script

echo "iniciando a aplicação php\n";

$line = fgets(STDIN);

echo "O resultado é '{$line}'";

inserir a descrição da imagem aqui

See the response taken from SOEN

Despite fgets be used in most cases to read file pointers, PHP supports protocols and Wrappers, through these functions.

In this case, we are reading the command line input as if it were a file line.

See the code working on IDEONE

  • The fgets function does not work for command line inputs.

  • I guess you didn’t test the code, because for me it worked correctly.

  • His own answer proves that fgets works. Maybe you haven’t seen that on the page where you linked in your reply

  • @Wallacemaxters I think what they meant by "by the command line" was, Pex, this: php cmd.php arg1 arg2 arg3 however, that’s not what the user wants (or at least, that’s not what I understood) and your answer is awesomesauce :)

  • http://www.urbandictionary.com/define.php?term=Awesomesauce

  • I don’t take the quality of the method fgets, I also think it’s great to use in a question in PHP, but the question is how to do it via command line, if you know the answer, I give you my full support. I’m not here to disqualify, just to contribute.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.