Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Pre-populate STDIN

Posted on 2011-02-26
4
Medium Priority
?
419 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 80

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

604 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