Use `if ($_ GET) `to determine if there are any defects in the get method of the form submission?

what is the way to determine how a form is submitted in an PHP file?

can I use if ($_ GET) in my example to determine whether the form is submitted or not?

<form method="get">
    <input type="text" name="num1" placeholder="num1">
    <input type="text" name="num2" placeholder="num2">
    <button type="submit"></button>
</form>

<?php

if($_GET){
    $num1 = $_GET["num1"];
    $num2 = $_GET["num2"];

    $result = $num1 + $num2;

    echo $result;
}


?>
Php
Apr.09,2021
When

is loaded for the first time, if has an empty array, no environment at hand, no verification of what it is, but should use empty ($_ GET)


is not rigorous, just judge whether $_ GET is an empty array, and do not judge whether the parameters are correct. If you remove a parameter
such as test.php?num1=1&num2=3 from the submitted url to test.php?num1=1 manually, you will find that you have reported an error


if (count ($_ GET)) {
/ / todo
}


by $GET. In some cases, post requests, but url has other parameters. There is a REQUEST_MEYHOD in $_ SERVER that can determine the requestor. You can take a look at how the bottom of the common framework determines the mode of the request.


be strict. If the other side gives you a random parameter, get is not safe.

if(
    isset($_GET['num1']) 
    && $_GET['num1']
    && isset ($_GET['num2'])
    && $_GET['num2']
){
    //TODO
}

receives it with a variable, and then determines whether it is empty or not. For example: $data = I ('get.'); if (empty ($data)) {/ / empty proof no data} else {
/ / receive
}

)
Menu