Page 1 of 1
PHP Command Line Input Loops
Posted: Sat Jul 29, 2006 3:44 pm
by supermike
I'm working on a project started in Linux Bash here...
http://sourceforge.net/projects/ust/
Screenshot here:
http://sourceforge.net/dbimage.php?id=82030
...but which I want to convert entirely to PHP-cli. My experience, however, has only been with web-based PHP.
What I don't understand is how to take user input and hold it there in a loop. Anyone got any ideas?
I'd also like a library to draw the (text user interface) TUI although I could convert my existing Bash code to pull this off.
BTW, if you want to help me develop this UST project, let me know. After I convert what I have so far to PHP-cli, I want to start making simple TUI data entry screens that you can tab around in and then press function keys to commit, cancel, or page through the screens. Doing the last bit of editing the configuration files on Ubuntu is going to be a fairly minor thing.
If you're skilled in PHP, you probably could help me knock this project out into beta fairly quickly.
Note: I already have some employees of Ubuntu interested.
Posted: Sat Jul 29, 2006 5:26 pm
by Ambush Commander
I think you'll be SOL on PHP. Here are PHP's cli capabilities:
http://us2.php.net/manual/en/features.commandline.php
Posted: Sat Jul 29, 2006 6:05 pm
by jamiel
You are going to run into irritating problems if you choose PHP for this project. And if you want to get Ubuntu interested you are better off using Python.
Posted: Sat Jul 29, 2006 7:19 pm
by Ollie Saunders
You are going to run into irritating problems if you choose PHP for this project. And if you want to get Ubuntu interested you are better off using Python.
Yep Jamiel is absolutely right. Plus Python is much better suited to console programming and its a great language.
Oh and supermike, being a ubuntu user myself, I wish you the best of luck with your project; it looks really cool.
Posted: Sun Jul 30, 2006 8:51 am
by supermike
ole wrote:Oh and supermike, being a ubuntu user myself, I wish you the best of luck with your project; it looks really cool.
I recommend you download the menuing demo. I think you'll like it.
Posted: Sun Jul 30, 2006 9:33 am
by supermike
I found this sample, but I have to keep playing with it to see if I can come up with something that can also handle one char receives where you press one char and a menu reacts.
Code: Select all
<?php
$f = fopen('php://stdin', 'r');
echo 'Sample Password: ';
system('stty -echo'); //-echo means hidden input
$sPassword = trim(fgets($f));
echo "\n"; //need this after using -echo followed by fgets
echo 'First Name: ';
system('stty echo');
$sFirstName = trim(fgets($f));
system('stty echo');
echo 'Last Name: ';
$sLastName = trim(fgets($f));
fclose($f);
echo "Your full name is $sFirstName $sLastName. The sample password you chose was $sPassword.\n";
?>
I got the code from a coworker in Brazil. It's like a hidden feature. I didn't see anywhere in the php.net docs where system('stty echo') was used.
Posted: Sun Jul 30, 2006 12:18 pm
by Ollie Saunders
What do you type to use php as the command line language in scripts? because...
...obviously doesn't work
Posted: Sun Jul 30, 2006 4:32 pm
by supermike
ole wrote:What do you type to use php as the command line language in scripts? because...
...obviously doesn't work
After you install php4-cli or php5-cli, you just take the sample I provided and run it at command line on Linux or Unix with:
root@host:/# php sample.php
However, if you do 'php -h' you can see some options that you might like.
php -d max_execution_time=1000 sample.php
...would mean that you override the default php.ini setting of 30 seconds to 1000 seconds, just so your script doesn't time out.
It would be great, however, if I could start it off like I would with Perl and Bash like:
#!/usr/bin/php
...but that feature doesn't work in PHP4. It probably works in PHP5, though -- haven't checked.
(I'm a very cautious programmer. I always stay one rev back from the final language compiler or interpreter unless I see ISPs and other developers switching more than 50% of their stuff to it. So I won't switch to PHP5 until PHP5.1 or 5.2 comes out, or perhaps even 6.)
Posted: Sun Jul 30, 2006 4:47 pm
by timvw
supermike wrote:
I got the code from a coworker in Brazil. It's like a hidden feature. I didn't see anywhere in the php.net docs where system('stty echo') was used.
It's not a hidden feature.. It's a *nix shell feature... (I blogged about it in
http://timvw.madoka.be/?p=340 a year ago or so...)
Posted: Sun Jul 30, 2006 4:52 pm
by timvw
supermike wrote:
It would be great, however, if I could start it off like I would with Perl and Bash like:
#!/usr/bin/php
...but that feature doesn't work in PHP4. It probably works in PHP5, though -- haven't checked.
The feature is called 'she-bang' line.. It tells your shell where it can find the program that is capable of handling/interpreting the rest of the file... If you want to know the path to your php executable you can use 'which php'...
(If i'm not mistaken, with php4 there was a php-cli executable... or the php executable an option/switch to tell the program was executed in the shell...)
Posted: Sun Jul 30, 2006 5:30 pm
by Ambush Commander
(I'm a very cautious programmer. I always stay one rev back from the final language compiler or interpreter unless I see ISPs and other developers switching more than 50% of their stuff to it. So I won't switch to PHP5 until PHP5.1 or 5.2 comes out, or perhaps even 6.)
Well, 5.1 is already out, and they're already doing release candidates for 5.2, and 5.0 is not supported, so I really recommend you jump straight to PHP 5.1.4 when you make the switch.
Posted: Sun Jul 30, 2006 8:58 pm
by supermike
Ambush Commander wrote:Well, 5.1 is already out, and they're already doing release candidates for 5.2, and 5.0 is not supported, so I really recommend you jump straight to PHP 5.1.4 when you make the switch.
Thanks for the tip, Ambush. I just might.
Posted: Sun Jul 30, 2006 8:59 pm
by supermike
BTW, I guess I'm famous now. The project is now highlighted at fridge.ubuntu.com.
Posted: Mon Jul 31, 2006 5:27 pm
by Ollie Saunders
BTW, I guess I'm famous now. The project is now highlighted at fridge.ubuntu.com.
Very cool.

Posted: Mon Jul 31, 2006 7:32 pm
by marshall
If you want Python help.. count on me ;D