PHP and undefined variables strategy

前端 未结 12 1096
名媛妹妹
名媛妹妹 2021-01-17 09:49

I am a C++ programmer starting with PHP. I find that I lose most of the debugging time (and my selfesteem!) due to undefined variables. From what I know, the only way to dea

12条回答
  •  臣服心动
    2021-01-17 10:31

    Personally, I try and set variables, even if it's with an empty string, array, Boolean, etc. Then I use a function such as isset() before using them. For example:

    $page_found = false;
    
    if ($page_found==false) {
        // Do page not found stuff here
    }
    
    if (isset($_POST['field'])) {
        $value = $_POST['field'];
        $sql = "UPDATE table SET field = '$value'";
    }
    

    And so on. And before some smart-ass says it: I know that query's unsafe. It was just an example of using isset().

提交回复
热议问题