What exactly does the PHP function “test_input()” do?

前端 未结 4 632
夕颜
夕颜 2021-02-04 11:01

I know I will be voted down for this, but I need to ask. What does the code below mean? Specifically, I am confused by

if (empty($_POST[\"comment\"])) { $comme         


        
4条回答
  •  孤独总比滥情好
    2021-02-04 11:26

    test_input is a user defined function and not a function built into PHP. Looking at the code it does indeed mean that if no comment has been inserted then it will be empty, however if it is not empty run the comment through the test_input function.

    On this page http://www.w3schools.com/php/php_form_validation.asp we can see where they have declared the test_input function, specifically this part of the page

    
    

    Lets say for example that every comment that was made you wanted to put a date and timestamp on the end, the following code would achieve this.

    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
         echo 'posted';
    
         if (empty($_POST["comment"]))
         {
             $comment = "";
         }
         else
         {
            $comment = add_date($_POST["comment"]);
         }
     }
    
     function add_date($comment)
     {
          return $comment . date('d-m-Y G:i:s');
     }
    

    Therefore if the user entered Hello this is a comment, $comment would now be Hello this is a comment 28-11-2014 16:00:00

    So to finalize, test_input is simply a function name created by w3schools and they use it whenever the page refers to any kind of validation of input etc.

提交回复
热议问题