?
Solved

Pre-populate STDIN

Posted on 2011-02-26
4
Medium Priority
?
411 Views
Last Modified: 2012-05-11
Hi! Please consider following:

#!/usr/bin/php
<?php
fwrite(STDOUT, "Please enter something:\n");
$answer = trim(fgets(STDIN));
echo "$answer\n";
?>

Open in new window


Is it possible to pre-populate a default value here in STDIN?
0
Comment
Question by:Julian Matz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 79

Assisted Solution

by:arnold
arnold earned 400 total points
ID: 34989759
echo "data for STDIN" | your_php_script
your_php_script < "filename"
The data from the filename will be available on STDIN.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1200 total points
ID: 34992012
Maybe something like this would make sense for your app.
#!/usr/bin/php
<?php
fwrite(STDOUT, "Please enter something:\n");
$answer = trim(fgets(STDIN));

if (empty($answer)) $answer = 'PREPOPULATED ANSWER';

echo "$answer\n";
?>

Open in new window

0
 
LVL 1

Assisted Solution

by:BasementCat
BasementCat earned 400 total points
ID: 34992292
You can't "pre-populate" STDIN since it's a file handle, but what Ray suggested will work.  The only way to "pre-populate" stdin would be to pipe some input to the program ahead of time, like
echo "hello, world" |yourprogram.php

Open in new window

0
 
LVL 21

Author Comment

by:Julian Matz
ID: 34992794
Thanks, I thought of the if (empty($answer)) solution afterwards. It will work for me. I just have the default answer in brackets after the question.

Thanks for your help.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question