Stuck in php hell just starting out

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ijustdidit2011
Forum Newbie
Posts: 2
Joined: Mon Jan 21, 2008 8:41 pm

Stuck in php hell just starting out

Post by ijustdidit2011 »

Code: Select all

<?php
    print("Are you Red? (Y/N)");
    fscanf(STDIN,"%s", $isRed);
 
    print("Are you Peanut? (Y/N)");
    fscanf(STDIN,"%s", $isPeanut);
    
    if($isRed == "Y");
    {
        if($isPeanut == "Y");
        {
            print("\nYou are a red & peanut");
        }
        else
        {
            print("\nYou are  red & plain");
        }       
    }   
    else
    {
        if($isPeanut == "Y");
        {
            print("\nYou are a green peanut");
        }
        else
        {
            print("\nYou are a green & plain");
        }   
    }
    
    
    print("\n\nPRESS ENTER TO EXIT");
    $dummy = fgets(STDIN);
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Stuck in php hell just starting out

Post by Christopher »

fscanf() ?!? :drunk:

I am guessing that you are not using PHP on the command line (but maybe you are). To get input from the browser, the variables in the request will be in the one of the superglobal arrays $_GET or $_POST. So for example, if the URL was:

Code: Select all

mysite.com?red=Y&peanut=N
Then your code would get the values with:

Code: Select all

$isRed = $_GET['red'];
$isPeanut = $_GET['peanut'];
(#10850)
ijustdidit2011
Forum Newbie
Posts: 2
Joined: Mon Jan 21, 2008 8:41 pm

Re: Stuck in php hell just starting out

Post by ijustdidit2011 »

if i dont use fscanf it causes problems changing to green while using the enter button.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Stuck in php hell just starting out

Post by Christopher »

So you are using PHP on the command line you rascal. :)

First, I would recommend that if you want to write command line applications that you use C, Perl, Python, etc. (or Shell Script on Unix). The only reason I would suggest to use PHP on the command line is to use a single language for tools that work in conjunction with a PHP web application.

If you insist, then I would recommend reading this:

http://www.php.net/manual/en/features.commandline.php
(#10850)
Post Reply