php获取返回未分配的值

时间:2015-11-09 18:09:03

标签: php url get

我有一个简单的测试页面,用于确认GET值

<?php
if ($_GET ['car'] = 'yes') {
echo 'You have a car. It is set to: ' . $_GET ['car'] . '<br>';
}
if ($_GET ['boat'] = 'yes') {
echo 'You have a boat. It is set to: ' . $_GET ['boat'] . '<br>';
}
?>

我正在验证多个网址字符串:

  1. test.php?car = yes&amp; boat = no
  2. test.php的汽车=否安培;舟=无
  3. test.php的汽车=否安培;舟=是
  4. test.php的汽车= YES&安培;舟=是
  5. 如果我导航到URL“test.php?car = no&amp; boat = no”,echo函数将打印“yes”而不是“no”。

    请帮助我理解为什么我没有收到'否'而不是'是'的预期结果。谢谢。

2 个答案:

答案 0 :(得分:0)

你的if语句使$ _GET [&#39; car&#39;]等于&#39;是&#39;而不是检查IF $ _GET [&#39; car&#39;] IS等于&#39;是&#39;。试试这个:

<?php
    if ($_GET['car'] == 'yes') {
        echo 'You have a car. It is set to: ' . $_GET['car'] . '<br>';
    }
    if ($_GET['boat'] == 'yes') {
        echo 'You have a boat. It is set to: ' . $_GET['boat'] . '<br>';
    }
?>

答案 1 :(得分:0)

作业运算符

$a=$b

基本赋值运算符是&#34; =&#34;。 as&#34;等于&#34;。唐&#39;吨。 这实际上意味着左操作数被设置为右侧表达式的值(即&#34;设置为&#34;)。

比较运算符

$a == $b 

Results TRUE, if $a is equal to $b after type juggling.

因此,将代码更改为以下代码,以便在未分配时进行比较。

<?php
if ($_GET ['car'] == 'yes') {
echo 'You have a car. It is set to: ' . $_GET ['car'] . '<br>';
}
if ($_GET ['boat'] == 'yes') {
echo 'You have a boat. It is set to: ' . $_GET ['boat'] . '<br>';
}
?>