Search code examples
phpif-statementget

if statement with $_GET['part']


I'm trying to get one page with 3 parts with the if state ment. Please look thru the code, and help me.

And I would be glad if you dont tell me to lear this or that, cuz that is what i'm bin doing.

<?php
if (isset($_GET['part'])&&!empty($_GET['part'])){
   if($_GET['part'] = '1'){
       ## Part 1
       ## Some content
       echo '1';
    }else if($_GET['part'] = '2'){
       ## part 2
       ## Some content
       echo '2';
    }else if($_GET['part'] = '3'){
       ## Part 3
       ## Some content
       echo '3';
    }

    }else{
       header('Location: index.php?part=1');
    }
?>

Solution

  • Wouldn't this be easier?

    <?php
    switch ($_GET['part'])) {
        case '1':
           ## part 1
           ## Some content
           break;
        case '2':
           ## part 2
           ## Some content
           break;
        case '3':
           ## Part 3
           ## Some content
           break;
        default:
           header('Location: index.php?part=1');
    }
    ?>