【问题标题】:if statement with $_GET['part']带有 $_GET['part'] 的 if 语句
【发布时间】:2014-10-12 11:57:58
【问题描述】:

我正在尝试使用 if 语句获取一个包含 3 个部分的页面。请查看代码,并帮助我。

如果你不让我学这个或那个,我会很高兴,因为这就是我正在做的事情。

<?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');
    }
?>

【问题讨论】:

  • 条件中的相等比较应该是== 而不是=。所以你应该有$_GET['part'] == 'value'
  • 不是一个用于赋值的等号,而是使用两个用于比较的等号!
  • 谢谢,这很有帮助:)

标签: php if-statement get


【解决方案1】:

这不是更容易吗?

<?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');
}
?>

【讨论】:

  • 可能是这样。但为什么我的代码不起作用? :S 但是开关看起来更好。
  • @Magnus,您的代码不起作用,它在 cmets 中进行了解释!
【解决方案2】:
<?php
if (isset($_GET['part'])&&!empty($_GET['part'])){
    if($_GET['part'] >=1 or $_GET['part'] <=3)
        header('Location: index.php?part = $_GET['part']');
    else
        header('Location: index.php?part = 1');
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2016-08-15
    • 2014-10-26
    • 2018-02-20
    • 2018-09-27
    • 1970-01-01
    相关资源
    最近更新 更多