vijay Posted March 17, 2007 Share Posted March 17, 2007 Hi.. This CLI example.. It will scan the first input and print hello "name" also.. but not the second one... Please try to find solution.. Thanks in advance Code of Application: <?php function read($len = 255) { $fp=fopen('php://stdin', 'r'); $input=fgets($fp, $len); fclose($fp); return str_replace(array("\r", "\n"), array('', ''), $input); //return $input; } echo("Whats your name? "); $name = read(); echo("Hello $name"); echo("\nWhats your last name? "); $last_name = read(); echo("\nHello $last_name"); ?> Regards, Vijay Link to comment Share on other sites More sharing options...
iyeru42 Posted March 18, 2007 Share Posted March 18, 2007 Variables can't go directly into strings. Change "Hello $name" to either "Hello ". $name or "Hello {$name}" Do the same thing with the last name. Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted March 18, 2007 Share Posted March 18, 2007 Variables can't go directly into strings. Change "Hello $name" to either "Hello ". $name or "Hello {$name}" Do the same thing with the last name.Since When???I use variables in strings like that frequently with no problem at all.BUT, you can't include a variable directly if you use single quots ('), only if you use double quots (").Then I didn't find read() in the PHP manual, but try readline() instead, to see if that works better.Good Luck and Don't Panic! Link to comment Share on other sites More sharing options...
vijay Posted March 19, 2007 Author Share Posted March 19, 2007 Hi.. Please first try to look in my code read is userdefined function... Its using.. $fp=fopen('php://stdin', 'r'); $input=fgets($fp, $len); and Its also scanning first input whatever it is.. but its stop scanning after then.. So.. I want solution for that.. hope this'll make sence.. Regards, Vijay Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted March 19, 2007 Share Posted March 19, 2007 Sorry, Missed the function, Hard to see without a "code field".I would still recommend you usin readline().I know I had this problem a few years ago in C++ (hade course in school), as what I can remember is that there were problems with newlines in the output, that it read to the next newline, which for some reason was one that was outputted. Don't remember exactly how I fixed the problem (it was the same, it got the first input, but not the second).Sorry to not be more helpfull Link to comment Share on other sites More sharing options...
justsomeguy Posted March 19, 2007 Share Posted March 19, 2007 The problem is that you are closing stdin. Once you close it, it's done. On Windows at least. So you need to either keep stdin open the entire time, or just use readline. <?php function read($length='255'){ if (!isset($GLOBALS['StdinPointer'])){ $GLOBALS['StdinPointer']=fopen("php://stdin","r"); } $line=fgets($GLOBALS['StdinPointer'],$length); return trim($line); } echo "Enter your name: "; $name=read(); echo "Enter your age: "; $age=read(); echo "Hi $name, Isn't it Great to be $age years old?"; @fclose($StdinPointer); ?> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now