Here I am showing how PHP can be used in a way that isn’t on a server or web page. You can use PHP in a terminal to make a game, or you can use it for more practical uses like a more elaborate sh or batch file in a command line interface. If you recall in the previous tutorial (020), I made up a story. Well, now we can introduce this to where it is somewhat like a game. It can be like a game because we can get the user’s input easily through the terminal. Note that this is not made for a server, but the command line interface. There are two parts of this tutorial.
Part 2
Here are the sources used in this tutorial:
tut028.php
<?php //PHP Tutorial 028 Terminal Usage //or the from the command line //add ;C:\wamp\bin\php\php5.3.0 // on the end of your PATH system variable in windows if you use WAMP //echo "tacos eat lunch for dinner"; //for more info go to http://php.net/manual/en/features.commandline.php echo "what is your name blessed ? "; $name = trim(fgets(STDIN)); /*echo "Hello $name how are you this day? "; $howami = trim(fgets(STDIN)); echo "Well that is just dandy, you are $howami.";*/ echo "Hello, $name, it was a dark and stormy "; $x = rand(1,6); $save = mktime(0,0,0,2,$x,1995); echo date("l",$save); echo " night! You are with "; $people = rand(2,40); echo $people; echo " other people, and you want to go and mow the grand lawn of the ages! "; echo "\nOne of your best friends, "; $friends = array(); $friends[] = "Joe"; $friends[] = "James"; $friends[] = "John"; $friends[] = "Jacob"; $friend = $friends[rand(0,count($friends)-1)]; echo $friend; echo " wants to join you on your wonderful adventure! Now, you must decide to let him or not!\n"; echo "\nSay Y for Yes, and N for no. "; $yon = strtoupper(trim(fgets(STDIN))); $yesorno = 0; if($yon == "Y"){ $yesorno = 1; } if($yesorno){ echo " You said yes to $friend!"; }else{ echo " You said no to $friend!"; } echo "\n Because you said, "; if($yesorno){ echo "yes"; }else{ echo "no"; } echo ", your friend is quite "; if($yesorno){ echo "happy!"; }else{ echo "sad."; } echo " Now you and "; if($yesorno){ echo $people + 1; }else{ echo $people; } echo " other people traveled over the Neatherlands! "; ?>