mpsn Posted December 3, 2011 Share Posted December 3, 2011 Hi, I'm getting an infinite loop using PHP and CLI. <?php #!/usr/bin/php -q $userName = fread(STDIN,10); $password = fread(STDIN,10); $isAuthenticated = false; $realUserId = "aaa"; $realPassword = "pass"; while( $isAuthenticated!=true ) { echo "Please login to begin: \n"; echo "Enter username: ".$userName."\n"; echo "Enter password: ".$password; //now check to make sure is registerd user //$userId = $u->userLogin($userName, $password); if($userName != $realUserId && $password != $realPassword) echo "Invalid username or password. Please try again.\n"; else { echo " You are logged in! You can begin.\n"; $isAuthenticated = true; } } ?> All help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/252358-php-cli-help/ Share on other sites More sharing options...
trq Posted December 3, 2011 Share Posted December 3, 2011 #!/usr/bin/php -q <?php $isAuthenticated = false; $realUserId = "aaa"; $realPassword = "pass"; while( $isAuthenticated!=true ) { echo "Please login to begin: \n"; echo "Enter username: \n"; $userName = fread(STDIN,10); echo "Enter password: \n"; $password = fread(STDIN,10); //now check to make sure is registerd user //$userId = $u->userLogin($userName, $password); if($userName != $realUserId && $password != $realPassword) echo "Invalid username or password. Please try again.\n"; else { echo " You are logged in! You can begin.\n"; $isAuthenticated = true; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293754 Share on other sites More sharing options...
mpsn Posted December 3, 2011 Author Share Posted December 3, 2011 This is related I guess, why doesn't it exit while loop when i type 'q'? <?php $task = "f"; function showTasks()//h { echo "TASKS:\n"; echo "b - show breakfast menu;" echo "l - show lunch menu;" echo "q - quit: Exit program\n"; echo "h: to view this list of options\n\n\n"; } while($task != 'q') { echo "Please choose a task: "; $task = fread(STDIN,10); echo $task; echo "\n"; if( trim($task) == "h") showTasks(); //other tasks will call other functions, i do later } echo "Thanks for using our menu!"; ?> All help appreicate. Quote Link to comment https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293763 Share on other sites More sharing options...
Pandemikk Posted December 3, 2011 Share Posted December 3, 2011 You use trim($task) to find out if it's h. You should use it to find out if it's q, too. $task = trim(fread(STDIN,10)); Quote Link to comment https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293770 Share on other sites More sharing options...
mpsn Posted December 3, 2011 Author Share Posted December 3, 2011 Is it possible to use both fread(STDIN,10) with $argv in php cli? function foo($a,$b); function foobar($c); let's say I have these functions(I'm aware it's function headers), so I wanto use fread(STDIN,10) to choose the function, and $argv to pass in arguments to the above functions. Can someone show me quickly how to approach this, i'd appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293789 Share on other sites More sharing options...
mpsn Posted December 3, 2011 Author Share Posted December 3, 2011 Here is concrete exampel of what i mean, let's say a simple calculator class with add, factorial finding, and government sales tax retrieiving, why doesn't this work? <?php #!/usr/bin/php -q class Calculator { function factorial($int) { if ($int == 1) return 1; else return $int*factorial($int-1); } function add($num1, $num2) { return $num1 + $num2; } function getGSTof($price) { return 0.05*$price; } } //RUNNER $c = new Calculator(); $task = ""; //TASKS:f for factorial, then 1 arg, a for add, then 2 args, g for gst, then 1 arg while($task != "q") { echo "Please choose a task: "; $task = trim( fread(STDIN,10) ); //FACTORIAL if ( $task == "f" ) { $num = $argv[1]; $factorial = $c->factorial($num); echo $factorial; echo "\n"; } //ADDITION if ( $task == "a" ) { $num = $argv[1]; $num2 = $argv[2]; $sum = $c->add($num, $num2); echo $sum; echo "\n"; } //GET GST if ( $task == "g" ) { $price = $argv[1]; $gst = $c->getGSTof($price); echo $gst; echo "\n"; } //QUIT if($task == "q" ) break; }//END WHILE echo "\n\n\n\nEnd Calculator program"; ?> All help apprciated. Quote Link to comment https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293799 Share on other sites More sharing options...
trq Posted December 3, 2011 Share Posted December 3, 2011 You can only use $argv to pass in initial arguments to the script. It's not much use if you don't know what function there going to be calling. Also, the shebang needs to be on the first line not within the <?php code block. Quote Link to comment https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293811 Share on other sites More sharing options...
mpsn Posted December 3, 2011 Author Share Posted December 3, 2011 Sorry, can you elaborate a little more. Quote Link to comment https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293812 Share on other sites More sharing options...
trq Posted December 3, 2011 Share Posted December 3, 2011 On what? Quote Link to comment https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293814 Share on other sites More sharing options...
mpsn Posted December 3, 2011 Author Share Posted December 3, 2011 You can only use $argv to pass in initial arguments to the script. It's not much use if you don't know what function there going to be calling. How would I do it with the example Calculator? Quote Link to comment https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293817 Share on other sites More sharing options...
trq Posted December 3, 2011 Share Posted December 3, 2011 I would prompt the user for what function they want to use, then prompt them again for the arguments needed. Quote Link to comment https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293818 Share on other sites More sharing options...
mpsn Posted December 3, 2011 Author Share Posted December 3, 2011 So there's no way to do it w/o sub-prompting. I mean, enter a character for task (this tells it which function to choose), then it will read the characters that follow the task (these are the arguments) If not, I'll do as told. Quote Link to comment https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293819 Share on other sites More sharing options...
trq Posted December 3, 2011 Share Posted December 3, 2011 You could split the input from STDIN by spaces and parse that as arguments. Quote Link to comment https://forums.phpfreaks.com/topic/252358-php-cli-help/#findComment-1293825 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.