简体   繁体   中英

PHP flow logic problem

I have a bit of a problem with my PHP code, I am assigning values to variables in different states of the flow depending on what I receive, but for some reason it keeps getting stuck at one point, here is the code.

if (isset($session)) {  
    //if the user is in the database
    if ($row == 1) {

        $from = $_GET['from'];

            if (isset($from)) { 

                $page = $_GET['page'];

                switch ($page) {
                    case "game":

                        $page = "game";
                        sendVars($page);//send the variable

                        break;
                    case "gallery":

                        $page = "gallery";
                        sendVars($page);//send the variable

                        break;
                    case "leaderboard":

                        $page = "leaderboard";
                        sendVars($page);//send the Variable                     
                        break;

                }
        }else {             
                $page = "game";

                sendVars($page);//send the variable
            }

        //if the user is not in the database
        }else {

            //do this
        }

} else {

    //register
}

Now for some odd reason, it keeps setting the value of $page to game, even though I set the page variable to gallery like so http://www.mydomai.com/?from=set&page=gallery . the only reason for this that I can think of is that my switch is not working as it should? or it is bypassing the switch somehow?

Thanx in advance!

I just ran your code after removing a few of the unessersary variable assignments:

<?php

// I added this function just for testing
function sendVars($page) {
  echo $page;
}

if (isset($_GET['from'])) {

  $page = $_GET['page'];

  switch ($page) {
    case "game":
      sendVars($page); //send the variable

      break;
    case "gallery":
      sendVars($page); //send the variable

      break;
    case "leaderboard":
      sendVars($page); //send the Variable
      break;
  }
} else {
  $page = "game";

  sendVars($page); //send the variable
}

And it all seems fine, xxx.php?from=1&page=gallery echos out "gallery", try doing a print_r($_GET) at the top of your script and see what it prints out and let us know.

On a side note I think the below may be shorter for you and still do the same thing:

if (isset($_GET['from'])) {
  // Check if $_GET['page'] exsists and is either game, gallery or leaderboard
  if (isset($_GET['page']) && in_array($_GET['page'], array('game', 'gallery', 'leaderboard')))
    sendVars($_GET['page']);
}
else
  sendVars('game');

I hope this helps

Cheers Luke

Try doing a var_dump($page);exit; before the switch and see what it spits out.

Also you can do a var_dump($from) and see what that is spitting out - it may be that it goes to the else, so it may not even be getting to the switch.

If this is inside a function, I personally prefer guard-style clauses than constantly increasing the levels of indentation. The idea is you pick out the bad conditions (ie if something is going wrong) to "protect" the larger block of logic.

In your case that's the switch statement.

if (!isset($session))
    return ...; // register

if ($row != 1)
    return ...; // do this

$from = $_GET['from'];
$page = $_GET['page'];

if (isset($from)) switch ($page) {
    case "game":
        $page = "game";
        break;

    case "gallery":
        $page = "gallery";
        break;

    case "leaderboard":
        $page = "leaderboard";                   
        break;
}

else $page = "game";

return sendVars($page);// don't need to repeat this if all cases do it!

It's just a style of code and it's not going to fix all (if any) of your problems. You actually don't need the switch block in there for this code. I can't see that it's doing anything.

You don't necessarily need the switch statement. I can't see an obvious problem with your logic but I think this will do the same thing:

 if (isset($session)) { //if the user is in the database if ($row == 1) { $page = (in_array($_GET['page'],array('game','gallery','leaderboard'))) ? $_GET['page'] : "game"; sendVars($page); //send the variable } else //if the user is not in the database { //do this } } else { //register } 

Just saw Luke used the same in_array method but 25 mins before me. Serves me right for being slow!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM