0

I'm having difficulty with a homework assignment using PHP.

    <?php
        if (!isset($_GET['board'])){
            echo "Board value not set";
        }else{
        $position = filter_input(INPUT_GET ,'board');
        $squares = str_split($postion);

    echo $squares[0].'<br/>';
    echo $squares[1].'<br/>';
    echo $squares[2].'<br/>';
    echo $squares[3].'<br/>';
    echo $squares[4].'<br/>';
    echo $squares[5].'<br/>';
    echo $squares[6].'<br/>';
    echo $squares[7].'<br/>';
    echo $squares[8] .'<br/>';

    if (winner('x',$squares)){
        echo 'You win.';
    }elseif(winner('o', $squares)){
        echo 'I win.';
    }else{
        echo 'No winner yet.';
    }
}
function winner($token, $squares){
    $won = false;
    if(($squares[0] === $token) && ($squares[1] === $token) && ($squares[2] === $token)){
        $won = true;
    }elseif(($squares[0] === $token) &&($squares[3] === $token) && ($squares[6] === $token)){
        $won = true;
    }elseif(($squares[0] === $token) &&($squares[4] === $token) && ($squares[8] === $token)){
        $won = true;
    }elseif(($squares[1] === $token) &&($squares[4] === $token) && ($squares[7] === $token)){
        $won = true;
    }elseif(($squares[2] === $token) &&($squares[4] === $token) && ($squares[6] === $token)){
        $won = true;
    }elseif(($squares[2] === $token) &&($squares[5] === $token) && ($squares[8] === $token)){
        $won = true;
    }elseif(($squares[3] === $token) &&($squares[4] === $token) && ($squares[5] === $token)){
        $won = true;
    }elseif(($squares[6] === $token) &&($squares[7] === $token) && ($squares[8] === $token)){
        $won = true;
    }
    return $won;
}
?>

This is the PHP I'm using. My problem comes when I enter the url and follow it with ?board=xxx---ooo the str_split method isn't storing anything in the squares[]. It recognizes that there is 'board' because the !isset gets triggered but it doesn't pass the characters through so my echo prints 8 returns, and my winner function can't evaluate properly.

I feel like I'm missing something minor here and just can't see it.

1
  • Where is your form, is your form method set to get ? Commented Jan 15, 2016 at 8:58

2 Answers 2

2

You have a typo, change :

$squares = str_split($postion); 

To :

$squares = str_split($position);
Sign up to request clarification or add additional context in comments.

Comments

0

Please change the following line:

$position = filter_input(INPUT_GET ,'board');

with

$position = $_GET['board'];

and care about your variable name spelling.

You missed an 'i' in your naming.

It could resolve your problem, I think.

2 Comments

Without any filter specified as the third parameter for the filter_input there's no difference between the two. The answer is the one given in the other answer before, as you also edited it later. So the first part is just misleading.
First solution will ensure that the data is gotten correctly from $_GET. without this try, any debugging try may faults. As the second solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.