PHP? url字符串无法使用变量

时间:2015-05-18 15:41:21

标签: php

我正在使用PHP显示不同的消息(使用ècho),具体取决于附加到网址的?,如下所示:http://somthing.com/page.php?something=1。我将名为code的变量附加到网址的末尾:http://somthing.com/page.php?code=1

      <?php
           $code;
            if(code == 1){ echo "Your account has been created."; 
            } else if(code == 2){
              echo "Thank you for your feedback.";
              if(isset($_POST["sent"])){
                echo "You will receive an email at " . $_POST["youremail"] . ".";
              }
            }


?>

我在第1行声明了变量。没有显示任何消息。我在这做错了什么? Page the php is on.

2 个答案:

答案 0 :(得分:0)

您需要从$ _GET

获取数据

这样的事情:

$code = $GET["code"];

答案 1 :(得分:0)

  • 您应该将$ _GET [&#39;代码&#39;]设置为$ code variable
  • 使用变量
  • 时应添加$ sign

固定代码:

$code = isset( $_GET['code'] ) ? $_GET['code'] : 0;
if ( $code == 1 ) {
    echo "Your account has been created.";
} else if ( $code == 2 ) {
    echo "Thank you for your feedback.";
    if ( isset( $_POST["sent"] ) ) {
        echo "You will receive an email at " . $_POST["youremail"] . ".";
    }
}