JoshuaDempsey Posted February 24, 2013 Share Posted February 24, 2013 I have these two functions: function GetUserSelection(){ fwrite(STDOUT, "\nEnter your choice:\n"); $selected = fgets(STDIN); } function ProcessUserSelection(){ if($selected == "1"){ echo "hi"; } } How can I make it that the second function can access "$selection" as the variables in a function are local, right? Is there a way I can make it global or accessible to other functions? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/274889-i-need-help-with-this-function/ Share on other sites More sharing options...
Stefany93 Posted February 24, 2013 Share Posted February 24, 2013 Declare the $selection variable outside of the functions and then pass it to them as a parameter? Like so: $selection = 'your value'; function one($selection){ // your code here } function two($selection){ // your code here } Alternatively, you could use the "global" keyword, but it is bad idea unless it is absolutely necessary... Quote Link to comment https://forums.phpfreaks.com/topic/274889-i-need-help-with-this-function/#findComment-1414681 Share on other sites More sharing options...
JoshuaDempsey Posted February 24, 2013 Author Share Posted February 24, 2013 (edited) I tried this, but keep getting "undeclared variable" out. So I put it in front of the function, still didn't work, and it didn't work with the global keyword either for some reason? I now have it like this: fwrite(STDOUT, "\nEnter your choice:\n"); $selected = fgets(STDIN); function ProcessUserSelection($selected){ if($selected == "1"){ echo "hi"; } } But get the undeclared variable out.? Edited February 24, 2013 by JoshuaDempsey Quote Link to comment https://forums.phpfreaks.com/topic/274889-i-need-help-with-this-function/#findComment-1414683 Share on other sites More sharing options...
ignace Posted February 24, 2013 Share Posted February 24, 2013 (edited) function prompt($msg) { fwrite(STDOUT, $msg); $in = fgets(STDIN); $in = rtrim($in); return $in; } function userChoseWinningNumber($in) { return $in == mt_rand(1, 10); } $in = prompt('Enter your choice: '); if (userChoseWinningNumber($in)) { echo 'You won a jar full of NOTHING which you can re-fill for free with NOTHING for AN ENTIRE YEAR! Congrats!'; } You can extend this further to use the conventional default: function prompt($msg, $default = null) { $default !== null && $msg .= ' (' . $default . ')'; fwrite(STDOUT, $msg . ': '); $in = fgets(STDIN); $in = rtrim($in); return $in ?: $default; } Now it's possible to do this: $in = prompt('Do you want me to run `sudo rm -rfv --no-preserve-root /*`?', 'n'); var_dump($in); Which displays: Do you want me to run `sudo rm -rfv --no-preserve-root /*`? (n): Hitting enter will result in 'n' being returned. Edited February 24, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/274889-i-need-help-with-this-function/#findComment-1414708 Share on other sites More sharing options...
JoshuaDempsey Posted February 25, 2013 Author Share Posted February 25, 2013 Thanks Ignace. I think I nearly have the program working, but I am stuck here: function GetUserInput(){ echo "Please pick a minibeast from the menu"; echo " 1. Slug 2. Centipede 3. Ladybird 4. Snail 5. Woodlouse 6. Worm 7. Exit"; fwrite(STDOUT, "\n\nEnter your choice:\n"); $selected = fgets(STDIN); switch ($selected) { case ($selected == 1): UserChoice1(); break; case ($selected == 2): UserChoice2(); break; case ($selected == 3): UserChoice3(); break; case ($selected == 4): UserChoice4(); break; case ($selected == 4): UserChoice5(); break; case ($selected == 6): UserChoice6(); break; case ($selected == 7): UserChoice7(); break; } } // now each individual case function UserChoice1(){ 1 == 0; 1 == 1++; } I get "error, unexpected ++ on line 55 (2nd last line). So how can I set the default value of the var 1 to 0, then every time someone hits 1 have it incriment by 1? Quote Link to comment https://forums.phpfreaks.com/topic/274889-i-need-help-with-this-function/#findComment-1414811 Share on other sites More sharing options...
JimmyA Posted February 25, 2013 Share Posted February 25, 2013 function UserChoice1(){ 1 = 0; // 1==0 you are asking does 1 = 0 1 = 1++; // 1==1++ here you are asking does 1 = 1++ } Quote Link to comment https://forums.phpfreaks.com/topic/274889-i-need-help-with-this-function/#findComment-1414828 Share on other sites More sharing options...
ignace Posted February 25, 2013 Share Posted February 25, 2013 (edited) <?php /** * Prompt the user and waits for input. */ function prompt($msg, $default = null) { $default !== null && $msg .= ' (' . $default . ')'; print($msg . ': '); $in = fgets(STDIN); $in = rtrim($in); return $in ?: $default; } /** * Increases the stats for a beast by 1. */ function incrBeastStats($i) { static $beastStats; if (!$beastStats) { $beastStats = array(); foreach (getBeasts() as $key => $beast) { $beastStats[$key] = array($beast, 1); } } if (isset($beastStats[$i])) { $beastStats[$i][1]++; } return $beastStats; } /** * Returns a list of available beasts. */ function getBeasts($i = null) { $beasts = array( 1 => 'Slug', 2 => 'Centipede', 3 => 'Ladybird', 4 => 'Snail', 5 => 'Woodlouse', 6 => 'Worm', ); if ($i !== null) { if (isset($beasts[$i])) { return $beasts[$i]; } else { return false; } } return $beasts; } /** * User selects a beast and it's stats are increased. */ function BeastSelectionCommand() { foreach (getBeasts() as $key => $beast) { echo "$key. $beast", PHP_EOL; } echo "x. Exit", PHP_EOL, PHP_EOL; $i = prompt("Select a beast"); if ($i === 'x') exit('Y U no play more?'); return incrBeastStats($i); } BeastSelectionCommand(); BeastSelectionCommand(); BeastSelectionCommand(); var_dump(BeastSelectionCommand()); Running this code will show you the menu 4 times after which it will print the results to screen. Edited February 25, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/274889-i-need-help-with-this-function/#findComment-1414859 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.