Page 1 of 1

Stuck in php hell just starting out

Posted: Mon Jan 21, 2008 8:43 pm
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);
?>

Re: Stuck in php hell just starting out

Posted: Mon Jan 21, 2008 9:07 pm
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'];

Re: Stuck in php hell just starting out

Posted: Mon Jan 21, 2008 9:18 pm
by ijustdidit2011
if i dont use fscanf it causes problems changing to green while using the enter button.

Re: Stuck in php hell just starting out

Posted: Mon Jan 21, 2008 10:01 pm
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