从链接传递PHP值

时间:2013-01-09 19:27:58

标签: php

我是PHP和学习的新手。我试图通过url链接传递一个值,但它似乎不起作用。

我传递的链接值是http://www.mysite.com/index.php?id=f

我想运行一个js脚本,如果下面看到ID不是F,但是现在我运行它时。它没有做任何事情:

<?php
$ShowDeskTop = $_GET['id'];
if (isset($ShowDeskTop)){
    echo $ShowDeskTop;

    if ($ShowDeskTop != "f"){
       echo "ShowDeskTop Value is not F";
               echo "<script type=\"text/javascript\">";
       echo "if (screen.width<800)";
       echo "{";
       echo "window.location=\"../mobile/index.php\"";
       echo "}";
       echo "</script>";  
    };
};
?>

我知道这很容易PHP 101,但我无法弄清楚。我已经尝试了从w3schools到Google上其他网站的所有内容,以获得答案并且没有运气。有人可以告诉我我做错了吗?

谢谢!

7 个答案:

答案 0 :(得分:2)

$ShowDeskTop$ShowDesktop变量名称区分大小写不同!

答案 1 :(得分:1)

我考虑过写!=而不是<>

答案 2 :(得分:1)

这是永远无法工作的,因为你设置变量AFTER检查它是否存在。 最简单的方法:

<?php
if (isset($_GET['id'])) {
    echo $_GET['id'];

    if ($_GET['id'] != 'f') {
?>
<script type="text/javascript">
    if (screen.width < 800) {
        window.location = "../mobile/index.php";
    }
</script>
<?php
    }
}
?>

我不认为&lt;&gt;在PHP中是有效的(它在VB.NET中..)不是运算符是!=或!==(严格/宽松的比较)。

此外,您不必使用;

关闭if语句

此:

if (expr) {

}

有效而不是:

if (expr) {

};

答案 3 :(得分:1)

你有很多问题,包括错误的变量大小写(即变量不匹配),在变量存在之前检查它们等等。你可以简单地做这样的事情:

if (!empty($_GET['id'])) { // note I check for $_GET['id'] value here not $ShowDeskTop
    $ShowDeskTop = $_GET['id']; 
    echo $ShowDeskTop; // note I change case here

    if ($ShowDeskTop !== "f"){  // note the use of strict comparison operator here
       echo "YES, the id doesn't = f";
       echo "<script type=\"text/javascript\">";
            echo "if (screen.width<800)";
            echo "{";
            echo "window.location=\"../mobile/index.php\"";
            echo "}";
       echo "</script>";
    } // note the removal of semicolon here it is not needed and is bad coding practice in PHP - this is basically just an empty line of code
} // removed semicolon here as well

答案 4 :(得分:0)

首先,您需要;

末尾的echo $ShowDesktop

f

if ($ShowDeskTop <> "f"){的含义是什么

答案 5 :(得分:0)

使用strcmp()代替&lt;&gt;操作

尝试

if(!strcmp($ShowDeskTop, "f")){
   echo "YES, the id doesn't = f";
}

答案 6 :(得分:0)

<?php

   $ShowDeskTop = $_GET['id'];     // assign before checking

   if (isset($ShowDeskTop)){

    //echo $ShowDeskTop;             

    if ($ShowDeskTop !== "f"){
       echo "YES, the id doesn't = f";
       echo "<script type='text/javascript'>";
            echo "if (screen.width<800)";
            echo "{";
            echo "window.location.replace('../mobile/index.php');"; // assuming your path is correct
            echo "}";
       echo "</script>";
    }
   }
 ?>