PHP Cli Getting Input from User and Then Dumping into Variable Possible

PHP cli getting input from user and then dumping into variable possible?

Have a look at this PHP manual page
http://php.net/manual/en/features.commandline.php

in particular

<?php
echo "Are you sure you want to do this? Type 'yes' to continue: ";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
if(trim($line) != 'yes'){
echo "ABORTING!\n";
exit;
}
echo "\n";
echo "Thank you, continuing...\n";
?>

Running php file from command prompt and taking input

you can do this:

 $input = trim(fgets(STDIN));

fgets takes input from STDIN and waits for a linebreak.
The trim function makes sure you don't have that line break at the end.

So to add this in your example:

echo "ENTER  YOUR NUMBER";
$num = trim(fgets(STDIN));
echo "Enter your message";
$msg = trim(fgets(STDIN));
echo $num;
echo $msg;

PHP-CLI How to write in user input?

That is not the default/commen way of handling this stuff in shell scripts. In almost any programm you'll find something like this:

Enter your value: [Default Value]

$default = "Default Value";
echo "Enter your value: [", $default, "]";
$result = fgets(STDIN);
if (empty($result)) $result = $default;

Where as the default value gets set when the STDIN of that get was empty.

I'm pretty surr that the thing you want to achive isn't possible at all.

send html input as variable into jquery which then sends it to PHP

You are missing .value

var a = document.getElementById("in").value;

Second

You are use jquery as below

var a = $("#in").val();

Change JS function as below

$(document).ready(function(){

$("button").click(function(){

$.post("runphp.php", { jsvar: $("#in").val()}, function(jsvar){ alert(jsvar);
});
});
});

piping data into command line php?

As I understand it, $argv will show the arguments of the program, in other words:

php script.php arg1 arg2 arg3

But if you pipe data into PHP, you will have to read it from standard input. I've never tried this, but I think it's something like this:

$fp = readfile("php://stdin");
// read $fp as if it were a file

Accessing $_SERVER variables from command line

If you're setting the server variables in your web server config, then they won't be present when you access PHP via the command line. (Since the web server won't be involved at all.)

To use $_SERVER variables in your CLI PHP script, see: Set $_SERVER variable when calling PHP from command line?


To summarize:

run: VALUE_ONE=1 ANOTHER_VALUE=2 php cli.php



Related Topics



Leave a reply



Submit