Hi,
I'm trying to use PHP as a shell scripting language...
What I need to do is read from the shell - but how?
C has scanf,
in PERL you do $var = <STDIN>;
but what about PHP?
I could not find a satisfying solution yet!
reading from STDIN
Moderator: General Moderators
- Fredix
- Forum Contributor
- Posts: 101
- Joined: Fri Jul 18, 2003 2:16 pm
- Location: Wehr (Eifel) Germany
- Contact:
Sorry, but I could not find the function you are talking about?!
I came across the following solution:
but this is not satisfying because............well look at the following script:
What should it do?
It should tell you to make some input, then read your input and finally print it.
WHAT IT DOES
Waits for input, then tells you type something and then directly prints what you've typed at the beginning.
Has anyone made the same/different experiences?
I came across the following solution:
Code: Select all
<?php
define('STDIN',fopen("php://stdin","r"));
$stdin = fgets(STDIN, 1024);
?>Code: Select all
#!/usr/bin/php -f
<?php
echo "Say something!\n";
define('STDIN',fopen("php://stdin","r"));
$stdin = fgets(STDIN, 1024);
echo "You said ".$stdin;
?>It should tell you to make some input, then read your input and finally print it.
WHAT IT DOES
Waits for input, then tells you type something and then directly prints what you've typed at the beginning.
I also heard about the function readline() but to use it I need to recompile my php... so I could not test it yet. But I even doubt it would help because as far as I know it is just the synonym for what I have so probably it would produce the same unsatisfying result....bash-2.05b$ ./shebang.php
blabla
Say something!
You said blabla
bash-2.05b$
Has anyone made the same/different experiences?
Actually, getinput() was a user-function that I used, rather than a inbuildt one, so sorry. Of course that wouldn't help you. Didn't know where i picked that up, so I found this while searching.
Not a very interesting page, but might give you more info as a function is used in this case.
http://www.zend.com/zend/spotlight/shellscriptingp1.php
Not a very interesting page, but might give you more info as a function is used in this case.
http://www.zend.com/zend/spotlight/shellscriptingp1.php
hmm... It works pretty well for me:
also in CLI version STDIN is a predefined constant.
Code: Select all
weirdan@office:~$ ./q.php
Say something!
asd
You said asd
weirdan@office:~$ php --version
PHP 4.3.3 (cli) (built: Oct 23 2003 14:55:11)
Copyright (c) 1997-2003 The PHP Group- Fredix
- Forum Contributor
- Posts: 101
- Joined: Fri Jul 18, 2003 2:16 pm
- Location: Wehr (Eifel) Germany
- Contact:
@Weirdan
@JAM
am reading.... though it looks like it is the same, hmm gonna try anyway
It seems to be the cgi version though the man page is for the CLI version!bash-2.05b$ php -v
PHP 4.3.3 (cgi-fcgi) (built: Sep 12 2003 00:18:31)
Copyright (c) 1997-2003 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2003 Zend Technologies
@JAM
am reading.... though it looks like it is the same, hmm gonna try anyway